diff --git a/frontend/src/pages/Manage/components/FileUpload.jsx b/frontend/src/pages/Manage/components/FileUpload.jsx
new file mode 100644
index 0000000..b7f2b03
--- /dev/null
+++ b/frontend/src/pages/Manage/components/FileUpload.jsx
@@ -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 (
+
+
+
+
+
+ { setFiles(Array.from(x.target.files)); propagateChange(x); }}
+ accept={acceptedFileTypes}
+ name={name}
+ ref={inputRef}
+ multiple={allowMultipleFiles}
+ {...inputProperties}
+ style={{ display: 'none' }}
+ />
+ inputRef.current.click()}
+ readOnly
+ value={`${files.length || 'No'} ${files.length === 1 ? 'File' : 'Files'} Selected`}
+ />
+
+ );
+};
+
+export default FileUpload;
diff --git a/frontend/src/pages/Manage/index.jsx b/frontend/src/pages/Manage/index.jsx
index e2bccd4..7c8dd16 100644
--- a/frontend/src/pages/Manage/index.jsx
+++ b/frontend/src/pages/Manage/index.jsx
@@ -1,39 +1,42 @@
-import React, { useState } from 'react';
+import React, { useEffect, useState } from 'react';
import {
Button,
FormControl, Heading, Input, InputGroup, InputLeftAddon,
} from '@chakra-ui/react';
import CurrencyInput from 'react-currency-input-field';
import { AiOutlineNumber } from 'react-icons/ai';
-import { MdPhotoLibrary, MdTextFields } from 'react-icons/md';
import { BsCurrencyDollar } from 'react-icons/bs';
+import { MdTextFields } from 'react-icons/md';
import IconTextDuo from '../../global/components/IconTextDuo';
import store from '../../redux/store';
+import { FileUpload } from './components/FileUpload';
const InputGroupStyle = {
marginTop: '1rem',
marginBottom: '1rem',
};
-const initialState = {
- name: '',
- quantity: 0,
- price: 0.0,
- images: [],
-};
-
const uploadImage = async (file) => {
const fd = new FormData();
fd.append('file', file);
const resp = await fetch('/api/v1/fileupload', { method: 'POST', body: fd });
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}`;
};
export default () => {
const [loading, setLoading] = useState(false);
- const [state, setState] = useState(initialState);
+ const [state, setState] = useState({
+ name: '',
+ quantity: 0,
+ price: 0.0,
+ images: [],
+ });
const save = async () => {
setLoading(true);
@@ -55,19 +58,28 @@ export default () => {
const data = await resp.json();
if (resp.ok) {
+ // Workaround for resetting this form as many custom inputs == VERY difficult to do
// eslint-disable-next-line no-underscore-dangle
- store.dispatch({ type: 'SET_ALERT', data: { header: 'Item Added!', content: `Item created with ID ${data._id}` } });
- setState(initialState);
+ window.location.href = `${window.location.href}?_id=${data._id}`;
} 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) {
- 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);
};
+ // 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 (
Create Product
@@ -122,14 +134,13 @@ export default () => {
- )} text="Images" />
- setState({ ...state, images: Array.from(evt.target.files) })}
+ setState({ ...state, images: Array.from(evt.target.files) })}
+ style={{ cursor: 'pointer !important' }}
/>