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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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={8} sx={{ mt: 4 }}>
  69. <Typography sx={{ pl: 12, fontSize: 20 }}>
  70. Save details for later (user will be logged out)
  71. </Typography>
  72. </Grid>
  73. <Grid item xs={4} sx={{ mt: 4 }}>
  74. <Typography sx={{ fontSize: 20 }}>Previous Orders</Typography>
  75. </Grid>
  76. <Grid item xs={8}>
  77. <ShippingDetailsForm
  78. submitHandler={updateUserHandler}
  79. enableBtn={enableBtn}
  80. ></ShippingDetailsForm>
  81. </Grid>
  82. <Grid item xs={4}>
  83. <Box sx={{ width: '60%', mt: 2 }}>{mapOrdersToDom()}</Box>
  84. </Grid>
  85. </Grid>
  86. );
  87. };
  88. export default ProfileContent;