UI tweaks and fixes

This commit is contained in:
oitsjustjose 2022-04-26 21:56:18 -05:00
commit cb889cc826
10 changed files with 94 additions and 96 deletions

View file

@ -24,6 +24,9 @@ const theme = extendTheme({
label: {
...activeLabelStyles,
},
textarea: {
paddingTop: 0,
},
},
'input:not(:placeholder-shown) + label, .chakra-select__wrapper + label':
{

View file

@ -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',
});

View file

@ -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',

View file

@ -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();

View file

@ -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,
}) => (
<Modal isOpen={open} onClose={superOnClose} size={size || 'md'}>
<Modal
isOpen={open}
onClose={superOnClose}
size={size || 'md'}
closeOnOverlayClick={false}
closeOnEsc={false}
>
<ModalOverlay />
<ModalContent>
<ModalHeader>{header}</ModalHeader>
@ -16,12 +21,6 @@ export default ({
<ModalBody>
{children}
</ModalBody>
<ModalFooter>
<Button colorScheme="blue" mr={3} onClick={superOnClose}>
{i18n.close}
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);

View file

@ -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 }) => {
<InlineLabelInput
required
type="text"
propSubmitEvt={save}
propSubmitEvt={() => {}}
propKeyPressEvt={() => {}}
propChangeEvt={(e) => setState({ ...state, name: e.target.value })}
propKeyPressEvt={(e) => e.key === 'Enter' && save()}
value={state.name}
labelText={i18n.labels.name}
labelIcon={(<AiOutlineFileText />)}
@ -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 && (
<FormLabel background="green.400" borderTopLeftRadius="8px" px={2} py={1}>
<IconTextDuo icon={(<BsMarkdown />)} text={i18n.labels.desc} />
</FormLabel>
)}
<Text fontSize=".75rem" color="red.500">{!state.description.length ? i18n.labels.required : null}</Text>
</FormControl>
<FormControl
onSubmit={save}
onKeyPress={(e) => e.key === 'Enter' && save()}
onSubmit={() => {}}
onKeyPress={() => {}}
isRequired
padding=".5rem"
borderRadius="8px"
@ -148,8 +141,8 @@ export default ({ product }) => {
</FormControl>
<FormControl
onSubmit={save}
onKeyPress={(e) => e.key === 'Enter' && save()}
onSubmit={() => {}}
onKeyPress={() => {}}
isRequired
padding=".5rem"
borderRadius="8px"
@ -170,8 +163,8 @@ export default ({ product }) => {
<Text fontSize=".75rem" color="red.500">{!state.price ? i18n.labels.required : null}</Text>
</FormControl>
<FormControl
onSubmit={save}
onKeyPress={(e) => 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}
</Button>

View file

@ -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 () => {
<InlineLabelInput
required
type="text"
propSubmitEvt={save}
propSubmitEvt={() => {}}
propKeyPressEvt={() => {}}
propChangeEvt={(e) => setState({ ...state, name: e.target.value })}
propKeyPressEvt={(e) => e.key === 'Enter' && save()}
value={state.name}
labelText={i18n.labels.name}
labelIcon={(<AiOutlineFileText />)}
@ -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 && (
<FormLabel background="green.400" borderTopLeftRadius="8px" px={2} py={1}>
<IconTextDuo icon={(<BsMarkdown />)} text={i18n.labels.desc} />
</FormLabel>
)}
<Text fontSize=".75rem" color="red.500">{!state.description.length ? i18n.labels.required : null}</Text>
</FormControl>
<FormControl
onSubmit={save}
onKeyPress={(e) => e.key === 'Enter' && save()}
onSubmit={() => {}}
onKeyPress={() => {}}
isRequired
padding=".5rem"
borderRadius="8px"
@ -142,8 +137,8 @@ export default () => {
</FormControl>
<FormControl
onSubmit={save}
onKeyPress={(e) => e.key === 'Enter' && save()}
onSubmit={() => {}}
onKeyPress={() => {}}
isRequired
padding=".5rem"
borderRadius="8px"
@ -163,8 +158,8 @@ export default () => {
<Text fontSize=".75rem" color="red.500">{!state.price ? i18n.labels.required : null}</Text>
</FormControl>
<FormControl
onSubmit={save}
onKeyPress={(e) => 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}
</Button>

View file

@ -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 () => {
<Thead>
<Tr>
<Th>{i18n.labels.name}</Th>
<Th>{i18n.labels.desc}</Th>
<Th>{i18n.labels.price}</Th>
<Th>{i18n.labels.qty}</Th>
<Th>{i18n.labels.hidden}</Th>
<Th>{i18n.labels.rating}</Th>
</Tr>
</Thead>
@ -88,16 +86,8 @@ export default () => {
key={x.name}
>
<Td>{x.name}</Td>
<Td>
{x.description?.substring(0, 24)}
...
</Td>
<Td>
$
{x.price}
</Td>
<Td>{`$${x.price}`}</Td>
<Td>{x.quantity}</Td>
<Td>{x.hidden ? General.yes : General.no}</Td>
<Td>{x.overallRating || 'No Reviews'}</Td>
</Tr>
))}
@ -108,25 +98,27 @@ export default () => {
<DynamicModal
open={newModal}
header={i18n.create.title}
superOnClose={() => {
setNewModal(false);
init(setProducts, setLoaded, setAllProducts);
}}
superOnClose={() => setNewModal(false)}
size="3xl"
>
<New />
<New closeModal={() => {
init(setProducts, setLoaded, setAllProducts).then(() => setNewModal(false));
}}
/>
</DynamicModal>
<DynamicModal
open={!!editItem}
header={i18n.edit.title(editItem?.name)}
superOnClose={() => {
setEditItem(null);
init(setProducts, setLoaded, setAllProducts);
}}
superOnClose={() => setEditItem(null)}
size="3xl"
>
<Edit product={editItem} />
<Edit
product={editItem}
closeModal={() => {
init(setProducts, setLoaded, setAllProducts).then(() => setEditItem(null));
}}
/>
</DynamicModal>
</div>
);

View file

@ -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 <div />;
}
if (!errored && !loaded) {
return (<Loading />);
}
return (
<div style={{ maxWidth: '768px', margin: 'auto' }}>
<Heading textAlign="center" mb={4}>{product.name}</Heading>
{product.images && product.images.length && (
{(product.images && product.images.length) ? (
<Carousel photos={product.images} />
) : (
<Heading textAlign="center" size="md">
(No Photos Available)
</Heading>
)}
<hr style={{ margin: '2rem auto' }} />
<Box my={2} mx="auto" display="block" textAlign="center">
<Heading my={2} size="md">
{/* TODO: currency support */}
@ -62,7 +73,7 @@ export default () => {
</Button>
</Box>
<hr />
<hr style={{ margin: '2rem auto' }} />
<div>
<Heading my={3}>Description</Heading>