|
@@ -1,6 +1,6 @@
|
|
|
import {useEffect, useState} from "react";
|
|
|
import {connect} from "react-redux";
|
|
|
-import {Box, Button, Container, Divider, Grid, Paper, Typography, useMediaQuery} from "@mui/material";
|
|
|
+import {Box, Button, CircularProgress, Container, Divider, Grid, Paper, Typography, useMediaQuery} from "@mui/material";
|
|
|
import {actionGoodFindOne} from "../actions/ActionGoodFind";
|
|
|
import Switch from "react-router-dom/es/Switch";
|
|
|
import Route from "react-router-dom/es/Route";
|
|
@@ -16,29 +16,10 @@ import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
|
|
|
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
|
|
|
import Carousel from 'react-material-ui-carousel'
|
|
|
import {backURL} from "../actions/PathDB";
|
|
|
-
|
|
|
-let defProduct = {
|
|
|
- "_id": "5dc45d0b5df9d670df48cc4b",
|
|
|
- "name": "Apple iPhone X 64GB Space Gray",
|
|
|
- "price": 5000,
|
|
|
- "images": [
|
|
|
- {
|
|
|
- "url": "images/00505f5f08ac113874318dee67975aa9",
|
|
|
- "originalFileName": "image"
|
|
|
- },
|
|
|
- {
|
|
|
- "url": "images/00505f5f08ac113874318dee67975aa9",
|
|
|
- "originalFileName": "image"
|
|
|
- },
|
|
|
- {
|
|
|
- "url": "images/00505f5f08ac113874318dee67975aa9",
|
|
|
- "originalFileName": "image"
|
|
|
- }
|
|
|
- ]
|
|
|
-}
|
|
|
+import imgNotFound from "../img/catalog/imgNotFound.png";
|
|
|
|
|
|
export const timeCalc = (createdAt) => {
|
|
|
- let formattedTime = 0;
|
|
|
+ let formattedTime;
|
|
|
let date = new Date(+createdAt);
|
|
|
let year = date.getFullYear();
|
|
|
let month = "0" + (date.getMonth()+1);
|
|
@@ -51,9 +32,9 @@ export const timeCalc = (createdAt) => {
|
|
|
return formattedTime;
|
|
|
}
|
|
|
|
|
|
-const ImageItem = ({images: {url, originalFileName}}) => {
|
|
|
+const ImageItem = ({images}) => {
|
|
|
return (
|
|
|
- <img src={backURL + '/' + url} alt={originalFileName.split('.')[0]}/>
|
|
|
+ <img src={images && images.url ? backURL + '/' + images.url : imgNotFound} alt={images?.originalFileName ? images.originalFileName.split('.')[0] : 'image'}/>
|
|
|
)
|
|
|
}
|
|
|
const CarouselItem = ({images}) => {
|
|
@@ -124,13 +105,19 @@ const ProductTags = ({title, subtitle}) => {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-const AddToCart = ({good, addToCart}) => {
|
|
|
- let [count, setCount] = useState(1)
|
|
|
+const AddToCart = ({cart, good, addToCart}) => {
|
|
|
+ let [count, setCount] = useState(cart[good?._id]?.count || 1)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ setCount(cart[good?._id]?.count || 1)
|
|
|
+ },[cart])
|
|
|
+
|
|
|
+ console.log(count)
|
|
|
return (
|
|
|
<Box width='100%' backgroundColor='#fff' padding='30px'>
|
|
|
<Grid container justifyContent='space-between'>
|
|
|
<Grid xs={5} item>
|
|
|
- <SetCount onCount={value => setCount(value)}/>
|
|
|
+ <SetCount defaultValue={count} onCount={value => setCount(value)}/>
|
|
|
</Grid>
|
|
|
<Grid xs={5} item>
|
|
|
<Button
|
|
@@ -139,14 +126,14 @@ const AddToCart = ({good, addToCart}) => {
|
|
|
color={"inherit"}
|
|
|
onClick={() => addToCart(good, count)}
|
|
|
>
|
|
|
- ADD TO CART
|
|
|
+ {good._id in cart ? 'CHANGE CART' : 'ADD TO CART'}
|
|
|
</Button>
|
|
|
</Grid>
|
|
|
</Grid>
|
|
|
</Box>
|
|
|
)
|
|
|
}
|
|
|
-const CAddToCart = connect(null, {addToCart: actionCardChange})(AddToCart)
|
|
|
+const CAddToCart = connect(state=>({cart: state.cart}), {addToCart: actionCardChange})(AddToCart)
|
|
|
|
|
|
const AddToWishList = ({good={}, wishlist ,onAddToWishList, onWishListRemove}) => {
|
|
|
const flag = good?._id in wishlist
|
|
@@ -174,32 +161,38 @@ const AddToWishList = ({good={}, wishlist ,onAddToWishList, onWishListRemove}) =
|
|
|
}
|
|
|
const CAddToWishList = connect(state => ({wishlist: state.wishlist}), {onAddToWishList: actionWishListAdd, onWishListRemove: actionWishListRemove})(AddToWishList)
|
|
|
|
|
|
-const Goods = ({good:{_id, name, description, price, images, categories, createdAt}=defProduct}) => {
|
|
|
+const Goods = ({good}) => {
|
|
|
const matches = useMediaQuery('(max-width:768px)');
|
|
|
|
|
|
return (
|
|
|
- <Grid container justifyContent='space-around' padding={matches ? "20px 0" : "50px 0"} >
|
|
|
- <Grid xs={12} md={6} item padding='5px 70px 5px 10px'>
|
|
|
- {Array.isArray(images) && images.length > 1 ? <CarouselItem images={images}/> : <Box sx={{width: '100%', display: 'flex', justifyContent: 'center', height: '340px'}}><ImageItem images={images[0]}/></Box>}
|
|
|
- <Divider sx={{margin: '20px 0'}}/>
|
|
|
- <ProductDescription description={description}/>
|
|
|
- </Grid>
|
|
|
- <Grid xs={12} md={6} item padding='5px 110px 5px 10px'>
|
|
|
- <ProductTitle title={name}/>
|
|
|
- <ProductPrice price={price}/>
|
|
|
- <CAddToCart good={{_id, name, price, images}}/>
|
|
|
- <CAddToWishList good={{_id, name, price, images}}/>
|
|
|
- <Box>
|
|
|
- {_id && <ProductTags key={'SKU'} title={'SKU'} subtitle={_id}/>}
|
|
|
- {Array.isArray(categories) &&
|
|
|
- <ProductTags key={'CATEGORY'} title={'CATEGORY'} subtitle={categories.map(item => {
|
|
|
- return <Link key={item?._id} style={{color: "#000", textDecoration: 'none'}} to={`/catalog/category/${item?._id}`}>{item?.name}</Link>
|
|
|
- })}/>
|
|
|
- }
|
|
|
- {createdAt && <ProductTags key={'TIMEOFCREATION'} title={'TIME OF CREATION'} subtitle={timeCalc(createdAt)}/>}
|
|
|
- </Box>
|
|
|
- </Grid>
|
|
|
- </Grid>
|
|
|
+ good && Object.values(good).length > 0 ?
|
|
|
+ <Grid container justifyContent='space-around' padding={matches ? "20px 0" : "50px 0"}>
|
|
|
+ <Grid xs={12} md={6} item padding='5px 70px 5px 10px'>
|
|
|
+ {Array.isArray(good?.images) && good?.images.length > 1 ? <CarouselItem images={good?.images}/> :
|
|
|
+ <Box sx={{width: '100%', display: 'flex', justifyContent: 'center', height: '340px'}}><ImageItem
|
|
|
+ images={good?.images[0]}/></Box>}
|
|
|
+ <Divider sx={{margin: '20px 0'}}/>
|
|
|
+ <ProductDescription description={good?.description}/>
|
|
|
+ </Grid>
|
|
|
+ <Grid xs={12} md={6} item padding='5px 110px 5px 10px'>
|
|
|
+ <ProductTitle title={good?.name}/>
|
|
|
+ <ProductPrice price={good?.price}/>
|
|
|
+ <CAddToCart good={good}/>
|
|
|
+ <CAddToWishList good={good}/>
|
|
|
+ <Box>
|
|
|
+ {good?._id && <ProductTags key={'SKU'} title={'SKU'} subtitle={good?._id}/>}
|
|
|
+ {Array.isArray(good?.categories) &&
|
|
|
+ <ProductTags key={'CATEGORY'} title={'CATEGORY'} subtitle={good?.categories.map(item => {
|
|
|
+ return <Link key={item?._id} style={{color: "#000", textDecoration: 'none'}}
|
|
|
+ to={`/catalog/category/${item?._id}`}>{item?.name}</Link>
|
|
|
+ })}/>
|
|
|
+ }
|
|
|
+ {good?.createdAt && <ProductTags key={'TIMEOFCREATION'} title={'TIME OF CREATION'}
|
|
|
+ subtitle={timeCalc(good?.createdAt)}/>}
|
|
|
+ </Box>
|
|
|
+ </Grid>
|
|
|
+ </Grid>:
|
|
|
+ <Box sx={{height: '100%', width: '100%', display: 'flex', justifyContent:'center', alignItems:'center'}}><CircularProgress color="inherit"/></Box>
|
|
|
)
|
|
|
}
|
|
|
const CGoods = connect(state => ({good: state.promise['goodFindOne']?.payload}))(Goods)
|
|
@@ -211,7 +204,7 @@ const BlockProduct = ({match:{params:{_id}}, getData}) => {
|
|
|
|
|
|
return(
|
|
|
<>
|
|
|
- <main style={{backgroundColor: "#f3f3f3"}}>
|
|
|
+ <main style={{backgroundColor: "#f3f3f3", minHeight:'300px'}}>
|
|
|
<Breadcrumb links={['good']}/>
|
|
|
<Container maxWidth="lg">
|
|
|
<CGoods key={_id} />
|