MainOrders.jsx 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {useEffect, useState} from "react";
  2. import {Box, CircularProgress, Divider, Pagination, Typography} from "@mui/material";
  3. import {NotFoundBlock} from "../../components/NotFoundBlock";
  4. import {connect} from "react-redux";
  5. import {actionFullOrderFind, actionOrderCount} from "../../actions/ActionOrderFind";
  6. import {actionMyOrderClear} from "../../reducers/MyOrdersReducer";
  7. import {AccordionItem} from "./AccordionItem";
  8. const MainOrders = ({itemsPerPage=100,
  9. orders,
  10. ordersPreload,
  11. orderCount,
  12. onFindOrders,
  13. onOrdersClear,
  14. onGetCountOrder}) => {
  15. const [page, setPage] = useState(1)
  16. const [count, setCount] = useState(1)
  17. const handleChange = (event, value) => {
  18. onOrdersClear()
  19. setPage(value);
  20. onFindOrders((itemsPerPage * value)-itemsPerPage, itemsPerPage);
  21. }
  22. useEffect(() => {
  23. if (Object.entries(orders).length === 0 && page === 1) onFindOrders(null, itemsPerPage)
  24. if (!orderCount) onGetCountOrder()
  25. else {
  26. setCount(Math.ceil(+orderCount?.payload / itemsPerPage))
  27. }
  28. }, [orders, orderCount])
  29. return (
  30. <>
  31. {ordersPreload?.status === "PENDING" || Object.entries(orders).length === 0 ?
  32. <Box
  33. sx={{
  34. height: '100%',
  35. width: '100%',
  36. display: 'flex',
  37. justifyContent:'center',
  38. alignItems:'center'
  39. }}
  40. >
  41. <CircularProgress color="inherit"/>
  42. </Box>
  43. :
  44. Object.entries(orders?.orderResult).length > 0 ?
  45. <Box>
  46. {orderCount?.payload &&
  47. <Typography
  48. variant='h5'
  49. marginBottom='30px'
  50. letterSpacing='3px'
  51. >
  52. Total orders: {orderCount.payload || 0}
  53. </Typography>
  54. }
  55. {Object.values(orders.orderResult)
  56. .map(item => <AccordionItem key={item._id} data={item}/>)
  57. }
  58. <Divider sx={{margin: '20px'}}/>
  59. <Box
  60. display='flex'
  61. justifyContent='center'
  62. width='100%'
  63. >
  64. <Pagination
  65. count={count}
  66. page={page}
  67. onChange={handleChange}
  68. defaultPage={1}
  69. color="primary"
  70. size="large"
  71. showFirstButton
  72. showLastButton
  73. />
  74. </Box>
  75. </Box>
  76. :
  77. <NotFoundBlock
  78. headerText={'OOPS! ORDERS CAN’T BE FOUND'}
  79. text={'No order has been made yet.'}
  80. />
  81. }
  82. </>
  83. )
  84. }
  85. export const CMainOrders = connect(state => ({orders: state.myorders,
  86. ordersPreload: state.promise['orderFind'], orderCount: state.promise['orderCount']}),
  87. {onFindOrders: actionFullOrderFind, onOrdersClear: actionMyOrderClear,
  88. onGetCountOrder: actionOrderCount})(MainOrders)