CartPage.jsx 9.4 KB

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