Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ShippingContent.tsx 3.9KB

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