index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 { actionStartChat } from '../../../../redux/controlApp/action'
  15. import { TContact } from '../../../../typescript/redux/contacts/types'
  16. const useStyles = makeStyles({
  17. list: {
  18. width: '100%',
  19. maxHeight: '890px',
  20. overflowY: 'scroll',
  21. '&::-webkit-scrollbar': {
  22. width: '0.4em'
  23. },
  24. '&::-webkit-scrollbar-track': {
  25. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  26. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  27. backgroundColor: '#eceeec',
  28. },
  29. '&::-webkit-scrollbar-thumb': {
  30. backgroundColor: '#ccc8c8',
  31. },
  32. "&::-webkit-scrollbar-thumb:focus": {
  33. backgroundColor: "#959595",
  34. },
  35. "&::-webkit-scrollbar-thumb:active": {
  36. backgroundColor: "#959595",
  37. },
  38. },
  39. listItem_iconAvatar: {
  40. marginRight:10
  41. },
  42. })
  43. const ContactsList = () => {
  44. const classes = useStyles()
  45. const dispatch = useDispatch()
  46. const { total, contacts } = useSelector(getState)
  47. const [selectedIndex, setSelectedIndex] = useState<number>(1);
  48. const handleListItemClick = async (i:number, companion:TContact) => {
  49. setSelectedIndex(i);
  50. await dispatch(actionStartChat(companion))
  51. }
  52. useEffect(() => {
  53. dispatch(asyncGetContacts())
  54. }, [dispatch])
  55. return total !== '0' ? (
  56. <List
  57. className={classes.list} component="nav"
  58. aria-label="main mailbox folders">
  59. {contacts.map((contact, i: number) => {
  60. const { name, lastName, avatarUrl, updatedAt } = contact
  61. return (
  62. <ListItemButton
  63. key={shortid.generate()}
  64. selected={selectedIndex === i}
  65. onClick={() => handleListItemClick(i, contact)}
  66. >
  67. <ListItemIcon className={classes.listItem_iconAvatar}>
  68. <Avatar alt={name} src={avatarUrl?`http://localhost:3000/${avatarUrl}`:undefined}
  69. sx={{ background: '#f0c712', width: 54, height: 54 }}>
  70. {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
  71. </Avatar>
  72. </ListItemIcon>
  73. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  74. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  75. secondary={`last seen ${timeStamp(updatedAt)}`} />
  76. </ListItemButton>)})}
  77. </List>
  78. ):<AlertInfo name='You do not have any contact yet!' />;
  79. }
  80. export default ContactsList