ClientsTab.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React from 'react';
  2. import {useEffect, useState} from "react";
  3. import Box from "@mui/material/Box";
  4. import Typography from "@mui/material/Typography";
  5. import {CircularProgress, Divider, Pagination} from "@mui/material";
  6. import {connect} from "react-redux";
  7. import {actionUserCount, actionUserFind} from "../../../actions/ActionUserFind";
  8. import {ClientsCart} from "./ClientsCart";
  9. const Clients = ({usersArr, usersCount, getAllClients, getCountUser}) => {
  10. const itemsPerPage = 100
  11. const [page, setPage] = useState(1)
  12. const [count, setCount] = useState(1)
  13. useEffect(() => {
  14. if (!usersArr && page === 1) getAllClients()
  15. if (!usersCount) getCountUser()
  16. else {
  17. setCount(Math.ceil(+usersCount?.payload / itemsPerPage))
  18. }
  19. }, [usersArr, usersCount])
  20. const handleChange = (event, value) => {
  21. setPage(value);
  22. getAllClients((itemsPerPage * value)-itemsPerPage, itemsPerPage);
  23. }
  24. return (
  25. <>
  26. <Box
  27. marginBottom='20px'
  28. width='100%'
  29. display='flex'
  30. justifyContent='space-between'
  31. alignItems='center'
  32. >
  33. {usersCount?.payload &&
  34. <Typography
  35. variant='h6'
  36. letterSpacing='2px'
  37. >
  38. Total clients: {usersCount.payload}
  39. </Typography>
  40. }
  41. </Box>
  42. {!usersArr || !usersArr?.payload ?
  43. <Box
  44. sx={{
  45. height: '100%',
  46. width: '100%',
  47. display: 'flex',
  48. justifyContent:'center',
  49. alignItems:'center'
  50. }}
  51. >
  52. <CircularProgress color="inherit"/>
  53. </Box>
  54. :
  55. Array.isArray(usersArr.payload) && usersArr.payload.map(item => {
  56. return(
  57. <ClientsCart
  58. key={item?._id}
  59. _id={item?._id || ''}
  60. login={item?.login || 'not found'}
  61. nick={item?.nick || ''}
  62. createdAt={item?.createdAt || ''}
  63. avatar={item?.avatar}
  64. acl={item?.acl}
  65. />
  66. )
  67. })
  68. }
  69. <Box
  70. width='100%'
  71. flexGrow='0'
  72. >
  73. <Divider sx={{margin: '20px'}}/>
  74. <Box
  75. display='flex'
  76. justifyContent='center'
  77. width='100%'
  78. >
  79. <Pagination
  80. count={count || 1}
  81. page={page}
  82. onChange={handleChange}
  83. defaultPage={1}
  84. color="primary"
  85. size="large"
  86. showFirstButton
  87. showLastButton
  88. />
  89. </Box>
  90. </Box>
  91. </>
  92. )
  93. }
  94. export const CClients = connect(state => ({
  95. usersArr: state.promise['allUsers'],
  96. usersCount: state.promise['usersCount']}),
  97. {
  98. getAllClients: actionUserFind,
  99. getCountUser: actionUserCount})
  100. (Clients)