Pretty inputs!
This commit is contained in:
parent
778b629fde
commit
ec5a31cbc9
2 changed files with 83 additions and 24 deletions
48
frontend/src/pages/Manage/components/FileUpload.jsx
Normal file
48
frontend/src/pages/Manage/components/FileUpload.jsx
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
// Yoinked from https://github.com/chakra-ui/chakra-ui/issues/457#issuecomment-1079961123
|
||||||
|
import React, { useRef, useState } from 'react';
|
||||||
|
import {
|
||||||
|
Input, InputGroup, InputLeftElement, Icon,
|
||||||
|
} from '@chakra-ui/react';
|
||||||
|
import { FiFile } from 'react-icons/fi';
|
||||||
|
|
||||||
|
export const FileUpload = ({
|
||||||
|
name,
|
||||||
|
placeholder,
|
||||||
|
propagateChange,
|
||||||
|
inputProperties,
|
||||||
|
acceptedFileTypes = '',
|
||||||
|
allowMultipleFiles = false,
|
||||||
|
required = false,
|
||||||
|
}) => {
|
||||||
|
const inputRef = useRef();
|
||||||
|
const [files, setFiles] = useState([]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<InputGroup>
|
||||||
|
<InputLeftElement>
|
||||||
|
<Icon as={FiFile} />
|
||||||
|
</InputLeftElement>
|
||||||
|
|
||||||
|
<input
|
||||||
|
required={required}
|
||||||
|
type="file"
|
||||||
|
onChange={(x) => { setFiles(Array.from(x.target.files)); propagateChange(x); }}
|
||||||
|
accept={acceptedFileTypes}
|
||||||
|
name={name}
|
||||||
|
ref={inputRef}
|
||||||
|
multiple={allowMultipleFiles}
|
||||||
|
{...inputProperties}
|
||||||
|
style={{ display: 'none' }}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
cursor="pointer"
|
||||||
|
placeholder={placeholder || 'Your file ...'}
|
||||||
|
onClick={() => inputRef.current.click()}
|
||||||
|
readOnly
|
||||||
|
value={`${files.length || 'No'} ${files.length === 1 ? 'File' : 'Files'} Selected`}
|
||||||
|
/>
|
||||||
|
</InputGroup>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default FileUpload;
|
||||||
|
|
@ -1,39 +1,42 @@
|
||||||
import React, { useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
FormControl, Heading, Input, InputGroup, InputLeftAddon,
|
FormControl, Heading, Input, InputGroup, InputLeftAddon,
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import CurrencyInput from 'react-currency-input-field';
|
import CurrencyInput from 'react-currency-input-field';
|
||||||
import { AiOutlineNumber } from 'react-icons/ai';
|
import { AiOutlineNumber } from 'react-icons/ai';
|
||||||
import { MdPhotoLibrary, MdTextFields } from 'react-icons/md';
|
|
||||||
import { BsCurrencyDollar } from 'react-icons/bs';
|
import { BsCurrencyDollar } from 'react-icons/bs';
|
||||||
|
import { MdTextFields } from 'react-icons/md';
|
||||||
import IconTextDuo from '../../global/components/IconTextDuo';
|
import IconTextDuo from '../../global/components/IconTextDuo';
|
||||||
import store from '../../redux/store';
|
import store from '../../redux/store';
|
||||||
|
import { FileUpload } from './components/FileUpload';
|
||||||
|
|
||||||
const InputGroupStyle = {
|
const InputGroupStyle = {
|
||||||
marginTop: '1rem',
|
marginTop: '1rem',
|
||||||
marginBottom: '1rem',
|
marginBottom: '1rem',
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialState = {
|
|
||||||
name: '',
|
|
||||||
quantity: 0,
|
|
||||||
price: 0.0,
|
|
||||||
images: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
const uploadImage = async (file) => {
|
const uploadImage = async (file) => {
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('file', file);
|
fd.append('file', file);
|
||||||
const resp = await fetch('/api/v1/fileupload', { method: 'POST', body: fd });
|
const resp = await fetch('/api/v1/fileupload', { method: 'POST', body: fd });
|
||||||
const { output, error } = await resp.json();
|
const { output, error } = await resp.json();
|
||||||
if (error) return null;
|
if (error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return `/api/v1/fileupload/${output}`;
|
return `/api/v1/fileupload/${output}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [state, setState] = useState(initialState);
|
const [state, setState] = useState({
|
||||||
|
name: '',
|
||||||
|
quantity: 0,
|
||||||
|
price: 0.0,
|
||||||
|
images: [],
|
||||||
|
});
|
||||||
|
|
||||||
const save = async () => {
|
const save = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
@ -55,19 +58,28 @@ export default () => {
|
||||||
|
|
||||||
const data = await resp.json();
|
const data = await resp.json();
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
|
// Workaround for resetting this form as many custom inputs == VERY difficult to do
|
||||||
// eslint-disable-next-line no-underscore-dangle
|
// eslint-disable-next-line no-underscore-dangle
|
||||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Item Added!', content: `Item created with ID ${data._id}` } });
|
window.location.href = `${window.location.href}?_id=${data._id}`;
|
||||||
setState(initialState);
|
|
||||||
} else {
|
} else {
|
||||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Item', content: data.error } });
|
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Item', content: JSON.stringify(data) } });
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Item', content: ex } });
|
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Item in Catch Block', content: ex } });
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Workaround for resetting this form as many custom inputs == VERY difficult to do
|
||||||
|
useEffect(() => {
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
if (params.has('_id')) {
|
||||||
|
store.dispatch({ type: 'SET_ALERT', data: { header: 'Item Added!', content: `Item created with ID ${params.get('_id')}` } });
|
||||||
|
window.history.replaceState(null, null, window.location.pathname);
|
||||||
|
}
|
||||||
|
return () => {};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Heading textAlign="center" marginBottom="1rem">Create Product</Heading>
|
<Heading textAlign="center" marginBottom="1rem">Create Product</Heading>
|
||||||
|
|
@ -122,14 +134,13 @@ export default () => {
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
|
|
||||||
<InputGroup style={InputGroupStyle}>
|
<InputGroup style={InputGroupStyle}>
|
||||||
<InputLeftAddon backgroundColor="#53b0fb" color="black"><IconTextDuo icon={(<MdPhotoLibrary />)} text="Images" /></InputLeftAddon>
|
<FileUpload
|
||||||
<Input
|
name="image"
|
||||||
id="images"
|
acceptedFileTypes="image/*"
|
||||||
type="file"
|
required
|
||||||
multiple
|
allowMultipleFiles
|
||||||
accept="image/*"
|
propagateChange={(evt) => setState({ ...state, images: Array.from(evt.target.files) })}
|
||||||
background="gray.50"
|
style={{ cursor: 'pointer !important' }}
|
||||||
onChange={(evt) => setState({ ...state, images: Array.from(evt.target.files) })}
|
|
||||||
/>
|
/>
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue