index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import List from '@mui/material/List';
  2. import ListItemButton from '@mui/material/ListItemButton';
  3. import Avatar from '@mui/material/Avatar';
  4. import ListItemText from '@mui/material/ListItemText';
  5. import ListItemIcon from '@mui/material/ListItemIcon';
  6. import { makeStyles } from '@material-ui/core'
  7. import { useState,useEffect } from 'react';
  8. import shortid from 'shortid';
  9. import { useSelector,useDispatch } from 'react-redux';
  10. import AlertInfo from '../../../reusableComponents/AlertInfo'
  11. import { getState } from '../../../../redux/contacts/selector'
  12. import { asyncGetContacts } from '../../../../redux/contacts/operations'
  13. import { firstLetter, slicedWord, timeStamp } from '../../../../helpers'
  14. import { asyncStartChatById } from '../../../../redux/chat/operations'
  15. const useStyles = makeStyles({
  16. list: {
  17. width: '100%',
  18. maxHeight: '890px',
  19. overflowY: 'scroll',
  20. '&::-webkit-scrollbar': {
  21. width: '0.4em'
  22. },
  23. '&::-webkit-scrollbar-track': {
  24. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  25. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  26. backgroundColor: '#eceeec',
  27. },
  28. '&::-webkit-scrollbar-thumb': {
  29. backgroundColor: '#ccc8c8',
  30. },
  31. "&::-webkit-scrollbar-thumb:focus": {
  32. backgroundColor: "#959595",
  33. },
  34. "&::-webkit-scrollbar-thumb:active": {
  35. backgroundColor: "#959595",
  36. },
  37. },
  38. listItem_iconAvatar: {
  39. marginRight:10
  40. },
  41. })
  42. const ContactsList = () => {
  43. const classes = useStyles()
  44. const dispatch = useDispatch()
  45. const { total, contacts } = useSelector(getState)
  46. const [selectedIndex, setSelectedIndex] = useState<number>(1);
  47. const handleListItemClick = async (i:number, companionId:string) => {
  48. setSelectedIndex(i);
  49. await dispatch(asyncStartChatById(companionId))
  50. }
  51. useEffect(() => {
  52. dispatch(asyncGetContacts())
  53. }, [dispatch])
  54. return total !== '0' ? (
  55. <List
  56. className={classes.list} component="nav"
  57. aria-label="main mailbox folders">
  58. {contacts.map(({name,lastName,avatarUrl,color,createdAt,companionId }, i: number) =>
  59. <ListItemButton
  60. key={shortid.generate()}
  61. selected={selectedIndex === i}
  62. onClick={() => handleListItemClick(i,companionId)}
  63. >
  64. <ListItemIcon className={classes.listItem_iconAvatar}>
  65. <Avatar alt={name} src={avatarUrl?`http://localhost:3000/${avatarUrl}`:undefined}
  66. sx={{ background: color, width: 54, height: 54 }}>
  67. {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
  68. </Avatar>
  69. </ListItemIcon>
  70. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  71. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  72. secondary={`Registered since ${timeStamp(createdAt)}`} />
  73. </ListItemButton>)}
  74. </List>
  75. ):<AlertInfo name='You do not have any contact yet!' />;
  76. }
  77. export default ContactsList