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.

ProfileContent.jsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Alert, Snackbar, Typography } from '@mui/material';
  2. import { Box } from '@mui/system';
  3. import { useSession } from 'next-auth/react';
  4. import { useTranslation } from 'next-i18next';
  5. import { useState } from 'react';
  6. import { updateUser } from '../../requests/user/userUpdateRequest';
  7. import { useUserUpdate } from '../../store/user-context';
  8. import CardContainer from '../cards/card-container/CardContainer';
  9. import OrderCard from '../cards/order-card/OrderCard';
  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. const ProfileContent = ({ orders }) => {
  15. const { t } = useTranslation('profile');
  16. const { data: session } = useSession();
  17. const { updateUserInfo } = useUserUpdate();
  18. const [enableBtn, setEnableBtn] = useState(true);
  19. const [open, setOpen] = useState(false);
  20. const updateUserHandler = async (values) => {
  21. try {
  22. setEnableBtn(false);
  23. updateUserInfo(values);
  24. await updateUser(values, session.user._id);
  25. setOpen(true);
  26. setTimeout(() => {
  27. setEnableBtn(true);
  28. }, 5000);
  29. } catch (error) {
  30. console.log(error);
  31. setTimeout(() => {
  32. setEnableBtn(true);
  33. }, 3000);
  34. }
  35. };
  36. const handleCloseNotification = () => {
  37. setOpen(false);
  38. };
  39. const mapOrdersToDom = () =>
  40. orders.slice(-4).map((order, i) => (
  41. <OrderCard
  42. key={i}
  43. data={{
  44. date: order.time.split('T')[0],
  45. name: order.shippingAddress.fullName,
  46. totalPrice: order.totalPrice,
  47. }}
  48. ></OrderCard>
  49. ));
  50. return (
  51. <PageWrapper>
  52. <StepTitle title={t('profile:title')} />
  53. <Snackbar
  54. anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
  55. open={open}
  56. autoHideDuration={3000}
  57. onClose={handleCloseNotification}
  58. >
  59. <Alert
  60. onClose={handleCloseNotification}
  61. severity="success"
  62. sx={{ width: '100%', backgroundColor: 'green', color: 'white' }}
  63. >
  64. {t('profile:notification')}
  65. </Alert>
  66. </Snackbar>
  67. <ContentContainer>
  68. <Box flexGrow={1} sx={{ minWidth: '65%' }}>
  69. <Typography sx={{ fontSize: 20, mb: 3 }}>
  70. {t('profile:subtitle1')}
  71. </Typography>
  72. <ShippingDetailsForm
  73. submitHandler={updateUserHandler}
  74. enableBtn={enableBtn}
  75. ></ShippingDetailsForm>
  76. </Box>
  77. <Box sx={{ mt: { xs: 5, md: 0 } }}>
  78. <Typography
  79. sx={{ fontSize: 20, mb: { xs: -2, md: 3 }, ml: { md: 3 } }}
  80. >
  81. {t('profile:subtitle2')}
  82. </Typography>
  83. <CardContainer>{mapOrdersToDom()}</CardContainer>
  84. </Box>
  85. </ContentContainer>
  86. </PageWrapper>
  87. );
  88. };
  89. export default ProfileContent;