Commit f4716df8 authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

Chat Box UI

parent fda4e3ac
...@@ -4,11 +4,13 @@ import { ChatState } from "../Context/ChatProvider"; ...@@ -4,11 +4,13 @@ import { ChatState } from "../Context/ChatProvider";
import SideDrawer from "../components/miscellaneous/SideDrawer"; import SideDrawer from "../components/miscellaneous/SideDrawer";
import MyChats from "../components/MyChats"; import MyChats from "../components/MyChats";
import ChatBox from "../components/ChatBox"; import ChatBox from "../components/ChatBox";
import { useState } from "react";
const ChatPage = () => { const ChatPage = () => {
const { user } = ChatState(); const { user } = ChatState();
const [fetchAgain, setFetchAgain] = useState(false);
return ( return (
...@@ -21,8 +23,10 @@ const ChatPage = () => { ...@@ -21,8 +23,10 @@ const ChatPage = () => {
height="91.5vh" height="91.5vh"
padding="10px" padding="10px"
> >
{user && <MyChats />} {user && <MyChats fetchAgain={fetchAgain} />}
{user && <ChatBox />} {user && (
<ChatBox fetchAgain={fetchAgain} setFetchAgain={setFetchAgain} />
)}
</Box> </Box>
</div> </div>
......
import React from "react"; import React from "react";
import { ChatState } from "../Context/ChatProvider";
import { Box } from "@chakra-ui/react";
import SingleChat from "./SingleChat";
const ChatBox = ({fetchAgain, setFetchAgain}) => {
const { selectedChat } = ChatState();
const ChatBox = () => {
return ( return (
<div>ChatBox</div> <Box
display={{ base: selectedChat ? "flex" : "none", md: "flex" }}
alignItems="center"
flexDir="column"
p={3}
bg="white"
w={{ base: "100%", md: "68%" }}
borderRadius="lg"
borderWidth="1px"
>
<SingleChat fetchAgain={fetchAgain} setFetchAgain={ setFetchAgain}/>
</Box>
); );
}; };
......
...@@ -7,7 +7,7 @@ import ChatLoading from "./ChatLoading"; ...@@ -7,7 +7,7 @@ import ChatLoading from "./ChatLoading";
import { getSender } from "../config/ChatLogics"; import { getSender } from "../config/ChatLogics";
import GroupChatModal from "./miscellaneous/GroupChatModal"; import GroupChatModal from "./miscellaneous/GroupChatModal";
const MyChats = () => { const MyChats = ({fetchAgain}) => {
const [loggedUser, setLoggedUser] = useState(); const [loggedUser, setLoggedUser] = useState();
const { user, selectedChat, setSelectedChat, chats, setChats } = ChatState(); const { user, selectedChat, setSelectedChat, chats, setChats } = ChatState();
...@@ -42,7 +42,7 @@ const MyChats = () => { ...@@ -42,7 +42,7 @@ const MyChats = () => {
useEffect(() => { useEffect(() => {
setLoggedUser(JSON.parse(localStorage.getItem("userInfo"))); setLoggedUser(JSON.parse(localStorage.getItem("userInfo")));
fetchChats(); fetchChats();
},[]) }, [fetchAgain]);
return ( return (
<Box <Box
......
import { Box, IconButton, Text } from "@chakra-ui/react";
import React from "react";
import { ChatState } from "../Context/ChatProvider";
import { ArrowBackIcon } from "@chakra-ui/icons";
import { getSender, getSenderFull } from "../config/ChatLogics";
import ProfileModal from "./miscellaneous/ProfileModal";
const SingleChat = ({ fetchAgain, setFetchAgain }) => {
const { user, selectedChat, setSelectedChat } = ChatState();
return (
<>
{selectedChat ? (
<>
<Text
fontSize={{ base: "28px", md: "30px" }}
pb={3}
px={2}
w="100%"
fontFamily="Work sans"
display="flex"
justifyContent={{ base: "space-between" }}
alignItems="center"
>
<IconButton
display={{ base: "flex", md: "none" }}
icon={<ArrowBackIcon />}
onClick={()=>setSelectedChat("")}
/>
{!selectedChat.isGroupChat ? (
<>
{getSender(user, selectedChat.users)}
<ProfileModal user={ getSenderFull(user, selectedChat.users)}/>
</>
) : (
<>
{selectedChat.chatName.toUpperCase()}
{/* <UpdateGroupChatModal
fetchAgain={fetchAgain}
setFetchAgain={setSelectedChat}
/>*/ }
</>
)}
</Text>
</>
) : (
<Box display="flex" alignItems="center" justifyContent="center" height="100%">
<Text fontSize="3xl" pb={3} fontFamily="Work sans" >
Click on a user to start chatting
</Text>
</Box>
)}
</>
);
};
export default SingleChat;
\ No newline at end of file
...@@ -66,15 +66,15 @@ const GroupChatModal = ({ children }) => { ...@@ -66,15 +66,15 @@ const GroupChatModal = ({ children }) => {
try { try {
const config = { const config = {
headers: { headers: {
Authorization: `Bearer ${user.token}`, Authorization : `Bearer ${user.token}`,
}, },
}; };
const { data } = await axios.post( const { data } = await axios.post(
"/api/chat/group", "/api/chat/group",
{ {
name: groupChatName, name:groupChatName,
users: JSON.stringify(selectedUsers.map((u) => u._id)), users:JSON.stringify(selectedUsers.map((u) => u._id)),
}, },
config config
); );
...@@ -178,7 +178,7 @@ const GroupChatModal = ({ children }) => { ...@@ -178,7 +178,7 @@ const GroupChatModal = ({ children }) => {
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
<Button colorScheme="blue" onClick={handleSubmit}> <Button onClick={handleSubmit} colorScheme="blue" >
Create Organization Create Organization
</Button> </Button>
</ModalFooter> </ModalFooter>
......
...@@ -2,3 +2,8 @@ export const getSender = (loggedUser, users) => { ...@@ -2,3 +2,8 @@ export const getSender = (loggedUser, users) => {
return users[0]._id === loggedUser._id ? users[1].name : users[0].name; return users[0]._id === loggedUser._id ? users[1].name : users[0].name;
}; };
export const getSenderFull = (loggedUser, users) => {
return users[0]._id === loggedUser._id ? users[1] : users[0];
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment