Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProfileContent.jsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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
  77. item
  78. xs={12}
  79. md={8}
  80. sx={{ ml: { sm: 12, xs: 6 }, mr: { md: -12 }, pr: { sm: 12, xs: 6 } }}
  81. >
  82. <ShippingDetailsForm
  83. submitHandler={updateUserHandler}
  84. enableBtn={enableBtn}
  85. ></ShippingDetailsForm>
  86. </Grid>
  87. <Grid
  88. item
  89. xs={12}
  90. sx={{
  91. mt: 4,
  92. display: { xs: 'block', md: 'none' },
  93. textAlign: 'center',
  94. }}
  95. >
  96. <Typography sx={{ fontSize: 20, ml: 0 }}>Previous Orders</Typography>
  97. </Grid>
  98. <Grid item xs={12} md={4}>
  99. <Box
  100. sx={{
  101. width: { sm: '60%', xs: '55%' },
  102. mt: 2,
  103. ml: { xs: 12, md: 4 },
  104. }}
  105. >
  106. {mapOrdersToDom()}
  107. </Box>
  108. </Grid>
  109. </Grid>
  110. );
  111. };
  112. export default ProfileContent;