Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ProfileContent.jsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Alert, Grid, 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 OrderCard from '../cards/order-card/OrderCard';
  8. import ShippingDetailsForm from '../forms/shipping-details/ShippingDetailsForm';
  9. const ProfileContent = ({ orders }) => {
  10. const { data: session } = useSession();
  11. const { updateUserInfo } = useUserUpdate();
  12. const [enableBtn, setEnableBtn] = useState(true);
  13. const [open, setOpen] = useState(false);
  14. const updateUserHandler = async (values) => {
  15. try {
  16. setEnableBtn(false);
  17. updateUserInfo(values);
  18. await updateUser(values, session.user._id);
  19. setOpen(true);
  20. setTimeout(() => {
  21. setEnableBtn(true);
  22. }, 5000);
  23. } catch (error) {
  24. console.log(error);
  25. setTimeout(() => {
  26. setEnableBtn(true);
  27. }, 3000);
  28. }
  29. };
  30. const handleCloseNotification = () => {
  31. setOpen(false);
  32. };
  33. const mapOrdersToDom = () =>
  34. orders.slice(-4).map((order, i) => (
  35. <OrderCard
  36. key={i}
  37. data={{
  38. date: order.time.split('T')[0],
  39. name: order.shippingAddress.fullName,
  40. totalPrice: order.totalPrice,
  41. }}
  42. ></OrderCard>
  43. ));
  44. return (
  45. <Grid container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
  46. <Snackbar
  47. anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
  48. open={open}
  49. autoHideDuration={3000}
  50. onClose={handleCloseNotification}
  51. >
  52. <Alert
  53. onClose={handleCloseNotification}
  54. severity="success"
  55. sx={{ width: '100%', backgroundColor: 'green', color: 'white' }}
  56. >
  57. User profile updated!
  58. </Alert>
  59. </Snackbar>
  60. <Grid item xs={12}>
  61. <Typography
  62. variant="h3"
  63. sx={{ pl: 12, mt: 12, height: '100%', color: 'primary.main' }}
  64. >
  65. Welcome to your user account
  66. </Typography>
  67. </Grid>
  68. <Grid item xs={12} md={8} sx={{ mt: 4, mb: { xs: -2, md: 0 } }}>
  69. <Typography sx={{ pl: 12, fontSize: 20 }}>
  70. Save details for later
  71. </Typography>
  72. </Grid>
  73. <Grid item xs={4} sx={{ mt: 4, display: { xs: 'none', md: 'block' } }}>
  74. <Typography sx={{ fontSize: 20, ml: 8 }}>Previous Orders</Typography>
  75. </Grid>
  76. <Grid item xs={12} md={8} sx={{ ml: { xs: -8, md: 0 } }}>
  77. <ShippingDetailsForm
  78. submitHandler={updateUserHandler}
  79. enableBtn={enableBtn}
  80. ></ShippingDetailsForm>
  81. </Grid>
  82. <Grid
  83. item
  84. xs={12}
  85. sx={{
  86. mt: 4,
  87. display: { xs: 'block', md: 'none' },
  88. textAlign: 'center',
  89. }}
  90. >
  91. <Typography sx={{ fontSize: 20, ml: 0 }}>Previous Orders</Typography>
  92. </Grid>
  93. <Grid item xs={12} md={4}>
  94. <Box
  95. sx={{
  96. width: '60%',
  97. mt: 2,
  98. ml: { md: 4, xs: 12 },
  99. }}
  100. >
  101. {mapOrdersToDom()}
  102. </Box>
  103. </Grid>
  104. </Grid>
  105. );
  106. };
  107. export default ProfileContent;