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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Grid, 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 updateUserHandler = async (values) => {
  14. try {
  15. setEnableBtn(false);
  16. updateUserInfo(values);
  17. await updateUser(values, session.user._id);
  18. } catch (error) {
  19. console.log(error);
  20. setTimeout(() => {
  21. setEnableBtn(true);
  22. }, 3000);
  23. }
  24. };
  25. const mapOrdersToDom = () =>
  26. orders.slice(-4).map((order, i) => (
  27. <OrderCard
  28. key={i}
  29. data={{
  30. date: order.time.split('T')[0],
  31. name: order.shippingAddress.fullName,
  32. totalPrice: order.totalPrice,
  33. }}
  34. ></OrderCard>
  35. ));
  36. return (
  37. <Grid container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
  38. <Grid item xs={12}>
  39. <Typography
  40. variant="h3"
  41. sx={{ pl: 12, mt: 12, height: '100%', color: 'primary.main' }}
  42. >
  43. Welcome to your user account
  44. </Typography>
  45. </Grid>
  46. <Grid item xs={8} sx={{ mt: 4 }}>
  47. <Typography sx={{ pl: 12, fontSize: 20 }}>
  48. Save details for later (user will be logged out)
  49. </Typography>
  50. </Grid>
  51. <Grid item xs={4} sx={{ mt: 4 }}>
  52. <Typography sx={{ fontSize: 20 }}>Previous Orders</Typography>
  53. </Grid>
  54. <Grid item xs={8}>
  55. <ShippingDetailsForm
  56. submitHandler={updateUserHandler}
  57. enableBtn={enableBtn}
  58. ></ShippingDetailsForm>
  59. </Grid>
  60. <Grid item xs={4}>
  61. <Box sx={{ width: '60%', mt: 2 }}>{mapOrdersToDom()}</Box>
  62. </Grid>
  63. </Grid>
  64. );
  65. };
  66. export default ProfileContent;