diff --git a/frontend/src/components/SearchComponents/User.js b/frontend/src/components/SearchComponents/User.js index 85180e0..48b82bc 100644 --- a/frontend/src/components/SearchComponents/User.js +++ b/frontend/src/components/SearchComponents/User.js @@ -2,7 +2,6 @@ import React from 'react' import { Avatar } from '@mui/material' import { MotionAnimate } from 'react-motion-animate' import image from '../../assets/images/user-img.jpg' -import { useEffect } from 'react' export default function User({values,accessChat}) { const accessChatHandler=()=>{ diff --git a/frontend/src/pages/HomeChat.js b/frontend/src/pages/HomeChat.js index 1af77db..831d0ec 100644 --- a/frontend/src/pages/HomeChat.js +++ b/frontend/src/pages/HomeChat.js @@ -71,7 +71,6 @@ export default function HomeChat() { }, [dispatch]); const selectChat = (data) => { - const isPresent = data.hasOwnProperty("notify"); dispatch(SetActiveChat(data)); }; diff --git a/frontend/src/pages/Search.js b/frontend/src/pages/Search.js index 06636d5..f11d981 100644 --- a/frontend/src/pages/Search.js +++ b/frontend/src/pages/Search.js @@ -1,20 +1,20 @@ -import React, { useState } from 'react' -import SearchBar from '../components/SearchComponents/SearchBar' -import Loading from '../components/SearchComponents/Loading' -import User from '../components/SearchComponents/User' -import { ToastContainer, toast } from 'react-toastify'; -import 'react-toastify/dist/ReactToastify.css'; -import { AddUser } from '../services/Actions/Chat/action'; -import { useDispatch } from 'react-redux'; -import { useNavigate } from 'react-router-dom'; -import { useSelector } from 'react-redux'; +import React, { useState } from "react"; +import SearchBar from "../components/SearchComponents/SearchBar"; +import Loading from "../components/SearchComponents/Loading"; +import User from "../components/SearchComponents/User"; +import { ToastContainer, toast } from "react-toastify"; +import "react-toastify/dist/ReactToastify.css"; +import { AddUser, SetActiveChat } from "../services/Actions/Chat/action"; +import { useDispatch } from "react-redux"; +import { useNavigate } from "react-router-dom"; +import { useSelector } from "react-redux"; export default function Search() { + const navigate = useNavigate(); + const allChats = useSelector((state) => state.chat.AllChats); + const dispatch = useDispatch(); + - const dispatch=useDispatch(); - const navigate=useNavigate() - const state=useSelector((state)=>state.chat.AllChats) - - const notify = (value)=>{ + const notify = (value) => { return toast.info(`Added ${value}`, { position: "bottom-center", autoClose: 2222, @@ -24,75 +24,102 @@ export default function Search() { draggable: true, progress: undefined, theme: "light", - }); + }); }; - const[isLoading,SetisLoading]=useState(false); - const [users,SetUsers]=useState([]) + const [isLoading, SetisLoading] = useState(false); + const [users, SetUsers] = useState([]); // const[query,setQuery]=useState(''); - const[resultsEmpty,setResultsEmpty]=useState(false); - + const [resultsEmpty, setResultsEmpty] = useState(false); - const onChangeTextHandler=(e)=>{ + const onChangeTextHandler = (e) => { // setQuery(e.target.value); - if(!e.target.value) - { + if (!e.target.value) { return; } - const timeout=setTimeout(() => { - searchHandler(e.target.value) + const timeout = setTimeout(() => { + searchHandler(e.target.value); }, 1000); - return ()=>{ + return () => { clearTimeout(timeout); - } - } - - const searchHandler=async(value)=>{ + }; + }; + const searchHandler = async (value) => { SetisLoading(true); - const cookie=localStorage.getItem('jwt'); - const response=await fetch(`${process.env.REACT_APP_API_URL}/api/v1/users?search=${value}`,{ - headers:{ - 'Content-type':'application/json', - 'Authorization':`Bearer ${cookie}` - } - }) - const data=await response.json(); - SetisLoading(false); - data.users.length=data.users.length>2?data.users.length=2:data.users.length; - SetUsers(data.users) - if(data.users.length===0) - setResultsEmpty(true) - else - setResultsEmpty(false); - - } - - const accessChatHandler=(values)=>{ + const cookie = localStorage.getItem("jwt"); + const response = await fetch( + `${process.env.REACT_APP_API_URL}/api/v1/users?search=${value}`, + { + headers: { + "Content-type": "application/json", + Authorization: `Bearer ${cookie}`, + }, + } + ); + const data = await response.json(); + SetisLoading(false); + data.users.length = + data.users.length > 2 ? (data.users.length = 2) : data.users.length; + SetUsers(data.users); + if (data.users.length === 0) setResultsEmpty(true); + else setResultsEmpty(false); + }; - const isPresent=state.find((data)=>{ - return data.email===values.email - }); - dispatch(AddUser(values,state)) + const accessChatHandler = (values) => { + let isPresent = false; + let activeChat = null; + for (let i = 0; i < allChats.length; i++) { + if (allChats[i].chatName === "sender") { + for (let j = 0; j < allChats[i].users.length; j++) { + if (allChats[i].users[j].email === values.email) { + isPresent = true; + activeChat = allChats[i]; + break; + } + } + } + if (isPresent) { + break; + } + } + if (isPresent) { + dispatch(SetActiveChat(activeChat)); + navigate("/home/message",{replace:true}) + }else{ + dispatch(AddUser(values, allChats)); notify(values.name); setTimeout(()=>{ + dispatch(SetActiveChat(allChats[0])); navigate('/home/message',{replace:true}) },2000) } + }; return ( -
0 matching results found
} - {isLoading&&0 matching results found
} + {isLoading &&