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.tsx 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { 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. import Notification from '../notification/Notification';
  15. const ProfileContent = ({ orders }) => {
  16. const { t } = useTranslation('profile');
  17. const { data: session } = useSession();
  18. const { updateUserInfo } = useUserUpdate();
  19. const [enableBtn, setEnableBtn] = useState(true);
  20. const [open, setOpen] = useState(false);
  21. const updateUserHandler = async (values) => {
  22. try {
  23. setEnableBtn(false);
  24. updateUserInfo(values);
  25. await updateUser(values, session.user._id);
  26. setOpen(true);
  27. setTimeout(() => {
  28. setEnableBtn(true);
  29. }, 5000);
  30. } catch (error) {
  31. console.log(error);
  32. setTimeout(() => {
  33. setEnableBtn(true);
  34. }, 3000);
  35. }
  36. };
  37. const handleCloseNotification = () => {
  38. setOpen(false);
  39. };
  40. const mapOrdersToDom = () =>
  41. orders.slice(-4).map((order, i) => (
  42. <OrderCard
  43. key={i}
  44. data={{
  45. date: order.time.split('T')[0],
  46. name: order.shippingAddress.fullName,
  47. totalPrice: order.totalPrice,
  48. }}
  49. ></OrderCard>
  50. ));
  51. return (
  52. <PageWrapper>
  53. <StepTitle title={t('profile:title')} />
  54. <Notification
  55. open={open}
  56. handleCloseNotification={handleCloseNotification}
  57. notification={t('profile:notification')}
  58. />
  59. <ContentContainer>
  60. <Box flexGrow={1} sx={{ minWidth: '65%' }}>
  61. <Typography sx={{ fontSize: 20, mb: 3 }}>
  62. {t('profile:subtitle1')}
  63. </Typography>
  64. <ShippingDetailsForm
  65. submitHandler={updateUserHandler}
  66. enableBtn={enableBtn}
  67. ></ShippingDetailsForm>
  68. </Box>
  69. <Box sx={{ mt: { xs: 5, md: 0 } }}>
  70. <Typography
  71. sx={{ fontSize: 20, mb: { xs: -2, md: 3 }, ml: { md: 3 } }}
  72. >
  73. {t('profile:subtitle2')}
  74. </Typography>
  75. <CardContainer>{mapOrdersToDom()}</CardContainer>
  76. </Box>
  77. </ContentContainer>
  78. </PageWrapper>
  79. );
  80. };
  81. export default ProfileContent;