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

ProfileContent.jsx 2.8KB

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