| mb: { xs: 2, md: 0 }, | mb: { xs: 2, md: 0 }, | ||||
| }} | }} | ||||
| > | > | ||||
| <Image | |||||
| src="/images/coffee-mug.svg" | |||||
| alt="profile" | |||||
| width={200} | |||||
| height={200} | |||||
| /> | |||||
| <Image src={product.image} alt="profile" width={200} height={200} /> | |||||
| </Box> | </Box> | ||||
| <Box | <Box | ||||
| sx={{ | sx={{ |
| }} | }} | ||||
| > | > | ||||
| <Box sx={{ display: 'flex', justifyContent: 'center' }}> | <Box sx={{ display: 'flex', justifyContent: 'center' }}> | ||||
| <Image | |||||
| src="/images/coffee-mug.svg" | |||||
| alt="profile" | |||||
| width={200} | |||||
| height={200} | |||||
| /> | |||||
| <Image src={data.image} alt="profile" width={200} height={200} /> | |||||
| </Box> | </Box> | ||||
| <Box | <Box | ||||
| sx={{ | sx={{ |
| import { Typography } from '@mui/material'; | import { Typography } from '@mui/material'; | ||||
| import { Box } from '@mui/system'; | import { Box } from '@mui/system'; | ||||
| import { destroyCookie } from 'nookies'; | import { destroyCookie } from 'nookies'; | ||||
| import { useEffect } from 'react'; | |||||
| import { useEffect, useState } from 'react'; | |||||
| import { useStore, useStoreUpdate } from '../../store/cart-context'; | import { useStore, useStoreUpdate } from '../../store/cart-context'; | ||||
| import CartCard from '../cards/cart-card/CartCard'; | import CartCard from '../cards/cart-card/CartCard'; | ||||
| import OrderSummaryCard from '../cards/order-summary-card/OrderSummaryCard'; | import OrderSummaryCard from '../cards/order-summary-card/OrderSummaryCard'; | ||||
| const CartContent = () => { | const CartContent = () => { | ||||
| const { cartStorage, totalPrice, totalQuantity } = useStore(); | const { cartStorage, totalPrice, totalQuantity } = useStore(); | ||||
| const { removeCartValue, updateItemQuantity } = useStoreUpdate(); | const { removeCartValue, updateItemQuantity } = useStoreUpdate(); | ||||
| const [cartInfo, setCartInfo] = useState({ | |||||
| cartStorage: [], | |||||
| totalPrice: 0, | |||||
| totalQuantity: 0, | |||||
| }); | |||||
| useEffect(() => { | |||||
| setCartInfo({ | |||||
| cartStorage, | |||||
| totalPrice, | |||||
| totalQuantity, | |||||
| }); | |||||
| }, [cartStorage, totalPrice, totalQuantity]); | |||||
| useEffect(() => { | useEffect(() => { | ||||
| destroyCookie(null, 'checkout-session', { | destroyCookie(null, 'checkout-session', { | ||||
| }, []); | }, []); | ||||
| const mapProductsToDom = () => { | const mapProductsToDom = () => { | ||||
| if (cartStorage?.length) { | |||||
| return cartStorage.map((element, i) => ( | |||||
| if (cartInfo.cartStorage?.length) { | |||||
| return cartInfo.cartStorage.map((element, i) => ( | |||||
| <CartCard | <CartCard | ||||
| key={i} | key={i} | ||||
| product={element?.product} | product={element?.product} | ||||
| <Box sx={{ mt: 2 }}> | <Box sx={{ mt: 2 }}> | ||||
| <OrderSummaryCard | <OrderSummaryCard | ||||
| data={{ totalPrice: totalPrice, totalQuantity: totalQuantity }} | |||||
| data={{ | |||||
| totalPrice: cartInfo.totalPrice, | |||||
| totalQuantity: cartInfo.totalQuantity, | |||||
| }} | |||||
| ></OrderSummaryCard> | ></OrderSummaryCard> | ||||
| </Box> | </Box> | ||||
| </Box> | </Box> |
| import { useSession } from 'next-auth/react'; | import { useSession } from 'next-auth/react'; | ||||
| import { useRouter } from 'next/router'; | import { useRouter } from 'next/router'; | ||||
| import { setCookie } from 'nookies'; | import { setCookie } from 'nookies'; | ||||
| import { useEffect, useState } from 'react'; | |||||
| import { useStore } from '../../store/cart-context'; | import { useStore } from '../../store/cart-context'; | ||||
| import { useCheckoutDataUpdate } from '../../store/checkout-context'; | import { useCheckoutDataUpdate } from '../../store/checkout-context'; | ||||
| import DataCard from '../cards/data-card/DataCard'; | import DataCard from '../cards/data-card/DataCard'; | ||||
| const { cartStorage } = useStore(); | const { cartStorage } = useStore(); | ||||
| const { addCheckoutValue } = useCheckoutDataUpdate(); | const { addCheckoutValue } = useCheckoutDataUpdate(); | ||||
| const [cartData, setCartData] = useState([]); | |||||
| const { data: session } = useSession(); | const { data: session } = useSession(); | ||||
| const router = useRouter(); | const router = useRouter(); | ||||
| useEffect(() => { | |||||
| setCartData(cartStorage); | |||||
| }, [cartStorage]); | |||||
| const submitHandler = (formValues) => { | const submitHandler = (formValues) => { | ||||
| addCheckoutValue( | addCheckoutValue( | ||||
| cartStorage, | |||||
| cartData, | |||||
| { ...formValues, email: session.user.email }, | { ...formValues, email: session.user.email }, | ||||
| session.user._id | session.user._id | ||||
| ); | ); | ||||
| }; | }; | ||||
| const mapProductsToDom = () => { | const mapProductsToDom = () => { | ||||
| return cartStorage?.map((entry, i) => ( | |||||
| return cartData?.map((entry, i) => ( | |||||
| <DataCard | <DataCard | ||||
| key={i} | key={i} | ||||
| data={entry.product} | data={entry.product} |
| export default function MainNav() { | export default function MainNav() { | ||||
| //react useState hook to save the current open/close state of the drawer, normally variables dissapear afte the function was executed | //react useState hook to save the current open/close state of the drawer, normally variables dissapear afte the function was executed | ||||
| const [open, setState] = useState(false); | const [open, setState] = useState(false); | ||||
| const [cartQuantity, setCartQuantity] = useState(0); | |||||
| const matches = useMediaQuery('(min-width: 900px)'); | const matches = useMediaQuery('(min-width: 900px)'); | ||||
| const router = useRouter(); | const router = useRouter(); | ||||
| } | } | ||||
| }, [matches]); | }, [matches]); | ||||
| useEffect(() => { | |||||
| setCartQuantity(totalQuantity); | |||||
| }, [totalQuantity]); | |||||
| return ( | return ( | ||||
| <AppBar | <AppBar | ||||
| position="absolute" | position="absolute" | ||||
| <Toolbar sx={{ width: '100%' }}> | <Toolbar sx={{ width: '100%' }}> | ||||
| <DesktopNav | <DesktopNav | ||||
| router={router} | router={router} | ||||
| totalQuantity={totalQuantity} | |||||
| totalQuantity={cartQuantity} | |||||
| session={session} | session={session} | ||||
| signOutHandler={signOutHandler} | signOutHandler={signOutHandler} | ||||
| /> | /> |
| import useMediaQuery from '@mui/material/useMediaQuery'; | import useMediaQuery from '@mui/material/useMediaQuery'; | ||||
| import { Box } from '@mui/system'; | import { Box } from '@mui/system'; | ||||
| import PropType from 'prop-types'; | import PropType from 'prop-types'; | ||||
| import { useEffect, useState } from 'react'; | |||||
| import { useStore, useStoreUpdate } from '../../../store/cart-context'; | import { useStore, useStoreUpdate } from '../../../store/cart-context'; | ||||
| import ProductImage from './ProductImage'; | import ProductImage from './ProductImage'; | ||||
| import ProductInfo from './ProductInfo'; | import ProductInfo from './ProductInfo'; | ||||
| const { addCartValue } = useStoreUpdate(); | const { addCartValue } = useStoreUpdate(); | ||||
| const { cartStorage } = useStore(); | const { cartStorage } = useStore(); | ||||
| const addProductToCart = (quantity) => addCartValue(product, quantity); | const addProductToCart = (quantity) => addCartValue(product, quantity); | ||||
| const inCart = cartStorage?.some( | |||||
| (item) => item.product.customID === product.customID | |||||
| ) | |||||
| ? true | |||||
| : false; | |||||
| const [inCart, setInCart] = useState(false); | |||||
| useEffect(() => { | |||||
| if (cartStorage) { | |||||
| if ( | |||||
| cartStorage?.some((item) => item.product.customID === product.customID) | |||||
| ) | |||||
| setInCart(true); | |||||
| } | |||||
| }, [cartStorage, product.customID]); | |||||
| return ( | return ( | ||||
| <Box | <Box |
| <Grid item xs={4} sx={{ mt: 4, display: { xs: 'none', md: 'block' } }}> | <Grid item xs={4} sx={{ mt: 4, display: { xs: 'none', md: 'block' } }}> | ||||
| <Typography sx={{ fontSize: 20, ml: 8 }}>Previous Orders</Typography> | <Typography sx={{ fontSize: 20, ml: 8 }}>Previous Orders</Typography> | ||||
| </Grid> | </Grid> | ||||
| <Grid item xs={12} md={8} sx={{ ml: { xs: -8, md: 0 } }}> | |||||
| <Grid | |||||
| item | |||||
| xs={12} | |||||
| md={8} | |||||
| sx={{ ml: { sm: 12, xs: 6 }, mr: { md: -12 }, pr: { sm: 12, xs: 6 } }} | |||||
| > | |||||
| <ShippingDetailsForm | <ShippingDetailsForm | ||||
| submitHandler={updateUserHandler} | submitHandler={updateUserHandler} | ||||
| enableBtn={enableBtn} | enableBtn={enableBtn} | ||||
| <Grid item xs={12} md={4}> | <Grid item xs={12} md={4}> | ||||
| <Box | <Box | ||||
| sx={{ | sx={{ | ||||
| width: '60%', | |||||
| width: { sm: '60%', xs: '55%' }, | |||||
| mt: 2, | mt: 2, | ||||
| ml: { md: 4, xs: 12 }, | |||||
| ml: { xs: 12, md: 4 }, | |||||
| }} | }} | ||||
| > | > | ||||
| {mapOrdersToDom()} | {mapOrdersToDom()} |
| import { useEffect, useState } from 'react'; | import { useEffect, useState } from 'react'; | ||||
| import { postOrder } from '../../requests/products/postOrderRequest'; | import { postOrder } from '../../requests/products/postOrderRequest'; | ||||
| import { useStoreUpdate } from '../../store/cart-context'; | import { useStoreUpdate } from '../../store/cart-context'; | ||||
| import { useCheckoutDataUpdate } from '../../store/checkout-context'; | |||||
| import { | |||||
| useCheckoutData, | |||||
| useCheckoutDataUpdate, | |||||
| } from '../../store/checkout-context'; | |||||
| import StepTitle from '../layout/steps-title/StepTitle'; | import StepTitle from '../layout/steps-title/StepTitle'; | ||||
| let initialRender = true; | let initialRender = true; | ||||
| const ReviewContent = () => { | const ReviewContent = () => { | ||||
| const { checkoutStorage } = useCheckoutData(); | |||||
| const { parseCheckoutValue, clearCheckout } = useCheckoutDataUpdate(); | const { parseCheckoutValue, clearCheckout } = useCheckoutDataUpdate(); | ||||
| const { clearCart } = useStoreUpdate(); | const { clearCart } = useStoreUpdate(); | ||||
| // eslint-disable-next-line no-unused-vars | |||||
| const [orderData, setOrderData] = useState(parseCheckoutValue()); | |||||
| const [orderData, setOrderData] = useState({}); | |||||
| const router = useRouter(); | const router = useRouter(); | ||||
| useEffect(() => { | useEffect(() => { | ||||
| if (initialRender) { | if (initialRender) { | ||||
| postOrder(orderData); | |||||
| setOrderData(parseCheckoutValue()); | |||||
| postOrder(parseCheckoutValue()); | |||||
| initialRender = false; | initialRender = false; | ||||
| return () => { | return () => { | ||||
| clearCheckout(); | clearCheckout(); | ||||
| }); | }); | ||||
| }; | }; | ||||
| } | } | ||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | |||||
| }, []); | |||||
| }, [checkoutStorage]); | |||||
| return ( | return ( | ||||
| <Box sx={{ py: 10, height: '100%', width: '100%' }}> | <Box sx={{ py: 10, height: '100%', width: '100%' }}> | ||||
| <StepTitle | <StepTitle | ||||
| }} | }} | ||||
| > | > | ||||
| <Typography sx={{ fontSize: 18, fontWeight: 600 }}> | <Typography sx={{ fontSize: 18, fontWeight: 600 }}> | ||||
| Total: ${orderData?.totalPrice} | |||||
| Total: ${orderData?.totalPrice.toFixed(2)} | |||||
| </Typography> | </Typography> | ||||
| </Box> | </Box> | ||||
| <Box | <Box |
| import { Box } from '@mui/system'; | import { Box } from '@mui/system'; | ||||
| import { useRouter } from 'next/router'; | import { useRouter } from 'next/router'; | ||||
| import { setCookie } from 'nookies'; | import { setCookie } from 'nookies'; | ||||
| import { useState } from 'react'; | |||||
| import { useEffect, useState } from 'react'; | |||||
| import { | import { | ||||
| useCheckoutData, | useCheckoutData, | ||||
| useCheckoutDataUpdate, | useCheckoutDataUpdate, | ||||
| } from '../../store/checkout-context'; | } from '../../store/checkout-context'; | ||||
| import { stripe } from '../../utils/helpers/stripe'; | import { stripe } from '../../utils/helpers/stripe'; | ||||
| //import DataCardS from '../cards/data-card-shipping/DataCardS'; | |||||
| import DataCard from '../cards/data-card/DataCard'; | import DataCard from '../cards/data-card/DataCard'; | ||||
| import StepTitle from '../layout/steps-title/StepTitle'; | import StepTitle from '../layout/steps-title/StepTitle'; | ||||
| import ButtonGroup from './shipping-btnGroup/ButtonGroup'; | import ButtonGroup from './shipping-btnGroup/ButtonGroup'; | ||||
| const { checkoutStorage } = useCheckoutData(); | const { checkoutStorage } = useCheckoutData(); | ||||
| const { changeContact, changeShippingData } = useCheckoutDataUpdate(); | const { changeContact, changeShippingData } = useCheckoutDataUpdate(); | ||||
| const [open, setOpen] = useState({ isOpen: false, type: '' }); | const [open, setOpen] = useState({ isOpen: false, type: '' }); | ||||
| const [checkoutData, setCheckoutData] = useState({}); | |||||
| const router = useRouter(); | const router = useRouter(); | ||||
| useEffect(() => { | |||||
| setCheckoutData(checkoutStorage); | |||||
| }, [checkoutStorage]); | |||||
| const handleOpen = (type) => setOpen({ isOpen: true, type }); | const handleOpen = (type) => setOpen({ isOpen: true, type }); | ||||
| const handleClose = () => setOpen({ isOpen: false, type: '' }); | const handleClose = () => setOpen({ isOpen: false, type: '' }); | ||||
| handleClose(); | handleClose(); | ||||
| }; | }; | ||||
| console.log(checkoutStorage); | |||||
| const handleStripePayment = () => { | const handleStripePayment = () => { | ||||
| stripe({ | stripe({ | ||||
| lineItems: checkoutStorage.products.map((el) => ({ | |||||
| lineItems: checkoutData.products.map((el) => ({ | |||||
| price: el.product.stripeID, | price: el.product.stripeID, | ||||
| quantity: el.quantity, | quantity: el.quantity, | ||||
| })), | })), | ||||
| userInfo: checkoutStorage.userInfo, | |||||
| userInfo: checkoutData.userInfo, | |||||
| }); | }); | ||||
| setCookie(null, 'review-session', 'active', { | setCookie(null, 'review-session', 'active', { | ||||
| maxAge: 3600, | maxAge: 3600, | ||||
| }; | }; | ||||
| const mapProductsToDom = () => { | const mapProductsToDom = () => { | ||||
| return checkoutStorage?.products?.map((entry, i) => ( | |||||
| return checkoutData?.products?.map((entry, i) => ( | |||||
| <DataCard | <DataCard | ||||
| key={i} | key={i} | ||||
| data={entry.product} | data={entry.product} | ||||
| > | > | ||||
| <Box flexGrow={1} sx={{ minWidth: '65%', ml: { xs: 2, md: 12 } }}> | <Box flexGrow={1} sx={{ minWidth: '65%', ml: { xs: 2, md: 12 } }}> | ||||
| <ShippingData | <ShippingData | ||||
| email={checkoutStorage?.userInfo?.email} | |||||
| address={checkoutStorage?.userInfo?.address} | |||||
| city={checkoutStorage?.userInfo?.city} | |||||
| postcode={checkoutStorage?.userInfo?.postcode} | |||||
| email={checkoutData?.userInfo?.email} | |||||
| address={checkoutData?.userInfo?.address} | |||||
| city={checkoutData?.userInfo?.city} | |||||
| postcode={checkoutData?.userInfo?.postcode} | |||||
| handleOpen={handleOpen} | handleOpen={handleOpen} | ||||
| /> | /> | ||||
| <Box | <Box |