CartPage.jsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {Box, Button, Container, Divider, Grid, Typography, useMediaQuery} from "@mui/material";
  2. import {useEffect, useState} from "react";
  3. import Breadcrumb from "../../components/Breadcrumbs";
  4. import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline";
  5. import {TableLine} from "../../components/TableLine";
  6. import {NotFoundBlock} from "../../components/NotFoundBlock";
  7. import imgUrl from "../../img/not-found/3.png";
  8. import AddShoppingCartIcon from "@mui/icons-material/AddShoppingCart";
  9. import {connect} from "react-redux";
  10. import {actionCardChange, actionCardClear, actionCardRemove} from "../../reducers/CartReducer";
  11. import {ActionFullOrder} from "../../actions/ActionOrder";
  12. import {actionClearPromise} from "../../reducers/PromiseReducer";
  13. import {CartGoodLine} from "./CartGoodLine";
  14. import {CBlockTotal} from "./BlockTotal";
  15. import {AccordionItem} from "../MyOrdersPage/AccordionItem";
  16. import {actionOrderFindOne} from "../../actions/ActionOrderFind";
  17. const CartPage = ({order, cart, finalOrder, onCardChange, onCartClear, onCartRemove, onOrderUpsert, actionClearOrder, onOrderFind}) => {
  18. const matches = useMediaQuery('(max-width:768px)')
  19. const [showDetails, setShowDetails] = useState(false)
  20. let rows = []
  21. for (const key of Object.values(cart)) {
  22. rows.push(key)
  23. }
  24. useEffect(() => {
  25. if (order && Object.entries(order).length > 0) {
  26. actionClearOrder('order')
  27. actionClearOrder('orderFindOne')
  28. }
  29. },[cart])
  30. return (
  31. <>
  32. <Breadcrumb links={['cart']}/>
  33. {Object.values(cart).length > 0 || order ?
  34. <main
  35. style={{
  36. backgroundColor: "#f3f3f3",
  37. padding: matches ? "20px 0" : "50px 0",
  38. minHeight:'300px'
  39. }}
  40. >
  41. <Container maxWidth="lg">
  42. {order && Object.entries(order).length > 0 ?
  43. <Box
  44. display='flex'
  45. minHeight='500px'
  46. height='100%'
  47. flexDirection='column'
  48. alignItems='center'
  49. justifyContent='space-around'
  50. >
  51. {order.error ?
  52. <Typography
  53. variant='h5'
  54. textAlign='center'
  55. marginBottom='20px'
  56. >
  57. Error, try again
  58. </Typography>
  59. :
  60. <>
  61. <Typography
  62. variant='h5'
  63. textAlign='center'
  64. marginBottom='20px'
  65. >
  66. Order successfully completed
  67. </Typography>
  68. <CheckCircleOutlineIcon/>
  69. <Typography
  70. variant='h4'
  71. textAlign='center'
  72. fontFamily='sarif'
  73. letterSpacing='2px'
  74. marginBottom='20px'
  75. sx={{textTransform: 'uppercase'}}
  76. >
  77. Thanks for your order!
  78. </Typography>
  79. <Typography
  80. variant='body1'
  81. textAlign='center'
  82. color="#616161"
  83. marginBottom='20px'
  84. >
  85. Attention! Shipping is paid separately upon receipt of the goods.
  86. </Typography>
  87. <Typography
  88. variant='body1'
  89. textAlign='center'
  90. color="#616161"
  91. marginBottom='20px'
  92. >
  93. Your order number: {order.payload?._id || 1}
  94. </Typography>
  95. <Typography
  96. variant='body1'
  97. textAlign='center'
  98. color="#616161"
  99. marginBottom='20px'
  100. >
  101. For the amount: ${+order.payload?.total || 0}
  102. </Typography>
  103. <Button
  104. variant='outlined'
  105. onClick={() => {setShowDetails(!showDetails); onOrderFind(order.payload?._id)}}
  106. >
  107. Show Details
  108. </Button>
  109. {showDetails && finalOrder?.payload &&
  110. <Box
  111. marginTop='40px'
  112. marginBottom='40px'
  113. width='100%'
  114. >
  115. <AccordionItem key={finalOrder.payload._id} data={finalOrder.payload}/>
  116. </Box>
  117. }
  118. </>
  119. }
  120. </Box>:
  121. <Grid
  122. container
  123. justifyContent='space-between'
  124. >
  125. <Grid item xs={8.5}>
  126. <TableLine
  127. columnName={['PRODUCT', 'QUANTITY', 'REMOVE', 'SUBTOTAL']}
  128. customSizeCol={[6, 3, 2, 1]}
  129. />
  130. <Divider sx={{marginBottom: '20px'}}/>
  131. {rows.map((item, index) =>
  132. <CartGoodLine
  133. key={index}
  134. item={item}
  135. onCartRemove={onCartRemove}
  136. onCardChange={onCardChange}
  137. />)
  138. }
  139. <Divider/>
  140. </Grid>
  141. <Grid item xs={3}
  142. sx={{backgroundColor: '#fff'}}
  143. height='100%'
  144. paddingBottom='20px'
  145. >
  146. <CBlockTotal
  147. cart={cart}
  148. rows={rows}
  149. onCartClear={onCartClear}
  150. onOrderUpsert={onOrderUpsert}
  151. />
  152. </Grid>
  153. </Grid>
  154. }
  155. </Container>
  156. </main>
  157. :
  158. <NotFoundBlock
  159. img={imgUrl}
  160. headerText={'YOUR CART IS CURRENTLY EMPTY'}
  161. text={
  162. <Box display='flex' alignItems='center'>
  163. <Typography component='span'>Click the</Typography>
  164. <AddShoppingCartIcon sx={{margin: '0 10px'}}/>
  165. <Typography component='span'>icons to add products</Typography>
  166. </Box>
  167. }
  168. />
  169. }
  170. </>
  171. )
  172. }
  173. export const CCartPage = connect(state => ({cart: state.cart, order: state.promise?.order,
  174. finalOrder: state.promise['orderFindOne']}),
  175. {onCardChange: actionCardChange, onCartClear: actionCardClear, onCartRemove: actionCardRemove,
  176. onOrderUpsert: ActionFullOrder, actionClearOrder: actionClearPromise, onOrderFind: actionOrderFindOne})(CartPage)