From 0440003a0293671ef72b8a091c2d521ac947c7eb Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 26 Apr 2022 21:14:20 -0500 Subject: [PATCH] Cleanup,dedup --- .../app/controllers/product/crud/update.ts | 2 + frontend/src/global/api.js | 14 + frontend/src/global/i18n.js | 24 +- frontend/src/global/search.js | 6 + frontend/src/pages/Home/index.jsx | 13 +- .../Manage/components/DeletableImage.jsx | 36 ++ .../pages/Manage/components/DynamicModal.jsx | 27 ++ .../pages/Manage/components/FileUpload.jsx | 4 +- .../pages/Manage/components/modal/Edit.jsx | 227 +++++++++++++ .../src/pages/Manage/components/modal/New.jsx | 201 +++++++++++ frontend/src/pages/Manage/index.jsx | 316 +++++++----------- frontend/src/pages/Product/index.jsx | 8 +- 12 files changed, 650 insertions(+), 228 deletions(-) create mode 100644 frontend/src/global/api.js create mode 100644 frontend/src/global/search.js create mode 100644 frontend/src/pages/Manage/components/DeletableImage.jsx create mode 100644 frontend/src/pages/Manage/components/DynamicModal.jsx create mode 100644 frontend/src/pages/Manage/components/modal/Edit.jsx create mode 100644 frontend/src/pages/Manage/components/modal/New.jsx diff --git a/backend/src/app/controllers/product/crud/update.ts b/backend/src/app/controllers/product/crud/update.ts index dc2aad5..ad40620 100644 --- a/backend/src/app/controllers/product/crud/update.ts +++ b/backend/src/app/controllers/product/crud/update.ts @@ -25,6 +25,8 @@ export default async (req: Request, res: Response) => { product.quantity = req.body.quantity || product.quantity; product.price = req.body.price || product.price; product.hidden = req.body.hidden || product.hidden; + product.description = req.body.description || product.description; + product.images = req.body.images || product.images; await product.save(); return res.status(200).json(product); diff --git a/frontend/src/global/api.js b/frontend/src/global/api.js new file mode 100644 index 0000000..9ab882c --- /dev/null +++ b/frontend/src/global/api.js @@ -0,0 +1,14 @@ +export 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) { + // eslint-disable-next-line no-console + console.error(error); + return null; + } + return `/api/v1/fileupload/${output}`; +}; + +export default uploadImage; diff --git a/frontend/src/global/i18n.js b/frontend/src/global/i18n.js index c052eba..f2335f1 100644 --- a/frontend/src/global/i18n.js +++ b/frontend/src/global/i18n.js @@ -2,6 +2,8 @@ import LocalizedStrings from 'react-localization'; export const General = new LocalizedStrings({ en: { + yes: 'Yes', + no: 'No', close: 'Close', save: 'Save', openMenu: 'Open Menu', @@ -33,6 +35,9 @@ export const Product = new LocalizedStrings({ export const Manage = new LocalizedStrings({ en: { + edit: { + title: (name) => `Editing ${name}`, + }, create: { title: 'Create Product', fileUpload: (count) => `${count || 'No'} ${count === 1 ? 'File' : 'Files'} Selected`, @@ -40,14 +45,17 @@ export const Manage = new LocalizedStrings({ header: 'Item Added!', content: `Item created with ID ${id}`, }), - labels: { - name: 'Name', - desc: 'Description (Supports Markdown)', - price: 'Price', - qty: 'Quantity', - required: 'This Field is Required', - save: 'Save Product', - }, + }, + labels: { + name: 'Name', + desc: 'Description', + price: 'Price', + qty: 'Quantity', + rating: 'Rating', + hidden: 'Hidden', + required: 'This Field is Required', + save: 'Save Product', + saveChanges: 'Save Changes', }, }, }); diff --git a/frontend/src/global/search.js b/frontend/src/global/search.js new file mode 100644 index 0000000..cce341a --- /dev/null +++ b/frontend/src/global/search.js @@ -0,0 +1,6 @@ +export default (term, all) => { + if (term.length) { + return all.filter((x) => x.name.toLowerCase().includes(term.toLowerCase())); + } + return all; +}; diff --git a/frontend/src/pages/Home/index.jsx b/frontend/src/pages/Home/index.jsx index aa26f1a..247d24d 100644 --- a/frontend/src/pages/Home/index.jsx +++ b/frontend/src/pages/Home/index.jsx @@ -9,22 +9,13 @@ import Card from './components/Card'; import { Home as i18n } from '../../global/i18n'; import Loading from '../../global/components/Loading'; import InlineLabelInput from '../../global/components/FormControl/InlineLabelInput'; +import search from '../../global/search'; export default () => { const [hasLoaded, setLoaded] = useState(false); const [allProducts, setAllProducts] = useState([]); const [products, setProducts] = useState([]); - const doSearch = (evt) => { - const terms = evt.target.value.toLowerCase(); - if (terms.length) { - const filtered = products.filter((x) => x.name.toLowerCase().includes(terms)); - setProducts(filtered); - } else { - setProducts(allProducts); - } - }; - useEffect(() => { if (hasLoaded) return () => {}; @@ -55,7 +46,7 @@ export default () => { setProducts(search(evt.target.value, allProducts))} propKeyPressEvt={() => {}} propSubmitEvt={() => {}} type="text" diff --git a/frontend/src/pages/Manage/components/DeletableImage.jsx b/frontend/src/pages/Manage/components/DeletableImage.jsx new file mode 100644 index 0000000..a293937 --- /dev/null +++ b/frontend/src/pages/Manage/components/DeletableImage.jsx @@ -0,0 +1,36 @@ +import React from 'react'; + +import { + Box, Flex, Heading, Image, +} from '@chakra-ui/react'; +import { BsTrash } from 'react-icons/bs'; + +export default ({ src, onClick }) => ( +
+ + + + + + + + +
+); diff --git a/frontend/src/pages/Manage/components/DynamicModal.jsx b/frontend/src/pages/Manage/components/DynamicModal.jsx new file mode 100644 index 0000000..36038cb --- /dev/null +++ b/frontend/src/pages/Manage/components/DynamicModal.jsx @@ -0,0 +1,27 @@ +import React from 'react'; +import { + Button, Modal, ModalBody, + ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, +} from '@chakra-ui/react'; +import { General as i18n } from '../../../global/i18n'; + +export default ({ + open, header, superOnClose, size, children, +}) => ( + + + + {header} + + + {children} + + + + + + + +); diff --git a/frontend/src/pages/Manage/components/FileUpload.jsx b/frontend/src/pages/Manage/components/FileUpload.jsx index a880d3e..d321da7 100644 --- a/frontend/src/pages/Manage/components/FileUpload.jsx +++ b/frontend/src/pages/Manage/components/FileUpload.jsx @@ -6,7 +6,7 @@ import { import { FiFile } from 'react-icons/fi'; import { Manage as i18n } from '../../../global/i18n'; -export const FileUpload = ({ +export default ({ name, placeholder, propagateChange, @@ -46,5 +46,3 @@ export const FileUpload = ({ ); }; - -export default FileUpload; diff --git a/frontend/src/pages/Manage/components/modal/Edit.jsx b/frontend/src/pages/Manage/components/modal/Edit.jsx new file mode 100644 index 0000000..a618555 --- /dev/null +++ b/frontend/src/pages/Manage/components/modal/Edit.jsx @@ -0,0 +1,227 @@ +import React, { useState } from 'react'; +import { + Box, Button, FormControl, FormLabel, Grid, GridItem, Heading, + InputGroup, InputLeftAddon, Text, Textarea, useColorModeValue, +} from '@chakra-ui/react'; +import CurrencyInput from 'react-currency-input-field'; +import { AiOutlineFileText, AiOutlineNumber } from 'react-icons/ai'; +import { BsCurrencyDollar, BsMarkdown } from 'react-icons/bs'; +import InlineLabelInput from '../../../../global/components/FormControl/InlineLabelInput'; +import IconTextDuo from '../../../../global/components/IconTextDuo'; +import { Manage as i18n } from '../../../../global/i18n'; +import store from '../../../../redux/store'; +import FileUpload from '../FileUpload'; +import DeletableImage from '../DeletableImage'; +import { uploadImage } from '../../../../global/api'; + +const InputGroupStyle = { + marginTop: '1rem', + marginBottom: '1rem', +}; + +export default ({ product }) => { + const [loading, setLoading] = useState(false); + const [state, setState] = useState({ + name: product.name, + description: product.description, + quantity: product.quantity, + price: product.price, + images: product.images, + newImages: [], + }); + + const save = async () => { + setLoading(true); + if (!state.name.length || !state.quantity || !state.price || !state.description) { + store.dispatch({ type: 'SET_ALERT', data: { header: 'Missing Info', content: 'Not all required fields have been filled. Please provide this info to proceed.' } }); + setLoading(false); + return; + } + + try { + // Step 1: Upload image files + // Join the old image files with the new ones as well + const imageUrls = (await Promise.all(state.newImages.map(uploadImage))).concat(state.images); + // Step 2: Upload the rest + // eslint-disable-next-line no-underscore-dangle + const resp = await fetch(`/api/v1/products/${product._id}`, { + method: 'PATCH', + headers: { + authorization: `Bearer ${window.localStorage.getItem('auth-token')}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + ...state, + images: imageUrls.length, + }), + }); + + const data = await resp.json(); + if (resp.ok) { + store.dispatch({ type: 'SET_ALERT', data: { header: 'Item Updated Successfully!', content: 'Your Changes have Been Saved and Will Be Reflected on the Management Page' } }); + } else { + 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 Edit Item in Catch Block', content: ex } }); + } + setLoading(false); + }; + + return ( +
+ + {i18n.title} + + + + setState({ ...state, name: e.target.value })} + propKeyPressEvt={(e) => e.key === 'Enter' && save()} + value={state.name} + labelText={i18n.labels.name} + labelIcon={()} + labelBgColor="#fa6663" + labelFgColor="black" + error={!state.name.length ? i18n.labels.required : null} + /> + + {}} + padding="0.5rem" + borderRadius="8px" + margin="auto" + variant="floating" + > +