Use toasts, cleanup code blocks
This commit is contained in:
parent
cb889cc826
commit
de582afe95
16 changed files with 550 additions and 52 deletions
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>
|
||||
);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue