Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion frontend/src/components/SearchComponents/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=()=>{
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/HomeChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export default function HomeChat() {
}, [dispatch]);

const selectChat = (data) => {
const isPresent = data.hasOwnProperty("notify");
dispatch(SetActiveChat(data));
};

Expand Down
155 changes: 91 additions & 64 deletions frontend/src/pages/Search.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 (
<div className='w-[80vw] relative flex flex-col'>
<ToastContainer/>
<SearchBar onChange={onChangeTextHandler} searchHandler={searchHandler} ></SearchBar>
<div className=' w-[100%] flex box-border justify-center py-2 relative'>
{!isLoading&&resultsEmpty&&<p>0 matching results found</p>}
{isLoading&&<Loading></Loading>}
{!isLoading&&users.length>0&&( <div className='w-[60%] border-[1px] rounded-md border-[#acacac] px-[1%] py-[1%] flex flex-col'>
{users.map((data,index)=><User accessChat={accessChatHandler} values={data} key={index}></User>)}
</div>)}
</div>
<div className="w-[80vw] relative flex flex-col">
<ToastContainer />
<SearchBar
onChange={onChangeTextHandler}
searchHandler={searchHandler}
></SearchBar>
<div className=" w-[100%] flex box-border justify-center py-2 relative">
{!isLoading && resultsEmpty && <p>0 matching results found</p>}
{isLoading && <Loading></Loading>}
{!isLoading && users.length > 0 && (
<div className="w-[60%] border-[1px] rounded-md border-[#acacac] px-[1%] py-[1%] flex flex-col">
{users.map((data, index) => (
<User
accessChat={accessChatHandler}
values={data}
key={index}
></User>
))}
</div>
)}
</div>
</div>
)
);
}