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.

ShippingContent.jsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Checkbox, FormControlLabel, Typography } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import { useRouter } from 'next/router';
  4. import { setCookie } from 'nookies';
  5. import { useEffect, useState } from 'react';
  6. import {
  7. useCheckoutData,
  8. useCheckoutDataUpdate,
  9. } from '../../store/checkout-context';
  10. import { stripe } from '../../utils/helpers/stripe';
  11. import DataCard from '../cards/data-card/DataCard';
  12. import StepTitle from '../layout/steps-title/StepTitle';
  13. import ButtonGroup from './shipping-btnGroup/ButtonGroup';
  14. import ShippingData from './shipping-data/ShippingData';
  15. import ShippingModal from './shipping-modal/ShippingModal';
  16. const ShippingContent = () => {
  17. const { checkoutStorage } = useCheckoutData();
  18. const { changeContact, changeShippingData } = useCheckoutDataUpdate();
  19. const [open, setOpen] = useState({ isOpen: false, type: '' });
  20. const [checkoutData, setCheckoutData] = useState({});
  21. const router = useRouter();
  22. useEffect(() => {
  23. setCheckoutData(checkoutStorage);
  24. }, [checkoutStorage]);
  25. const handleOpen = (type) => setOpen({ isOpen: true, type });
  26. const handleClose = () => setOpen({ isOpen: false, type: '' });
  27. const handleChangeShipping = (values) => {
  28. changeShippingData(values);
  29. handleClose();
  30. };
  31. const handleChangeContact = (values) => {
  32. changeContact(values);
  33. handleClose();
  34. };
  35. const handleStripePayment = () => {
  36. stripe({
  37. lineItems: checkoutData.products.map((el) => ({
  38. price: el.product.stripeID,
  39. quantity: el.quantity,
  40. })),
  41. userInfo: checkoutData.userInfo,
  42. });
  43. setCookie(null, 'review-session', 'active', {
  44. maxAge: 3600,
  45. expires: new Date(Date.now() + 3600),
  46. path: '/',
  47. });
  48. };
  49. const handleBackToCart = () => {
  50. router.replace('/cart');
  51. };
  52. const mapProductsToDom = () => {
  53. return checkoutData?.products?.map((entry, i) => (
  54. <DataCard
  55. key={i}
  56. data={entry.product}
  57. quantity={entry.quantity}
  58. ></DataCard>
  59. ));
  60. };
  61. return (
  62. <Box container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
  63. <StepTitle
  64. title="Shipping"
  65. breadcrumbsArray={['Cart', 'Checkout', 'Shipping']}
  66. />
  67. <Box sx={{ ml: { xs: 2, md: 12 }, my: 2 }}>
  68. <Typography sx={{ fontSize: 20 }}>
  69. The following fields will be used as the shipping details for your
  70. order
  71. </Typography>
  72. </Box>
  73. <Box
  74. sx={{
  75. display: 'flex',
  76. flexDirection: { xs: 'column', lg: 'row' },
  77. mr: { xs: 2, md: 12 },
  78. }}
  79. >
  80. <Box flexGrow={1} sx={{ minWidth: '65%', ml: { xs: 2, md: 12 } }}>
  81. <ShippingData
  82. email={checkoutData?.userInfo?.email}
  83. address={checkoutData?.userInfo?.address}
  84. city={checkoutData?.userInfo?.city}
  85. postcode={checkoutData?.userInfo?.postcode}
  86. handleOpen={handleOpen}
  87. />
  88. <Box
  89. sx={{
  90. display: 'flex',
  91. justifyContent: 'space-between',
  92. backgroundColor: '#f2f2f2',
  93. alignItems: 'center',
  94. mb: 2,
  95. width: { sm: '200px' },
  96. borderRadius: 2,
  97. p: 1,
  98. }}
  99. >
  100. <FormControlLabel
  101. control={<Checkbox checked disabled />}
  102. label="Free Shipping"
  103. sx={{ color: 'black', ml: 2 }}
  104. />
  105. </Box>
  106. <ButtonGroup
  107. handleStripePayment={handleStripePayment}
  108. handleBackToCart={handleBackToCart}
  109. />
  110. </Box>
  111. <Box
  112. sx={{
  113. ml: { xs: 2, md: 12, lg: 2 },
  114. mt: { xs: 5, md: 5, lg: 2 },
  115. display: 'flex',
  116. flexDirection: { xs: 'column', sm: 'row', lg: 'column' },
  117. justifyContent: { sm: 'flex-start' },
  118. flexWrap: 'wrap',
  119. }}
  120. >
  121. {mapProductsToDom()}
  122. </Box>
  123. </Box>
  124. <ShippingModal
  125. open={open}
  126. handleClose={handleClose}
  127. handleChangeShipping={handleChangeShipping}
  128. handleChangeContact={handleChangeContact}
  129. />
  130. </Box>
  131. );
  132. };
  133. export default ShippingContent;