1
- import React , { useState } from ' react'
2
- import SearchBar from ' ../components/SearchComponents/SearchBar'
3
- import Loading from ' ../components/SearchComponents/Loading'
4
- import User from ' ../components/SearchComponents/User'
5
- import { ToastContainer , toast } from ' react-toastify' ;
6
- import ' react-toastify/dist/ReactToastify.css' ;
7
- import { AddUser } from ' ../services/Actions/Chat/action' ;
8
- import { useDispatch } from ' react-redux' ;
9
- import { useNavigate } from ' react-router-dom' ;
10
- import { useSelector } from ' react-redux' ;
1
+ import React , { useState } from " react" ;
2
+ import SearchBar from " ../components/SearchComponents/SearchBar" ;
3
+ import Loading from " ../components/SearchComponents/Loading" ;
4
+ import User from " ../components/SearchComponents/User" ;
5
+ import { ToastContainer , toast } from " react-toastify" ;
6
+ import " react-toastify/dist/ReactToastify.css" ;
7
+ import { AddUser , SetActiveChat } from " ../services/Actions/Chat/action" ;
8
+ import { useDispatch } from " react-redux" ;
9
+ import { useNavigate } from " react-router-dom" ;
10
+ import { useSelector } from " react-redux" ;
11
11
export default function Search ( ) {
12
+ const navigate = useNavigate ( ) ;
13
+ const allChats = useSelector ( ( state ) => state . chat . AllChats ) ;
14
+ const dispatch = useDispatch ( ) ;
15
+
12
16
13
- const dispatch = useDispatch ( ) ;
14
- const navigate = useNavigate ( )
15
- const state = useSelector ( ( state ) => state . chat . AllChats )
16
-
17
- const notify = ( value ) => {
17
+ const notify = ( value ) => {
18
18
return toast . info ( `Added ${ value } ` , {
19
19
position : "bottom-center" ,
20
20
autoClose : 2222 ,
@@ -24,75 +24,102 @@ export default function Search() {
24
24
draggable : true ,
25
25
progress : undefined ,
26
26
theme : "light" ,
27
- } ) ;
27
+ } ) ;
28
28
} ;
29
29
30
- const [ isLoading , SetisLoading ] = useState ( false ) ;
31
- const [ users , SetUsers ] = useState ( [ ] )
30
+ const [ isLoading , SetisLoading ] = useState ( false ) ;
31
+ const [ users , SetUsers ] = useState ( [ ] ) ;
32
32
// const[query,setQuery]=useState('');
33
- const [ resultsEmpty , setResultsEmpty ] = useState ( false ) ;
34
-
33
+ const [ resultsEmpty , setResultsEmpty ] = useState ( false ) ;
35
34
36
- const onChangeTextHandler = ( e ) => {
35
+ const onChangeTextHandler = ( e ) => {
37
36
// setQuery(e.target.value);
38
- if ( ! e . target . value )
39
- {
37
+ if ( ! e . target . value ) {
40
38
return ;
41
39
}
42
40
43
- const timeout = setTimeout ( ( ) => {
44
- searchHandler ( e . target . value )
41
+ const timeout = setTimeout ( ( ) => {
42
+ searchHandler ( e . target . value ) ;
45
43
} , 1000 ) ;
46
44
47
- return ( ) => {
45
+ return ( ) => {
48
46
clearTimeout ( timeout ) ;
49
- }
50
- }
51
-
52
- const searchHandler = async ( value ) => {
47
+ } ;
48
+ } ;
53
49
50
+ const searchHandler = async ( value ) => {
54
51
SetisLoading ( true ) ;
55
- const cookie = localStorage . getItem ( 'jwt' ) ;
56
- const response = await fetch ( `${ process . env . REACT_APP_API_URL } /api/v1/users?search=${ value } ` , {
57
- headers :{
58
- 'Content-type' :'application/json' ,
59
- 'Authorization' :`Bearer ${ cookie } `
60
- }
61
- } )
62
- const data = await response . json ( ) ;
63
- SetisLoading ( false ) ;
64
- data . users . length = data . users . length > 2 ?data . users . length = 2 :data . users . length ;
65
- SetUsers ( data . users )
66
- if ( data . users . length === 0 )
67
- setResultsEmpty ( true )
68
- else
69
- setResultsEmpty ( false ) ;
70
-
71
- }
72
-
73
- const accessChatHandler = ( values ) => {
52
+ const cookie = localStorage . getItem ( "jwt" ) ;
53
+ const response = await fetch (
54
+ `${ process . env . REACT_APP_API_URL } /api/v1/users?search=${ value } ` ,
55
+ {
56
+ headers : {
57
+ "Content-type" : "application/json" ,
58
+ Authorization : `Bearer ${ cookie } ` ,
59
+ } ,
60
+ }
61
+ ) ;
62
+ const data = await response . json ( ) ;
63
+ SetisLoading ( false ) ;
64
+ data . users . length =
65
+ data . users . length > 2 ? ( data . users . length = 2 ) : data . users . length ;
66
+ SetUsers ( data . users ) ;
67
+ if ( data . users . length === 0 ) setResultsEmpty ( true ) ;
68
+ else setResultsEmpty ( false ) ;
69
+ } ;
74
70
75
- const isPresent = state . find ( ( data ) => {
76
- return data . email === values . email
77
- } ) ;
78
- dispatch ( AddUser ( values , state ) )
71
+ const accessChatHandler = ( values ) => {
72
+ let isPresent = false ;
73
+ let activeChat = null ;
74
+ for ( let i = 0 ; i < allChats . length ; i ++ ) {
75
+ if ( allChats [ i ] . chatName === "sender" ) {
76
+ for ( let j = 0 ; j < allChats [ i ] . users . length ; j ++ ) {
77
+ if ( allChats [ i ] . users [ j ] . email === values . email ) {
78
+ isPresent = true ;
79
+ activeChat = allChats [ i ] ;
80
+ break ;
81
+ }
82
+ }
83
+ }
84
+ if ( isPresent ) {
85
+ break ;
86
+ }
87
+ }
88
+ if ( isPresent ) {
89
+ dispatch ( SetActiveChat ( activeChat ) ) ;
90
+ navigate ( "/home/message" , { replace :true } )
91
+ } else {
92
+ dispatch ( AddUser ( values , allChats ) ) ;
79
93
notify ( values . name ) ;
80
94
setTimeout ( ( ) => {
95
+ dispatch ( SetActiveChat ( allChats [ 0 ] ) ) ;
81
96
navigate ( '/home/message' , { replace :true } )
82
97
} , 2000 )
83
98
}
99
+ } ;
84
100
85
101
return (
86
- < div className = 'w-[80vw] relative flex flex-col' >
87
- < ToastContainer />
88
- < SearchBar onChange = { onChangeTextHandler } searchHandler = { searchHandler } > </ SearchBar >
89
- < div className = ' w-[100%] flex box-border justify-center py-2 relative' >
90
- { ! isLoading && resultsEmpty && < p > 0 matching results found</ p > }
91
- { isLoading && < Loading > </ Loading > }
92
- { ! isLoading && users . length > 0 && ( < div className = 'w-[60%] border-[1px] rounded-md border-[#acacac] px-[1%] py-[1%] flex flex-col' >
93
- { users . map ( ( data , index ) => < User accessChat = { accessChatHandler } values = { data } key = { index } > </ User > ) }
94
- </ div > ) }
95
- </ div >
102
+ < div className = "w-[80vw] relative flex flex-col" >
103
+ < ToastContainer />
104
+ < SearchBar
105
+ onChange = { onChangeTextHandler }
106
+ searchHandler = { searchHandler }
107
+ > </ SearchBar >
108
+ < div className = " w-[100%] flex box-border justify-center py-2 relative" >
109
+ { ! isLoading && resultsEmpty && < p > 0 matching results found</ p > }
110
+ { isLoading && < Loading > </ Loading > }
111
+ { ! isLoading && users . length > 0 && (
112
+ < div className = "w-[60%] border-[1px] rounded-md border-[#acacac] px-[1%] py-[1%] flex flex-col" >
113
+ { users . map ( ( data , index ) => (
114
+ < User
115
+ accessChat = { accessChatHandler }
116
+ values = { data }
117
+ key = { index }
118
+ > </ User >
119
+ ) ) }
120
+ </ div >
121
+ ) }
122
+ </ div >
96
123
</ div >
97
- )
124
+ ) ;
98
125
}
0 commit comments