UI support for products!
This commit is contained in:
parent
013202546b
commit
29f4983a15
18 changed files with 21172 additions and 20 deletions
19
backend/init.js
Normal file
19
backend/init.js
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
const user = require("./dist/app/models/User").default;
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const init = async () => {
|
||||||
|
await mongoose.connect(
|
||||||
|
`mongodb://${process.env.MONGO_URI}/${process.env.MONGO_COLLECTION}`,
|
||||||
|
{ autoIndex: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
await user.updateOne({ email: 'stovallj1995@gmail.com' }, { $set: { admin: true } });
|
||||||
|
|
||||||
|
// const u = new user();
|
||||||
|
// u.email = "stovallj1995@gmail.com";
|
||||||
|
// u.password = u.hashPassword("hotdog123");
|
||||||
|
// u.isEmailValidated = true;
|
||||||
|
// await u.save();
|
||||||
|
};
|
||||||
|
|
||||||
|
init().then(process.exit);
|
||||||
2053
backend/package-lock.json
generated
2053
backend/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,7 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import Products from "../../../models/Product";
|
import Products from "../../../models/Product";
|
||||||
import { LOGGER } from "../../../util/Logger";
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
import { Types } from "mongoose";
|
||||||
|
|
||||||
export default async (req: Request, res: Response) => {
|
export default async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -10,7 +11,7 @@ export default async (req: Request, res: Response) => {
|
||||||
.json({ error: "Request is missing Parameter 'id'" });
|
.json({ error: "Request is missing Parameter 'id'" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const product = await Products.findById(req.params.id);
|
const product = await Products.findById(new Types.ObjectId(req.params.id));
|
||||||
if (!product) {
|
if (!product) {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ export type Review = Document & {
|
||||||
|
|
||||||
export type ProductModel = Document & {
|
export type ProductModel = Document & {
|
||||||
name: string;
|
name: string;
|
||||||
|
description: string;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
price: number;
|
price: number;
|
||||||
images: string[];
|
images: string[];
|
||||||
|
|
@ -43,6 +44,10 @@ const ProductSchema = new Schema<ProductModel>({
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
description: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
quantity: {
|
quantity: {
|
||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
|
|
|
||||||
18870
frontend/package-lock.json
generated
18870
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -13,8 +13,10 @@
|
||||||
"react-dom": "^18.0.0",
|
"react-dom": "^18.0.0",
|
||||||
"react-icons": "^4.3.1",
|
"react-icons": "^4.3.1",
|
||||||
"react-localization": "^1.0.19",
|
"react-localization": "^1.0.19",
|
||||||
|
"react-markdown": "^8.0.3",
|
||||||
"react-phone-input-2": "^2.15.0",
|
"react-phone-input-2": "^2.15.0",
|
||||||
"react-redux": "^7.2.8",
|
"react-redux": "^7.2.8",
|
||||||
|
"react-responsive-carousel": "^3.2.23",
|
||||||
"react-router-dom": "^6.3.0",
|
"react-router-dom": "^6.3.0",
|
||||||
"react-scripts": "5.0.1",
|
"react-scripts": "5.0.1",
|
||||||
"redux": "^4.1.2",
|
"redux": "^4.1.2",
|
||||||
|
|
|
||||||
|
|
@ -60,3 +60,7 @@ body.chakra-ui-dark .chakra-input.currency {
|
||||||
border-top-left-radius: 0;
|
border-top-left-radius: 0;
|
||||||
border-bottom-left-radius: 0;
|
border-bottom-left-radius: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.slider-wrapper.axis-horizontal {
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,47 @@ import { Provider } from 'react-redux';
|
||||||
import Routes from './Routes';
|
import Routes from './Routes';
|
||||||
import store from './redux/store';
|
import store from './redux/store';
|
||||||
|
|
||||||
|
const activeLabelStyles = {
|
||||||
|
transform: 'scale(0.85) translateY(-24px)',
|
||||||
|
background: 'transparent !important',
|
||||||
|
padding: '0 !important',
|
||||||
|
};
|
||||||
|
|
||||||
const theme = extendTheme({
|
const theme = extendTheme({
|
||||||
config: {
|
config: {
|
||||||
initialColorMode: window.localStorage.getItem('color-mode') || 'light',
|
initialColorMode: window.localStorage.getItem('color-mode') || 'light',
|
||||||
useSystemColorMode: false,
|
useSystemColorMode: false,
|
||||||
},
|
},
|
||||||
|
components: {
|
||||||
|
Form: {
|
||||||
|
variants: {
|
||||||
|
floating: {
|
||||||
|
container: {
|
||||||
|
_focusWithin: {
|
||||||
|
label: {
|
||||||
|
...activeLabelStyles,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'input:not(:placeholder-shown) + label, .chakra-select__wrapper + label':
|
||||||
|
{
|
||||||
|
...activeLabelStyles,
|
||||||
|
},
|
||||||
|
label: {
|
||||||
|
top: 0,
|
||||||
|
left: 0,
|
||||||
|
zIndex: 2,
|
||||||
|
position: 'absolute',
|
||||||
|
pointerEvents: 'none',
|
||||||
|
mx: 'var(--chakra-space-2)',
|
||||||
|
// px: 1,
|
||||||
|
my: 'var(--chakra-space-2)',
|
||||||
|
transformOrigin: 'left top',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default () => (
|
export default () => (
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { BrowserRouter, Routes as RouterRoutes, Route } from 'react-router-dom';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import FakeNavItem from './global/components/Nav/FakeNavItem';
|
import FakeNavItem from './global/components/Nav/FakeNavItem';
|
||||||
import Home from './pages/Home/index';
|
import Home from './pages/Home/index';
|
||||||
|
import Product from './pages/Product/index';
|
||||||
import Manage from './pages/Manage/index';
|
import Manage from './pages/Manage/index';
|
||||||
import Login from './pages/User/Login';
|
import Login from './pages/User/Login';
|
||||||
import Register from './pages/User/Register';
|
import Register from './pages/User/Register';
|
||||||
|
|
@ -53,6 +54,14 @@ export const Pages = [
|
||||||
adminOnly: false,
|
adminOnly: false,
|
||||||
hidden: true,
|
hidden: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Product',
|
||||||
|
path: '/product',
|
||||||
|
icon: null,
|
||||||
|
element: (<Product />),
|
||||||
|
adminOnly: false,
|
||||||
|
hidden: true,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const Routes = ({ user }) => (
|
const Routes = ({ user }) => (
|
||||||
|
|
|
||||||
16
frontend/src/global/hooks/useWindowDimensions.js
Normal file
16
frontend/src/global/hooks/useWindowDimensions.js
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
|
||||||
|
const get = () => {
|
||||||
|
const { innerWidth: width, innerHeight: height } = window;
|
||||||
|
return { width, height };
|
||||||
|
};
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const [dim, setDim] = useState(get());
|
||||||
|
useEffect(() => {
|
||||||
|
const resize = () => setDim(get());
|
||||||
|
window.addEventListener('resize', resize);
|
||||||
|
return () => window.removeEventListener('resize', resize);
|
||||||
|
}, []);
|
||||||
|
return dim;
|
||||||
|
};
|
||||||
|
|
@ -23,6 +23,14 @@ export const Home = new LocalizedStrings({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const Product = new LocalizedStrings({
|
||||||
|
en: {
|
||||||
|
downloadFailedAlert: {
|
||||||
|
header: 'Failed to Get Product Details',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const Manage = new LocalizedStrings({
|
export const Manage = new LocalizedStrings({
|
||||||
en: {
|
en: {
|
||||||
create: {
|
create: {
|
||||||
|
|
@ -34,6 +42,7 @@ export const Manage = new LocalizedStrings({
|
||||||
}),
|
}),
|
||||||
labels: {
|
labels: {
|
||||||
name: 'Name',
|
name: 'Name',
|
||||||
|
desc: 'Description (Supports Markdown)',
|
||||||
price: 'Price',
|
price: 'Price',
|
||||||
qty: 'Quantity',
|
qty: 'Quantity',
|
||||||
required: 'This Field is Required',
|
required: 'This Field is Required',
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { createRoot } from 'react-dom/client';
|
||||||
|
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
|
import 'react-responsive-carousel/lib/styles/carousel.min.css';
|
||||||
|
|
||||||
const container = document.getElementById('root');
|
const container = document.getElementById('root');
|
||||||
const root = createRoot(container);
|
const root = createRoot(container);
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* eslint-disable no-underscore-dangle */
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
|
|
@ -9,10 +10,13 @@ import {
|
||||||
Flex,
|
Flex,
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import { BsStarFill, BsStarHalf } from 'react-icons/bs';
|
import { BsStarFill, BsStarHalf } from 'react-icons/bs';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
export default ({ product }) => {
|
export default ({ product }) => {
|
||||||
const filtered = product.images.filter((x) => !!x);
|
const filtered = product.images.filter((x) => !!x);
|
||||||
const images = filtered.length ? filtered : ['https://dv2ls.com/f/PpMcVb8gN'];
|
const images = filtered.length ? filtered : ['https://dv2ls.com/f/PpMcVb8gN'];
|
||||||
|
const nav = useNavigate();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
margin="auto"
|
margin="auto"
|
||||||
|
|
@ -25,6 +29,7 @@ export default ({ product }) => {
|
||||||
rounded="lg"
|
rounded="lg"
|
||||||
pos="relative"
|
pos="relative"
|
||||||
zIndex={1}
|
zIndex={1}
|
||||||
|
onClick={() => nav(`/product?productId=${product._id}`, { replace: true })}
|
||||||
_hover={{
|
_hover={{
|
||||||
transition: 'all .3s ease',
|
transition: 'all .3s ease',
|
||||||
boxShadow: 'var(--chakra-shadows-2xl)',
|
boxShadow: 'var(--chakra-shadows-2xl)',
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,19 @@ import InlineLabelInput from '../../global/components/FormControl/InlineLabelInp
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const [hasLoaded, setLoaded] = useState(false);
|
const [hasLoaded, setLoaded] = useState(false);
|
||||||
|
const [allProducts, setAllProducts] = useState([]);
|
||||||
const [products, setProducts] = 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(() => {
|
useEffect(() => {
|
||||||
if (hasLoaded) return () => {};
|
if (hasLoaded) return () => {};
|
||||||
|
|
||||||
|
|
@ -22,6 +33,7 @@ export default () => {
|
||||||
const data = await resp.json();
|
const data = await resp.json();
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
setProductsPt(data);
|
setProductsPt(data);
|
||||||
|
setAllProducts(data);
|
||||||
setLoadedPt(true);
|
setLoadedPt(true);
|
||||||
} else {
|
} else {
|
||||||
store.dispatch({ type: 'SET_ALERT', data: { header: i18n.downloadFailedAlert.header, content: JSON.stringify(data.error) } });
|
store.dispatch({ type: 'SET_ALERT', data: { header: i18n.downloadFailedAlert.header, content: JSON.stringify(data.error) } });
|
||||||
|
|
@ -30,7 +42,7 @@ export default () => {
|
||||||
|
|
||||||
_(setProducts, setLoaded);
|
_(setProducts, setLoaded);
|
||||||
return () => {};
|
return () => {};
|
||||||
}, [hasLoaded, setLoaded, setProducts]);
|
}, [hasLoaded, setLoaded, setProducts, setAllProducts]);
|
||||||
|
|
||||||
if (!hasLoaded) {
|
if (!hasLoaded) {
|
||||||
return (<Loading />);
|
return (<Loading />);
|
||||||
|
|
@ -43,7 +55,7 @@ export default () => {
|
||||||
</Heading>
|
</Heading>
|
||||||
<Box maxW="400px" m="auto">
|
<Box maxW="400px" m="auto">
|
||||||
<InlineLabelInput
|
<InlineLabelInput
|
||||||
propChangeEvt={() => {}}
|
propChangeEvt={doSearch}
|
||||||
propKeyPressEvt={() => {}}
|
propKeyPressEvt={() => {}}
|
||||||
propSubmitEvt={() => {}}
|
propSubmitEvt={() => {}}
|
||||||
type="text"
|
type="text"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
Box, Button, FormControl, Heading, InputGroup, InputLeftAddon, Text,
|
Box, Button, FormControl, FormLabel, Heading,
|
||||||
|
InputGroup, InputLeftAddon, Text, Textarea, useColorModeValue,
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import CurrencyInput from 'react-currency-input-field';
|
import CurrencyInput from 'react-currency-input-field';
|
||||||
import { AiOutlineFileText, AiOutlineNumber } from 'react-icons/ai';
|
import { AiOutlineFileText, AiOutlineNumber } from 'react-icons/ai';
|
||||||
|
|
@ -33,6 +34,7 @@ export default () => {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
name: '',
|
name: '',
|
||||||
|
description: '',
|
||||||
quantity: null,
|
quantity: null,
|
||||||
price: null,
|
price: null,
|
||||||
images: [],
|
images: [],
|
||||||
|
|
@ -113,6 +115,31 @@ export default () => {
|
||||||
error={!state.name.length ? i18n.create.labels.required : null}
|
error={!state.name.length ? i18n.create.labels.required : null}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<FormControl
|
||||||
|
onSubmit={() => {}}
|
||||||
|
padding="0.5rem"
|
||||||
|
borderRadius="8px"
|
||||||
|
margin="auto"
|
||||||
|
variant="floating"
|
||||||
|
>
|
||||||
|
<Textarea
|
||||||
|
id="manage-text-area"
|
||||||
|
value={state.description}
|
||||||
|
onChange={(e) => setState({ ...state, description: e.target.value })}
|
||||||
|
background={useColorModeValue('gray.50', 'gray.700')}
|
||||||
|
resize="vertical"
|
||||||
|
size="md"
|
||||||
|
/>
|
||||||
|
|
||||||
|
{state.description.length === 0 && (
|
||||||
|
<FormLabel background="green.400" borderTopLeftRadius="8px" px={2} py={1}>
|
||||||
|
<IconTextDuo icon={(<AiOutlineFileText />)} text={i18n.create.labels.desc} />
|
||||||
|
</FormLabel>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Text fontSize=".75rem" color="red.500">{!state.description.length ? i18n.create.labels.required : null}</Text>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
<FormControl
|
<FormControl
|
||||||
onSubmit={save}
|
onSubmit={save}
|
||||||
onKeyPress={(e) => e.key === 'Enter' && save()}
|
onKeyPress={(e) => e.key === 'Enter' && save()}
|
||||||
|
|
|
||||||
28
frontend/src/pages/Product/components/Carousel.jsx
Normal file
28
frontend/src/pages/Product/components/Carousel.jsx
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import React from 'react';
|
||||||
|
import { Carousel } from 'react-responsive-carousel';
|
||||||
|
import { Flex, Image } from '@chakra-ui/react';
|
||||||
|
import useWindowDimensions from '../../../global/hooks/useWindowDimensions';
|
||||||
|
|
||||||
|
const fwidth = 1024;
|
||||||
|
|
||||||
|
export default ({ photos }) => {
|
||||||
|
const { width } = useWindowDimensions();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Flex justifyContent="center" alignItems="center">
|
||||||
|
<Carousel
|
||||||
|
autoPlay
|
||||||
|
infiniteLoop
|
||||||
|
emulateTouch={width < fwidth}
|
||||||
|
renderThumbs={photos.length > 1 ? (x) => (x) : () => {}}
|
||||||
|
width={width > fwidth ? '768px' : '98vw'}
|
||||||
|
>
|
||||||
|
{photos.map((x) => (
|
||||||
|
<div>
|
||||||
|
<Image key={x} src={x} />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</Carousel>
|
||||||
|
</Flex>
|
||||||
|
);
|
||||||
|
};
|
||||||
76
frontend/src/pages/Product/index.jsx
Normal file
76
frontend/src/pages/Product/index.jsx
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
import React, { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
import { Box, Button, Heading } from '@chakra-ui/react';
|
||||||
|
import ReactMarkdown from 'react-markdown';
|
||||||
|
import Loading from '../../global/components/Loading';
|
||||||
|
import { Product as i18n } from '../../global/i18n';
|
||||||
|
import store from '../../redux/store';
|
||||||
|
import Carousel from './components/Carousel';
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const [product, setProduct] = useState(null);
|
||||||
|
const [loaded, setLoaded] = useState(false);
|
||||||
|
|
||||||
|
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' } });
|
||||||
|
setLoadedPt(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const productId = params.get('productId');
|
||||||
|
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) } });
|
||||||
|
}
|
||||||
|
setLoadedPt(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
_(setProduct, setLoaded);
|
||||||
|
return () => {};
|
||||||
|
}, [setProduct, loaded, setLoaded]);
|
||||||
|
|
||||||
|
if (!loaded) {
|
||||||
|
return (<Loading />);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ maxWidth: '768px', margin: 'auto' }}>
|
||||||
|
<Heading textAlign="center" mb={4}>{product.name}</Heading>
|
||||||
|
{product.images && product.images.length && (
|
||||||
|
<Carousel photos={product.images} />
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Box my={2} mx="auto" display="block" textAlign="center">
|
||||||
|
<Heading my={2} size="md">
|
||||||
|
{/* TODO: currency support */}
|
||||||
|
{/* TODO: Support for shipping costs to be paid and selected by user */}
|
||||||
|
{`$${product.price} (USD)`}
|
||||||
|
</Heading>
|
||||||
|
|
||||||
|
<Button colorScheme="green">
|
||||||
|
Add to Cart
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<Heading my={3}>Description</Heading>
|
||||||
|
<Heading my={3} size="md">{product.name}</Heading>
|
||||||
|
<ReactMarkdown>{product.description || ''}</ReactMarkdown>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr style={{ margin: '2rem auto' }} />
|
||||||
|
|
||||||
|
<p>{JSON.stringify(product)}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
11
frontend/workspace.code-workspace
Normal file
11
frontend/workspace.code-workspace
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "../backend"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue