index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import BottomNavigation from '@mui/material/BottomNavigation';
  2. import BottomNavigationAction from '@mui/material/BottomNavigationAction';
  3. import { useState, useEffect } from 'react';
  4. import { useDispatch,useSelector } from 'react-redux';
  5. import { makeStyles } from '@material-ui/core'
  6. import ChatListRecent from './ChatListRecent'
  7. import FilesList from './FilesList';
  8. import MediaList from './MediaList';
  9. import TextList from './TextList';
  10. import AudioList from './AudioList'
  11. import VideoList from './VideoList';
  12. import { asyncGetAllMessages } from '../../../../redux/allMessages/operations';
  13. import { asyncGetChats } from '../../../../redux/chats/operations';
  14. import { getStateMemo } from '../../../../redux/chats/selector';
  15. import { getAllMessagesMemo } from '../../../../redux/allMessages/selector';
  16. import { getIsOpen } from '../../../../redux/control/selector';
  17. import { sortByRecent,handleSort } from '../../../../helpers';
  18. import { asyncStartChatById } from '../../../../redux/chat/operations';
  19. import { actionIsOpen } from '../../../../redux/control/action';
  20. import { TAllMessages } from '../../../../typescript/redux/allMessages/types';
  21. import { TChats } from '../../../../typescript/redux/chats/types';
  22. const useStyles = makeStyles({
  23. container: {
  24. height:'7vh',
  25. borderBottom: 'solid 2px #dddddd',
  26. display: 'flex',
  27. justifyContent: 'space-around',
  28. alignContent: "center",
  29. alignItems:"center",
  30. flexWrap: 'nowrap',
  31. color:'rgba(0, 0, 0, 0.6)'
  32. },
  33. item: {
  34. height:'100%',
  35. display: 'flex',
  36. flexDirection: 'column',
  37. justifyContent: 'center',
  38. alignContent: "center",
  39. alignItems: "center",
  40. cursor:'pointer',
  41. },
  42. icon: {
  43. fontSize: '1rem',
  44. lineHeight: 0,
  45. marginBottom: 0,
  46. fontWeight:600
  47. },
  48. underline: {
  49. fontSize: '2.2rem',
  50. lineHeight: 0,
  51. },
  52. })
  53. interface ISearchLists {
  54. value: string,
  55. sort: boolean
  56. }
  57. const SearchLists = ({ value,sort }: ISearchLists) => {
  58. const classes = useStyles()
  59. const dispatch = useDispatch()
  60. const { chats } = useSelector(getStateMemo)
  61. const messagesMemo = useSelector(getAllMessagesMemo)
  62. const isOpen = useSelector(getIsOpen)
  63. const [isActive, setIsActive] = useState<number>(0)
  64. const handleIsActive = (newValue: number): void => setIsActive(newValue)
  65. const handleListItemClick = (companionId: string) => {
  66. isOpen&&dispatch(actionIsOpen(''))
  67. dispatch(asyncStartChatById(companionId))
  68. }
  69. const filterChats= (arr:any) => arr.filter((el:any) => {
  70. const credentials = el.name + ' ' + el.lastName
  71. if(credentials.toLowerCase().includes(value.toLowerCase())) return el
  72. })
  73. const filterMessages = (arr: any) => arr.filter((el: any) => {
  74. if (!el.fullType && el.message.toLowerCase().includes(value.toLowerCase())) {
  75. return el
  76. } else if (el.fullType&& el.fullType.toLowerCase().includes(value.toLowerCase())) {
  77. return el
  78. }
  79. })
  80. useEffect(() => {
  81. dispatch(asyncGetAllMessages())
  82. dispatch(asyncGetChats())
  83. const handleReset = () => {
  84. dispatch(asyncGetAllMessages())
  85. dispatch(asyncGetChats())
  86. }
  87. const idInterval = setInterval(handleReset, 5000);
  88. return () => clearInterval(idInterval);
  89. }, [dispatch]);
  90. const filteredAndSorted:TChats = filterChats(sortByRecent(chats,sort))
  91. const allMessagesMemo:TAllMessages = filterMessages(handleSort('updatedAt',messagesMemo,sort))
  92. return (
  93. <>
  94. <div className={classes.container}>
  95. <div className={classes.item} style={{color:isActive === 0?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  96. onClick={() => handleIsActive(0)}>
  97. <span className={classes.icon}>Chats</span>
  98. <span className={classes.underline}>___</span>
  99. </div>
  100. <div className={classes.item} style={{color:isActive === 1?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  101. onClick={() => handleIsActive(1)}>
  102. <span className={classes.icon}>Files</span>
  103. <span className={classes.underline}>___</span>
  104. </div>
  105. <div className={classes.item} style={{color:isActive === 2?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  106. onClick={() => handleIsActive(2)}>
  107. <span className={classes.icon}>Media</span>
  108. <span className={classes.underline}>___</span>
  109. </div>
  110. <div className={classes.item} style={{color:isActive === 3?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  111. onClick={() => handleIsActive(3)}>
  112. <span className={classes.icon}>Text</span>
  113. <span className={classes.underline}>___</span>
  114. </div>
  115. <div className={classes.item} style={{color:isActive === 4?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  116. onClick={() => handleIsActive(4)}>
  117. <span className={classes.icon}>Audio</span>
  118. <span className={classes.underline}>___</span>
  119. </div>
  120. <div className={classes.item} style={{color:isActive === 5?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  121. onClick={() => handleIsActive(5)}>
  122. <span className={classes.icon}>Video</span>
  123. <span className={classes.underline}>___</span>
  124. </div>
  125. </div>
  126. {isActive === 0 && <ChatListRecent value={value}
  127. filteredAndSorted={filteredAndSorted} handleListItemClick={handleListItemClick} />}
  128. {isActive === 1 && <FilesList allMessagesMemo={allMessagesMemo} value={value}/>}
  129. {isActive === 2 && <MediaList allMessagesMemo={allMessagesMemo} value={value}/>}
  130. {isActive === 3 && <TextList allMessagesMemo={allMessagesMemo} value={value}/>}
  131. {isActive === 4 && <AudioList allMessagesMemo={allMessagesMemo} value={value}/>}
  132. {isActive === 5 && <VideoList allMessagesMemo={allMessagesMemo} value={value}/>}
  133. </>
  134. )
  135. }
  136. export default SearchLists