MainOrders.jsx 3.7 KB

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