Skip to content

Commit

Permalink
Merge pull request #31 from feliciahmq/Issues
Browse files Browse the repository at this point in the history
Fix: Add user
  • Loading branch information
vanesssalai authored Jul 7, 2024
2 parents 5e89aaf + f729cb2 commit f553f22
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 274 deletions.
494 changes: 261 additions & 233 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ return (
<Route path="/notifications/:userID" element={<NotificationsPage />} />
<Route path="/chat" element={<Chat />} />
<Route path="/chat/:userID" element={<Chat />} />
<Route path="/chat/:userID/:chatID" element={<Chat />} />
</Routes>
</Router>
</LikeCountProvider>
Expand Down
1 change: 1 addition & 0 deletions src/components/chatComp/chatApp_chat/Chat.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
display: flex;
align-items: center;
gap: 20px;
cursor: pointer;

img {
width: 60px;
Expand Down
18 changes: 17 additions & 1 deletion src/components/chatComp/chatApp_chat/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useChatStore } from '../../../lib/chatStore';
import { useUserStore } from '../../../lib/userStore';
import upload from '../../../lib/upload';
import { formatDistanceToNow, isValid } from 'date-fns';
import { useNavigate } from 'react-router-dom';

const Chat = () => {
const [chat, setChat] = useState();
Expand All @@ -21,6 +22,7 @@ const Chat = () => {

const { currentUser } = useUserStore();
const { chatId, user, isCurrentUserBlocked, isReceiverBlocked } = useChatStore();
const navigate = useNavigate();

const endRef = useRef(null);

Expand Down Expand Up @@ -147,10 +149,14 @@ const Chat = () => {
setShowImg(false);
};

const handleProfileClick = () => {
navigate(`/profile/${user?.id}`);
}

return (
<div className='chat'>
<div className="top">
<div className="user">
<div className="user" onClick={handleProfileClick}>
<img src={user?.profilePic || "/chat-icons/avatar.png"} alt="" />
<div className="texts">
<span>{user?.username}</span>
Expand Down Expand Up @@ -188,6 +194,11 @@ const Chat = () => {
placeholder={(isCurrentUserBlocked || isReceiverBlocked) ? "You cannot send a message" : "Type a message"}
value={text}
onChange={e=>setText(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleSendText();
}
}}
disabled={isCurrentUserBlocked || isReceiverBlocked}
/>
<div className="emoji">
Expand All @@ -213,6 +224,11 @@ const Chat = () => {
<input
placeholder="Add a message to this image."
value={imgMsg}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleSendImg();
}
}}
onChange={(e) => setImgMsg(e.target.value)}
/>
<button onClick={handleSendImg}>Send Image</button>
Expand Down
1 change: 1 addition & 0 deletions src/components/chatComp/chatApp_detail/Detail.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
align-items: center;
gap: 15px;
border-bottom: 1px solid #07060635;
cursor: pointer;

img {
width: 80px;
Expand Down
9 changes: 8 additions & 1 deletion src/components/chatComp/chatApp_detail/Detail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useChatStore } from '../../../lib/chatStore';
import { useUserStore } from '../../../lib/userStore';
import { db } from '../../../lib/firebaseConfig';
import './Detail.css';
import { useNavigate } from 'react-router-dom';

const Detail = () => {
const { chatId, user, isCurrentUserBlocked, isReceiverBlocked, changeBlock } = useChatStore();
Expand All @@ -12,6 +13,8 @@ const Detail = () => {
const [showPhotos, setShowPhotos] = useState(false);
const [icon, setIcon] = useState("/chat-icons/arrowDown.png");

const navigate = useNavigate();

useEffect(() => {
if (!chatId) return;

Expand Down Expand Up @@ -49,9 +52,13 @@ const Detail = () => {
setIcon(showPhotos ? "/chat-icons/arrowDown.png" : "/chat-icons/arrowUp.png");
};

const handleProfileClick = () => {
navigate(`/profile/${user?.id}`);
}

return (
<div className='detail'>
<div className="user">
<div className="user" onClick={handleProfileClick}>
<img src={user?.profilePic || "/chat-icons/avatar.png"} alt="" />
<h2>{user?.username}</h2>
</div>
Expand Down
76 changes: 46 additions & 30 deletions src/components/chatComp/chatApp_list/addUser/addUser.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { arrayUnion, collection, doc, getDocs, query, setDoc, updateDoc, where } from "firebase/firestore";
import { addDoc, arrayUnion, collection, doc, getDoc, getDocs, query, updateDoc, where } from "firebase/firestore";
import "./addUser.css"
import { db } from "../../../../lib/firebaseConfig";
import { useState } from "react";
import { useUserStore } from "../../../../lib/userStore";
import toast from "react-hot-toast";
import { useNavigate } from "react-router-dom";

const addUser = ({ closePopup }) => {
const [user, setUser] = useState(null);
const [error, setError] = useState("");
const { currentUser } = useUserStore();
const navigate = useNavigate();

const handleSearch = async e => {
e.preventDefault();
Expand All @@ -27,48 +30,61 @@ const addUser = ({ closePopup }) => {
setError("User not found.");
}
} catch (err) {
console.log(err);
setUser(null);
setError("User not found.");
console.log(err);
setUser(null);
setError("User not found.");
}
};

const handleAdd = async () => {

const chatRef = collection(db, "Chats");
const userChatsRef = collection(db, "UserChats");

try {
const newChatRef = doc(chatRef);
const currentUserChatsDoc = await getDoc(doc(userChatsRef, currentUser.id));
const existingChats = currentUserChatsDoc.exists() && currentUserChatsDoc.data().chats ? currentUserChatsDoc.data().chats : [];
const existingChat = existingChats.find( chat => chat.receiverId === user.id );

if (existingChat) {
toast.error("A chat with this user already exists.");
navigate(`/chat/${currentUser.id}/${existingChat.id}`);
return;
}

await setDoc(newChatRef, {
createdAt: new Date(),
messages:[],
});
const newChatRef = await addDoc(chatRef, {
createdAt: new Date(),
messages: [],
});

if (!newChatRef.id) {
throw new Error("Failed to create new chat");
}

await updateDoc(doc(userChatsRef, user.id), {
chats: arrayUnion({
chatId: newChatRef.id,
lastMessage: "",
receiverId: currentUser.id,
updatedAt: Date.now(),
}),
});
await updateDoc(doc(userChatsRef, user.id), {
chats: arrayUnion({
chatId: newChatRef.id,
lastMessage: "",
receiverId: currentUser.id,
updatedAt: Date.now(),
}),
});

await updateDoc(doc(userChatsRef, currentUser.id), {
chats: arrayUnion({
chatId: newChatRef.id,
lastMessage: "",
receiverId: user.id,
updatedAt: Date.now(),
}),
});
await updateDoc(doc(userChatsRef, currentUser.id), {
chats: arrayUnion({
chatId: newChatRef.id,
lastMessage: "",
receiverId: user.id,
updatedAt: Date.now(),
}),
});

closePopup();
navigate(`/chat/${currentUser.id}/${newChatRef.id}`);

closePopup();
} catch (err) {
console.log(err);
console.log(err);
}
}
};

return (
<div className="addUser">
Expand All @@ -90,7 +106,7 @@ const addUser = ({ closePopup }) => {
) : (
error &&
<div className="error">
{error}
{error}
</div>
)}
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/components/chatComp/chatApp_list/chatList/ChatList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useUserStore } from '../../../../lib/userStore';
import { doc, onSnapshot, getDoc, updateDoc } from 'firebase/firestore';
import { db } from '../../../../lib/firebaseConfig';
import { useChatStore } from '../../../../lib/chatStore';
import { useNavigate } from 'react-router-dom';

const ChatList = () => {
const [chats, setChats] = useState([]);
Expand All @@ -14,6 +15,8 @@ const ChatList = () => {
const { currentUser } = useUserStore();
const { changeChat } = useChatStore();

const navigate = useNavigate();

useEffect(() => {
if (!currentUser || !currentUser.id) {
console.error("Current user is not defined");
Expand Down Expand Up @@ -63,6 +66,7 @@ const ChatList = () => {
chats: userChats,
})
changeChat(chat.chatId, chat.user);
navigate(`/chat/${currentUser.id}/${chat.chatId}`);

} catch (err) {
console.log(err);
Expand Down
1 change: 1 addition & 0 deletions src/components/chatComp/chatApp_list/userInfo/UserInfo.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
align-items: center;
gap: 20px;
flex-grow: 1;
cursor: pointer;

h2 {
font-size: 18px;
Expand Down
8 changes: 7 additions & 1 deletion src/components/chatComp/chatApp_list/userInfo/UserInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { useNavigate } from 'react-router-dom';
import { useUserStore } from '../../../../lib/userStore';
import './UserInfo.css';

const UserInfo = () => {
const {currentUser } = useUserStore();
const navigate = useNavigate();

const handleProfileClick = () => {
navigate(`/profile/${currentUser.id}`);
}

return (
<div className='userInfo'>
<div className='user'>
<div className='user' onClick={handleProfileClick}>
<img src={currentUser.profilePic || '/chat-icons/avatar.png'} alt='' />
<h2>{currentUser.username}</h2>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/lib/chatStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useUserStore } from './userStore';

export const useChatStore = create((set) => ({
chatId: null,
setChatId: (id) => set({ chatId: id }),
user: null,
isCurrentUserBlocked: false,
isReceiverBlocked: false,
Expand Down
10 changes: 8 additions & 2 deletions src/pages/chatapp/ChatApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import List from "../../components/chatComp/chatApp_list/List.jsx";
import Header from "../../components/header/Header";
import LoginSignupForm from "../../pages/registration/LoginSignupForm.jsx";
import { useChatStore } from '../../lib/chatStore.js';
import { useParams } from 'react-router-dom';

const ChatApp = () => {
const { userID, chatID } = useParams();
const { currentUser, isLoading, fetchUserInfo } = useUserStore();
const { chatId } = useChatStore();
const { chatId, setChatId } = useChatStore();

useEffect(() => {
const unSub = onAuthStateChanged(auth, (user) => {
Expand All @@ -29,7 +31,11 @@ const ChatApp = () => {
};
}, [fetchUserInfo]);

console.log(currentUser);
useEffect(() => {
if (userID && chatID) {
setChatId(chatID);
}
}, [userID, chatID, setChatId]);

if (isLoading) {
// return <LoadingPage />;
Expand Down
15 changes: 15 additions & 0 deletions src/pages/registration/LoginSignupForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ body {
width: 100%;
}

.password {
display: flex;
align-items: center;
position: relative;
width: 100%;
}

.password p {
position: absolute;
right: 0;
cursor: pointer;
padding-top: 14px;
padding-right: 10px;
}

.container {
background-color: #fff;
border-radius: 10px;
Expand Down
Loading

0 comments on commit f553f22

Please sign in to comment.