index.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import List from '@mui/material/List';
  2. import { makeStyles } from '@material-ui/core'
  3. import { useEffect } from 'react';
  4. import { useSelector,useDispatch } from 'react-redux';
  5. import AlertInfo from '../../../reusableComponents/AlertInfo'
  6. import ContactItem from './ContactItem';
  7. import { getState } from '../../../../redux/contacts/selector'
  8. import { asyncGetContacts } from '../../../../redux/contacts/operations'
  9. import { asyncStartChatById } from '../../../../redux/chat/operations'
  10. import { getIsOpen } from '../../../../redux/control/selector'
  11. import { actionIsOpen } from '../../../../redux/control/action';
  12. import { handleSort,timeStampFilter } from '../../../../helpers';
  13. import { TContacts,TContact } from '../../../../typescript/redux/contacts/types';
  14. const useStyles = makeStyles({
  15. list: {
  16. width: '100%',
  17. maxHeight: '93vh',
  18. overflowY: 'scroll',
  19. '&::-webkit-scrollbar': {
  20. width: '0.4em'
  21. },
  22. '&::-webkit-scrollbar-track': {
  23. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  24. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  25. backgroundColor: '#eceeec',
  26. },
  27. '&::-webkit-scrollbar-thumb': {
  28. backgroundColor: '#ccc8c8',
  29. },
  30. "&::-webkit-scrollbar-thumb:focus": {
  31. backgroundColor: "#959595",
  32. },
  33. "&::-webkit-scrollbar-thumb:active": {
  34. backgroundColor: "#959595",
  35. },
  36. },
  37. })
  38. interface IContactList {
  39. value: string,
  40. handleClick: any,
  41. sort: boolean,
  42. date: any
  43. }
  44. const ContactsList = ({value,handleClick,sort,date} : IContactList) => {
  45. const classes = useStyles()
  46. const dispatch = useDispatch()
  47. const { total, contacts } = useSelector(getState)
  48. const isOpen = useSelector(getIsOpen)
  49. const filteredContacts = ():TContacts => handleSort('name', contacts, sort).filter((el:TContact) => {
  50. const credentials = el.name + ' ' + el.lastName
  51. if (!date) {
  52. return credentials.toLowerCase().includes(value.toLowerCase())
  53. } else if (credentials.toLowerCase().includes(value.toLowerCase())
  54. && timeStampFilter(date) === timeStampFilter(el.createdAt)) {
  55. return el
  56. }
  57. })
  58. const handleListItemClick = (companionId:string) => {
  59. handleClick()
  60. isOpen&&dispatch(actionIsOpen(''))
  61. dispatch(asyncStartChatById(companionId))
  62. }
  63. useEffect(() => {
  64. dispatch(asyncGetContacts())
  65. const handleReset = () => isOpen !== 'credentials' &&dispatch(asyncGetContacts())
  66. const idInterval = setInterval(handleReset, 3000);
  67. return () => clearInterval(idInterval);
  68. }, [dispatch,isOpen]);
  69. const arr = filteredContacts()
  70. return total !== '0' ? (
  71. <List
  72. className={classes.list} component="nav"
  73. aria-label="main mailbox folders">
  74. {arr.length > 0 ? arr.map((contact) => <ContactItem key={contact.number}
  75. contact={contact} handleListItemClick={handleListItemClick} isOpen={isOpen}/>) :
  76. <AlertInfo name={`Can not find Contact by request : ${value}`} />}
  77. </List>
  78. ):<AlertInfo name='You do not have any Contact yet!' />;
  79. }
  80. export default ContactsList