您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ProfileContent.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, FC } from 'react';
  6. import { updateUser } from '../../requests/user/userUpdateRequest';
  7. import { useUserUpdate } from '../../store/user-context';
  8. import { OrderDataDB } from '../../utils/interface/orderInterface';
  9. import CardContainer from '../cards/card-container/CardContainer';
  10. import OrderCard from '../cards/order-card/OrderCard';
  11. import ShippingDetailsForm from '../forms/shipping-details/ShippingDetailsForm';
  12. import ContentContainer from '../layout/content-wrapper/ContentContainer';
  13. import PageWrapper from '../layout/page-wrapper/PageWrapper';
  14. import StepTitle from '../layout/steps-title/StepTitle';
  15. import Notification from '../notification/Notification';
  16. interface Props {
  17. orders: Array<OrderDataDB>;
  18. }
  19. interface FormValues {
  20. fullName: string;
  21. address: string;
  22. address2: string;
  23. city: string;
  24. country: string;
  25. postcode: string;
  26. }
  27. const ProfileContent: FC<Props> = ({ orders }) => {
  28. const { t } = useTranslation('profile');
  29. const { data: session } = useSession();
  30. const { updateUserInfo } = useUserUpdate();
  31. const [enableBtn, setEnableBtn] = useState(true);
  32. const [open, setOpen] = useState(false);
  33. const updateUserHandler = async (values: FormValues) => {
  34. if (session?.user) {
  35. try {
  36. setEnableBtn(false);
  37. updateUserInfo(values);
  38. await updateUser(values, session.user._id);
  39. setOpen(true);
  40. setTimeout(() => {
  41. setEnableBtn(true);
  42. }, 5000);
  43. } catch (error) {
  44. console.log(error);
  45. setTimeout(() => {
  46. setEnableBtn(true);
  47. }, 3000);
  48. }
  49. }
  50. };
  51. const handleCloseNotification = () => {
  52. setOpen(false);
  53. };
  54. const mapOrdersToDom = () =>
  55. orders.slice(-4).map((order, i) => (
  56. <OrderCard
  57. key={i}
  58. data={{
  59. date: order.time.toLocaleString().split('T')[0],
  60. name: order.shippingAddress.fullName,
  61. totalPrice: order.totalPrice,
  62. }}
  63. ></OrderCard>
  64. ));
  65. return (
  66. <PageWrapper>
  67. <StepTitle title={t('profile:title')} breadcrumbsArray={[]} />
  68. <Notification
  69. open={open}
  70. handleCloseNotification={handleCloseNotification}
  71. notification={t('profile:notification')}
  72. />
  73. <ContentContainer>
  74. <Box flexGrow={1} sx={{ minWidth: '65%' }}>
  75. <Typography sx={{ fontSize: 20, mb: 3 }}>
  76. {t('profile:subtitle1')}
  77. </Typography>
  78. <ShippingDetailsForm
  79. submitHandler={updateUserHandler}
  80. enableBtn={enableBtn}
  81. backBtn={false}
  82. isCheckout={false}
  83. ></ShippingDetailsForm>
  84. </Box>
  85. <Box sx={{ mt: { xs: 5, md: 0 } }}>
  86. <Typography
  87. sx={{ fontSize: 20, mb: { xs: -2, md: 3 }, ml: { md: 3 } }}
  88. >
  89. {t('profile:subtitle2')}
  90. </Typography>
  91. <CardContainer>{mapOrdersToDom()}</CardContainer>
  92. </Box>
  93. </ContentContainer>
  94. </PageWrapper>
  95. );
  96. };
  97. export default ProfileContent;