You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CheckoutContent.jsx 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { Box } from '@mui/system';
  2. import { useSession } from 'next-auth/react';
  3. import { useRouter } from 'next/router';
  4. import { setCookie } from 'nookies';
  5. import { useEffect, useState } from 'react';
  6. import { useStore } from '../../store/cart-context';
  7. import { useCheckoutDataUpdate } from '../../store/checkout-context';
  8. import CardContainer from '../cards/card-container/CardContainer';
  9. import DataCard from '../cards/data-card/DataCard';
  10. import ShippingDetailsForm from '../forms/shipping-details/ShippingDetailsForm';
  11. import ContentContainer from '../layout/content-wrapper/ContentContainer';
  12. import PageWrapper from '../layout/page-wrapper/PageWrapper';
  13. import StepTitle from '../layout/steps-title/StepTitle';
  14. import PageDescription from '../page-description/PageDescription';
  15. const CheckoutContent = () => {
  16. const { cartStorage } = useStore();
  17. const { addCheckoutValue } = useCheckoutDataUpdate();
  18. const [cartData, setCartData] = useState([]);
  19. const { data: session } = useSession();
  20. const router = useRouter();
  21. useEffect(() => {
  22. setCartData(cartStorage);
  23. }, [cartStorage]);
  24. const submitHandler = (formValues) => {
  25. addCheckoutValue(
  26. cartData,
  27. { ...formValues, email: session.user.email },
  28. session.user._id
  29. );
  30. setCookie(null, 'shipping-session', 'active', {
  31. maxAge: 3600,
  32. expires: new Date(Date.now() + 3600),
  33. path: '/',
  34. });
  35. router.push('/shipping');
  36. };
  37. const mapProductsToDom = () => {
  38. return cartData?.map((entry, i) => (
  39. <DataCard
  40. key={i}
  41. data={entry.product}
  42. quantity={entry.quantity}
  43. ></DataCard>
  44. ));
  45. };
  46. return (
  47. <PageWrapper>
  48. <StepTitle
  49. title="Items in Your Cart"
  50. breadcrumbsArray={['Cart', 'Checkout']}
  51. />
  52. <PageDescription
  53. description="The following fields will be used as the shipping details for your
  54. order"
  55. />
  56. <ContentContainer>
  57. <Box flexGrow={1} sx={{ minWidth: '65%' }}>
  58. <ShippingDetailsForm
  59. backBtn={true}
  60. isCheckout={true}
  61. submitHandler={submitHandler}
  62. ></ShippingDetailsForm>
  63. </Box>
  64. <CardContainer>{mapProductsToDom()}</CardContainer>
  65. </ContentContainer>
  66. </PageWrapper>
  67. );
  68. };
  69. export default CheckoutContent;