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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Typography } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import { useSession } from 'next-auth/react';
  4. import { useRouter } from 'next/router';
  5. import { setCookie } from 'nookies';
  6. import { useStore } from '../../store/cart-context';
  7. import { useCheckoutDataUpdate } from '../../store/checkout-context';
  8. import DataCard from '../cards/data-card/DataCard';
  9. import ShippingDetailsForm from '../forms/shipping-details/ShippingDetailsForm';
  10. import StepTitle from '../layout/steps-title/StepTitle';
  11. const CheckoutContent = () => {
  12. const { cartStorage } = useStore();
  13. const { addCheckoutValue } = useCheckoutDataUpdate();
  14. const { data: session } = useSession();
  15. const router = useRouter();
  16. const submitHandler = (formValues) => {
  17. addCheckoutValue(
  18. cartStorage,
  19. { ...formValues, email: session.user.email },
  20. session.user._id
  21. );
  22. setCookie(null, 'shipping-session', 'active', {
  23. maxAge: 3600,
  24. expires: new Date(Date.now() + 3600),
  25. path: '/',
  26. });
  27. router.push('/shipping');
  28. };
  29. const mapProductsToDom = () => {
  30. return cartStorage?.map((entry, i) => (
  31. <DataCard
  32. key={i}
  33. data={entry.product}
  34. quantity={entry.quantity}
  35. ></DataCard>
  36. ));
  37. };
  38. return (
  39. <Box container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
  40. <StepTitle
  41. title="Items in Your Cart"
  42. breadcrumbsArray={['Cart', 'Checkout']}
  43. />
  44. <Box sx={{ ml: { xs: 2, md: 12 }, my: 2 }}>
  45. <Typography sx={{ fontSize: 20 }}>
  46. The following fields will be used as the shipping details for your
  47. order
  48. </Typography>
  49. </Box>
  50. <Box
  51. sx={{
  52. display: 'flex',
  53. flexDirection: { xs: 'column', lg: 'row' },
  54. mr: { xs: 2, md: 12 },
  55. ml: { xs: 2, md: 12 },
  56. }}
  57. >
  58. <Box flexGrow={1}>
  59. <ShippingDetailsForm
  60. backBtn={true}
  61. isCheckout={true}
  62. submitHandler={submitHandler}
  63. ></ShippingDetailsForm>
  64. </Box>
  65. <Box
  66. sx={{
  67. ml: { lg: 2 },
  68. mt: { xs: 5, md: 5, lg: 2 },
  69. display: 'flex',
  70. flexDirection: { xs: 'column', sm: 'row', lg: 'column' },
  71. justifyContent: { sm: 'space-between', md: 'flex-start' },
  72. flexWrap: 'wrap',
  73. }}
  74. >
  75. {mapProductsToDom()}
  76. </Box>
  77. </Box>
  78. </Box>
  79. );
  80. };
  81. export default CheckoutContent;