CartPage.jsx 9.3 KB

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