diff --git a/backend/src/app/controllers/product/crud/update.ts b/backend/src/app/controllers/product/crud/update.ts index ad40620..b14d3b3 100644 --- a/backend/src/app/controllers/product/crud/update.ts +++ b/backend/src/app/controllers/product/crud/update.ts @@ -22,11 +22,12 @@ export default async (req: Request, res: Response) => { .json({ error: `No such product with ID ${req.params.id} found` }); } - 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; + req.body.name && (product.name = req.body.name); + req.body.description && (product.description = req.body.description); + req.body.quantity && (product.quantity = req.body.quantity); + req.body.price && (product.price = req.body.price); + req.body.images && (product.images = req.body.images); + req.body.hidden && (product.hidden = req.body.hidden); await product.save(); return res.status(200).json(product); diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 1e28561..4df0964 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -24,6 +24,9 @@ const theme = extendTheme({ label: { ...activeLabelStyles, }, + textarea: { + paddingTop: 0, + }, }, 'input:not(:placeholder-shown) + label, .chakra-select__wrapper + label': { diff --git a/frontend/src/global/api.js b/frontend/src/global/api.js index 9ab882c..2b062d4 100644 --- a/frontend/src/global/api.js +++ b/frontend/src/global/api.js @@ -11,4 +11,7 @@ export const uploadImage = async (file) => { return `/api/v1/fileupload/${output}`; }; -export default uploadImage; +export const getAuthHeaders = () => ({ + authorization: `Bearer ${window.localStorage.getItem('auth-token')}`, + 'Content-Type': 'application/json', +}); diff --git a/frontend/src/global/i18n.js b/frontend/src/global/i18n.js index f2335f1..890534d 100644 --- a/frontend/src/global/i18n.js +++ b/frontend/src/global/i18n.js @@ -5,6 +5,7 @@ export const General = new LocalizedStrings({ yes: 'Yes', no: 'No', close: 'Close', + cancel: 'Cancel', save: 'Save', openMenu: 'Open Menu', login: 'Log In', diff --git a/frontend/src/pages/Home/components/Card.jsx b/frontend/src/pages/Home/components/Card.jsx index 6be58d5..7af9c73 100644 --- a/frontend/src/pages/Home/components/Card.jsx +++ b/frontend/src/pages/Home/components/Card.jsx @@ -13,7 +13,7 @@ import { BsStarFill, BsStarHalf } from 'react-icons/bs'; import { useNavigate } from 'react-router-dom'; export default ({ product }) => { - const filtered = product.images.filter((x) => !!x); + const filtered = product.images.filter((x) => x !== null); const images = filtered.length ? filtered : ['https://dv2ls.com/f/PpMcVb8gN']; const nav = useNavigate(); diff --git a/frontend/src/pages/Manage/components/DynamicModal.jsx b/frontend/src/pages/Manage/components/DynamicModal.jsx index 36038cb..bffd83c 100644 --- a/frontend/src/pages/Manage/components/DynamicModal.jsx +++ b/frontend/src/pages/Manage/components/DynamicModal.jsx @@ -1,14 +1,19 @@ import React from 'react'; import { - Button, Modal, ModalBody, - ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, + Modal, ModalBody, + ModalCloseButton, ModalContent, ModalHeader, ModalOverlay, } from '@chakra-ui/react'; -import { General as i18n } from '../../../global/i18n'; export default ({ open, header, superOnClose, size, children, }) => ( - + {header} @@ -16,12 +21,6 @@ export default ({ {children} - - - - ); diff --git a/frontend/src/pages/Manage/components/modal/Edit.jsx b/frontend/src/pages/Manage/components/modal/Edit.jsx index a618555..a640556 100644 --- a/frontend/src/pages/Manage/components/modal/Edit.jsx +++ b/frontend/src/pages/Manage/components/modal/Edit.jsx @@ -12,14 +12,14 @@ 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'; +import { getAuthHeaders, uploadImage } from '../../../../global/api'; const InputGroupStyle = { marginTop: '1rem', marginBottom: '1rem', }; -export default ({ product }) => { +export default ({ product, closeModal }) => { const [loading, setLoading] = useState(false); const [state, setState] = useState({ name: product.name, @@ -35,37 +35,30 @@ export default ({ product }) => { 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; + return false; } 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, - }), + headers: getAuthHeaders(), + body: JSON.stringify({ ...state, images: imageUrls }), }); 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) } }); + setLoading(false); + return true; } + 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 false; }; return ( @@ -85,9 +78,9 @@ export default ({ product }) => { {}} + propKeyPressEvt={() => {}} propChangeEvt={(e) => setState({ ...state, name: e.target.value })} - propKeyPressEvt={(e) => e.key === 'Enter' && save()} value={state.name} labelText={i18n.labels.name} labelIcon={()} @@ -110,20 +103,20 @@ export default ({ product }) => { background={useColorModeValue('gray.50', 'gray.700')} resize="vertical" size="md" + paddingTop="32px" + transition="padding ease-in-out 150ms" /> - {state.description.length === 0 && ( )} text={i18n.labels.desc} /> - )} {!state.description.length ? i18n.labels.required : null} e.key === 'Enter' && save()} + onSubmit={() => {}} + onKeyPress={() => {}} isRequired padding=".5rem" borderRadius="8px" @@ -148,8 +141,8 @@ export default ({ product }) => { e.key === 'Enter' && save()} + onSubmit={() => {}} + onKeyPress={() => {}} isRequired padding=".5rem" borderRadius="8px" @@ -170,8 +163,8 @@ export default ({ product }) => { {!state.price ? i18n.labels.required : null} e.key === 'Enter' && save()} + onSubmit={() => {}} + onKeyPress={() => {}} isRequired padding=".5rem" borderRadius="8px" @@ -217,7 +210,7 @@ export default ({ product }) => { colorScheme="green" isLoading={loading} type="submit" - onClick={save} + onClick={() => save().then((x) => { if (x) closeModal(); })} > {i18n.labels.saveChanges} diff --git a/frontend/src/pages/Manage/components/modal/New.jsx b/frontend/src/pages/Manage/components/modal/New.jsx index 1fd2a83..4d86f8a 100644 --- a/frontend/src/pages/Manage/components/modal/New.jsx +++ b/frontend/src/pages/Manage/components/modal/New.jsx @@ -11,14 +11,14 @@ import IconTextDuo from '../../../../global/components/IconTextDuo'; import { Manage as i18n } from '../../../../global/i18n'; import store from '../../../../redux/store'; import FileUpload from '../FileUpload'; -import { uploadImage } from '../../../../global/api'; +import { getAuthHeaders, uploadImage } from '../../../../global/api'; const InputGroupStyle = { marginTop: '1rem', marginBottom: '1rem', }; -export default () => { +export default ({ closeModal }) => { const [loading, setLoading] = useState(false); const [state, setState] = useState({ name: '', @@ -33,34 +33,29 @@ export default () => { 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; + return false; } + try { - // Step 1: Upload image files const imageUrls = await Promise.all(state.images.map(uploadImage)); - // Step 2: Upload the rest const resp = await fetch('/api/v1/products', { method: 'PUT', - headers: { - authorization: `Bearer ${window.localStorage.getItem('auth-token')}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - ...state, - images: imageUrls.length ? imageUrls : ['https://dv2ls.com/f/PpMcVb8gN'], - }), + headers: getAuthHeaders(), + body: JSON.stringify({ ...state, images: imageUrls }), }); const data = await resp.json(); if (resp.ok) { store.dispatch({ type: 'SET_ALERT', data: { header: 'Added New Product!', content: 'Your New Product has Been Saved and Will Be Reflected on the Management Page' } }); - } else { - store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Product', content: JSON.stringify(data) } }); + setLoading(false); + return true; } + store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Product', content: JSON.stringify(data) } }); } catch (ex) { store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Product', content: ex } }); } setLoading(false); + return false; }; return ( @@ -80,9 +75,9 @@ export default () => { {}} + propKeyPressEvt={() => {}} propChangeEvt={(e) => setState({ ...state, name: e.target.value })} - propKeyPressEvt={(e) => e.key === 'Enter' && save()} value={state.name} labelText={i18n.labels.name} labelIcon={()} @@ -105,20 +100,20 @@ export default () => { background={useColorModeValue('gray.50', 'gray.700')} resize="vertical" size="md" + paddingTop="32px" + transition="padding ease-in-out 150ms" /> - {state.description.length === 0 && ( )} text={i18n.labels.desc} /> - )} {!state.description.length ? i18n.labels.required : null} e.key === 'Enter' && save()} + onSubmit={() => {}} + onKeyPress={() => {}} isRequired padding=".5rem" borderRadius="8px" @@ -142,8 +137,8 @@ export default () => { e.key === 'Enter' && save()} + onSubmit={() => {}} + onKeyPress={() => {}} isRequired padding=".5rem" borderRadius="8px" @@ -163,8 +158,8 @@ export default () => { {!state.price ? i18n.labels.required : null} e.key === 'Enter' && save()} + onSubmit={() => {}} + onKeyPress={() => {}} isRequired padding=".5rem" borderRadius="8px" @@ -191,7 +186,7 @@ export default () => { colorScheme="green" isLoading={loading} type="submit" - onClick={save} + onClick={() => save().then((x) => { if (x) closeModal(); })} > {i18n.labels.save} diff --git a/frontend/src/pages/Manage/index.jsx b/frontend/src/pages/Manage/index.jsx index d6ac337..7e61fd2 100644 --- a/frontend/src/pages/Manage/index.jsx +++ b/frontend/src/pages/Manage/index.jsx @@ -3,7 +3,7 @@ import { Table, Thead, Tbody, Tr, Th, Td, TableContainer, Box, Button, Flex, } from '@chakra-ui/react'; import { BiSearchAlt2 } from 'react-icons/bi'; -import { Manage as i18n, General } from '../../global/i18n'; +import { Manage as i18n } from '../../global/i18n'; import Loading from '../../global/components/Loading'; import store from '../../redux/store'; import InlineLabelInput from '../../global/components/FormControl/InlineLabelInput'; @@ -72,10 +72,8 @@ export default () => { {i18n.labels.name} - {i18n.labels.desc} {i18n.labels.price} {i18n.labels.qty} - {i18n.labels.hidden} {i18n.labels.rating} @@ -88,16 +86,8 @@ export default () => { key={x.name} > {x.name} - - {x.description?.substring(0, 24)} - ... - - - $ - {x.price} - + {`$${x.price}`} {x.quantity} - {x.hidden ? General.yes : General.no} {x.overallRating || 'No Reviews'} ))} @@ -108,25 +98,27 @@ export default () => { { - setNewModal(false); - init(setProducts, setLoaded, setAllProducts); - }} + superOnClose={() => setNewModal(false)} size="3xl" > - + { + init(setProducts, setLoaded, setAllProducts).then(() => setNewModal(false)); + }} + /> { - setEditItem(null); - init(setProducts, setLoaded, setAllProducts); - }} + superOnClose={() => setEditItem(null)} size="3xl" > - + { + init(setProducts, setLoaded, setAllProducts).then(() => setEditItem(null)); + }} + /> ); diff --git a/frontend/src/pages/Product/index.jsx b/frontend/src/pages/Product/index.jsx index 2121676..6148dc1 100644 --- a/frontend/src/pages/Product/index.jsx +++ b/frontend/src/pages/Product/index.jsx @@ -12,6 +12,7 @@ import Carousel from './components/Carousel'; export default () => { const [product, setProduct] = useState(null); const [loaded, setLoaded] = useState(false); + const [errored, setErrored] = useState(false); useEffect(() => { if (loaded) return () => {}; @@ -20,7 +21,7 @@ export default () => { const params = new URLSearchParams(window.location.search); if (!params.has('productId')) { store.dispatch({ type: 'SET_ALERT', data: { header: i18n.downloadFailedAlert.header, content: 'URL is invalid as it\'s missing the Product ID' } }); - setLoadedPt(true); + setErrored(true); return; } @@ -39,17 +40,27 @@ export default () => { return () => {}; }, [setProduct, loaded, setLoaded]); - if (!loaded) { + if (errored) { + return
; + } + + if (!errored && !loaded) { return (); } return (
{product.name} - {product.images && product.images.length && ( + {(product.images && product.images.length) ? ( + ) : ( + + (No Photos Available) + )} +
+ {/* TODO: currency support */} @@ -62,7 +73,7 @@ export default () => { -
+
Description