Use toasts, cleanup code blocks
This commit is contained in:
parent
cb889cc826
commit
de582afe95
16 changed files with 550 additions and 52 deletions
|
|
@ -15,7 +15,7 @@ import { useNavigate } from 'react-router-dom';
|
|||
export default ({ product }) => {
|
||||
const filtered = product.images.filter((x) => x !== null);
|
||||
const images = filtered.length ? filtered : ['https://dv2ls.com/f/PpMcVb8gN'];
|
||||
const nav = useNavigate();
|
||||
const navigate = useNavigate();
|
||||
|
||||
return (
|
||||
<Box
|
||||
|
|
@ -29,7 +29,7 @@ export default ({ product }) => {
|
|||
rounded="lg"
|
||||
pos="relative"
|
||||
zIndex={1}
|
||||
onClick={() => nav(`/product?productId=${product._id}`, { replace: true })}
|
||||
onClick={() => navigate(`/product/${product._id}`, { replace: true })}
|
||||
_hover={{
|
||||
transition: 'all .3s ease',
|
||||
boxShadow: 'var(--chakra-shadows-2xl)',
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import React, { useState } from 'react';
|
||||
import {
|
||||
Box, Button, FormControl, FormLabel, Grid, GridItem, Heading,
|
||||
Box, Button, ButtonGroup, FormControl, FormLabel, Grid, GridItem, Heading,
|
||||
InputGroup, InputLeftAddon, Text, Textarea, useColorModeValue,
|
||||
} from '@chakra-ui/react';
|
||||
import CurrencyInput from 'react-currency-input-field';
|
||||
|
|
@ -13,6 +13,7 @@ import store from '../../../../redux/store';
|
|||
import FileUpload from '../FileUpload';
|
||||
import DeletableImage from '../DeletableImage';
|
||||
import { getAuthHeaders, uploadImage } from '../../../../global/api';
|
||||
import { setToast } from '../../../../global/toast';
|
||||
|
||||
const InputGroupStyle = {
|
||||
marginTop: '1rem',
|
||||
|
|
@ -49,18 +50,32 @@ export default ({ product, closeModal }) => {
|
|||
|
||||
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' } });
|
||||
setToast('Product Updated Successfully!', '', 'success');
|
||||
setLoading(false);
|
||||
return true;
|
||||
}
|
||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Item', content: JSON.stringify(data) } });
|
||||
setToast('Failed to Edit Product', data?.error || '', 'error');
|
||||
} catch (ex) {
|
||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Edit Item in Catch Block', content: ex } });
|
||||
setToast('Failed to Edit Product', ex || '', 'error');
|
||||
}
|
||||
setLoading(false);
|
||||
return false;
|
||||
};
|
||||
|
||||
const del = async () => {
|
||||
// eslint-disable-next-line no-alert
|
||||
if (window.confirm('Are you sure you want to delete this Product?')) {
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
const resp = await fetch(`/api/v1/products/${product._id}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
setToast(`Deleted ${product.name} Successfully`, '', 'info');
|
||||
return resp.ok;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Heading textAlign="center" marginBottom="1rem">
|
||||
|
|
@ -203,17 +218,25 @@ export default ({ product, closeModal }) => {
|
|||
))}
|
||||
</Grid>
|
||||
|
||||
<Button
|
||||
display="block"
|
||||
mx="auto"
|
||||
mt={4}
|
||||
colorScheme="green"
|
||||
isLoading={loading}
|
||||
type="submit"
|
||||
onClick={() => save().then((x) => { if (x) closeModal(); })}
|
||||
>
|
||||
{i18n.labels.saveChanges}
|
||||
</Button>
|
||||
<ButtonGroup textAlign="center" display="block" mx="auto" mt={4}>
|
||||
<Button
|
||||
colorScheme="green"
|
||||
isLoading={loading}
|
||||
type="submit"
|
||||
onClick={() => save().then((x) => { if (x) closeModal(); })}
|
||||
>
|
||||
{i18n.labels.saveChanges}
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
colorScheme="red"
|
||||
type="button"
|
||||
onClick={() => del().then((x) => { if (x) closeModal(); })}
|
||||
>
|
||||
{i18n.labels.delete}
|
||||
</Button>
|
||||
</ButtonGroup>
|
||||
|
||||
</Box>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import { Manage as i18n } from '../../../../global/i18n';
|
|||
import store from '../../../../redux/store';
|
||||
import FileUpload from '../FileUpload';
|
||||
import { getAuthHeaders, uploadImage } from '../../../../global/api';
|
||||
import { setToast } from '../../../../global/toast';
|
||||
|
||||
const InputGroupStyle = {
|
||||
marginTop: '1rem',
|
||||
|
|
@ -46,13 +47,13 @@ export default ({ closeModal }) => {
|
|||
|
||||
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' } });
|
||||
setToast('Added New Product Successfully!', '', 'success');
|
||||
setLoading(false);
|
||||
return true;
|
||||
}
|
||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Product', content: JSON.stringify(data) } });
|
||||
setToast('Failed to Add Product', data?.error || '', 'error');
|
||||
} catch (ex) {
|
||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Failed to Add Product', content: ex } });
|
||||
setToast('Failed to Add Product', ex || '', 'error');
|
||||
}
|
||||
setLoading(false);
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Table, Thead, Tbody, Tr, Th, Td, TableContainer, Box, Button, Flex,
|
||||
Table, Thead, Tbody, Tr, Th, Td, TableContainer, Box, Button, Flex, Heading,
|
||||
} from '@chakra-ui/react';
|
||||
import { BiSearchAlt2 } from 'react-icons/bi';
|
||||
import { BiSearchAlt2, BiPlus } from 'react-icons/bi';
|
||||
import { Manage as i18n } from '../../global/i18n';
|
||||
import Loading from '../../global/components/Loading';
|
||||
import store from '../../redux/store';
|
||||
|
|
@ -11,6 +11,7 @@ import DynamicModal from './components/DynamicModal';
|
|||
import New from './components/modal/New';
|
||||
import Edit from './components/modal/Edit';
|
||||
import search from '../../global/search';
|
||||
import IconTextDuo from '../../global/components/IconTextDuo';
|
||||
|
||||
const init = async (setProductsPt, setLoadedPt, setAllProductsPt) => {
|
||||
const resp = await fetch('/api/v1/products');
|
||||
|
|
@ -45,8 +46,15 @@ export default () => {
|
|||
|
||||
return (
|
||||
<div>
|
||||
<Heading my={3} textAlign="center">
|
||||
OpenCommerce Store
|
||||
</Heading>
|
||||
<Heading size="md" my={3} textAlign="center">
|
||||
Product Management
|
||||
</Heading>
|
||||
|
||||
<Flex justifyContent="center" alignItems="center">
|
||||
<Box maxW="400px" m="auto">
|
||||
<Box>
|
||||
<InlineLabelInput
|
||||
propChangeEvt={(evt) => setProducts(search(evt.target.value, allProducts))}
|
||||
propKeyPressEvt={() => {}}
|
||||
|
|
@ -61,7 +69,7 @@ export default () => {
|
|||
</Box>
|
||||
|
||||
<Button backgroundColor="green.400" onClick={() => setNewModal(true)}>
|
||||
Create
|
||||
<IconTextDuo text="Create" icon={(<BiPlus />)} />
|
||||
</Button>
|
||||
</Flex>
|
||||
|
||||
|
|
|
|||
27
frontend/src/pages/Product/components/Code.jsx
Normal file
27
frontend/src/pages/Product/components/Code.jsx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { Code, useColorMode } from '@chakra-ui/react';
|
||||
import React from 'react';
|
||||
import SyntaxHighlighter from 'react-syntax-highlighter';
|
||||
import atomOneDark from 'react-syntax-highlighter/dist/cjs/styles/hljs/atom-one-dark';
|
||||
import atomOneLight from 'react-syntax-highlighter/dist/cjs/styles/hljs/atom-one-light';
|
||||
|
||||
export default ({
|
||||
inline, className, children, props,
|
||||
}) => {
|
||||
const { colorMode } = useColorMode();
|
||||
const match = /language-(\w+)/.exec(className || '');
|
||||
|
||||
return !inline && match && match.length ? (
|
||||
<SyntaxHighlighter
|
||||
language={match[1]}
|
||||
PreTag="div"
|
||||
style={colorMode === 'dark' ? atomOneDark : atomOneLight}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</SyntaxHighlighter>
|
||||
) : (
|
||||
<Code {...props}>
|
||||
{children}
|
||||
</Code>
|
||||
);
|
||||
};
|
||||
|
|
@ -1,42 +1,41 @@
|
|||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
import {
|
||||
Box, Button, Code, Heading,
|
||||
} from '@chakra-ui/react';
|
||||
import { Box, Button, Heading } from '@chakra-ui/react';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import { MdOutlineArrowBackIosNew } from 'react-icons/md';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import Loading from '../../global/components/Loading';
|
||||
import { Product as i18n } from '../../global/i18n';
|
||||
import store from '../../redux/store';
|
||||
import Carousel from './components/Carousel';
|
||||
import Code from './components/Code';
|
||||
|
||||
export default () => {
|
||||
const [product, setProduct] = useState(null);
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [errored, setErrored] = useState(false);
|
||||
|
||||
const navigate = useNavigate();
|
||||
const params = useParams();
|
||||
|
||||
useEffect(() => {
|
||||
if (loaded) return () => {};
|
||||
|
||||
const _ = async (setProductPt, setLoadedPt) => {
|
||||
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' } });
|
||||
setErrored(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const productId = params.get('productId');
|
||||
const _ = async (setProductPt, setLoadedPt, setErroredPt) => {
|
||||
// const productId = params.get('productId');
|
||||
const productId = params.id;
|
||||
const resp = await fetch(`/api/v1/products/${productId}`);
|
||||
const data = await resp.json();
|
||||
if (resp.ok) {
|
||||
setProductPt(data);
|
||||
} else {
|
||||
store.dispatch({ type: 'SET_ALERT', data: { header: i18n.downloadFailedAlert.header, content: JSON.stringify(data.error) } });
|
||||
setErroredPt(true);
|
||||
}
|
||||
setLoadedPt(true);
|
||||
};
|
||||
|
||||
_(setProduct, setLoaded);
|
||||
_(setProduct, setLoaded, setErrored);
|
||||
return () => {};
|
||||
}, [setProduct, loaded, setLoaded]);
|
||||
|
||||
|
|
@ -50,6 +49,9 @@ export default () => {
|
|||
|
||||
return (
|
||||
<div style={{ maxWidth: '768px', margin: 'auto' }}>
|
||||
<Button colorScheme="purple" onClick={() => navigate('/', { replace: true })}>
|
||||
<MdOutlineArrowBackIosNew />
|
||||
</Button>
|
||||
<Heading textAlign="center" mb={4}>{product.name}</Heading>
|
||||
{(product.images && product.images.length) ? (
|
||||
<Carousel photos={product.images} />
|
||||
|
|
@ -78,14 +80,13 @@ export default () => {
|
|||
<div>
|
||||
<Heading my={3}>Description</Heading>
|
||||
<Heading my={3} size="md">{product.name}</Heading>
|
||||
<ReactMarkdown>{product.description || ''}</ReactMarkdown>
|
||||
<ReactMarkdown
|
||||
components={{ code: Code }}
|
||||
skipHtml
|
||||
>
|
||||
{product.description || ''}
|
||||
</ReactMarkdown>
|
||||
</div>
|
||||
|
||||
<hr style={{ margin: '2rem auto' }} />
|
||||
|
||||
<Code colorScheme="cyan">
|
||||
{JSON.stringify(product, null, 2)}
|
||||
</Code>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import store from '../../redux/store';
|
|||
import StandardInput from '../../global/components/FormControl/StandardInput';
|
||||
import IconTextDuo from '../../global/components/IconTextDuo';
|
||||
import { Login as i18n } from '../../global/i18n';
|
||||
import { setToast } from '../../global/toast';
|
||||
|
||||
export default () => {
|
||||
const [state, setState] = useState({ email: '', password: '' });
|
||||
|
|
@ -28,7 +29,7 @@ export default () => {
|
|||
|
||||
if (resp.ok) {
|
||||
store.dispatch({ type: 'SET_USER_TOKEN', token: data.token });
|
||||
store.dispatch({ type: 'SET_ALERT', data: { header: 'Logged In!', content: '' } });
|
||||
setToast(`Welcome ${data.first}!`, '', 'info');
|
||||
setTimeout(() => store.dispatch({ type: 'SET_ALERT', data: null }), 1000);
|
||||
navigate('/', { replace: true });
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in a new issue