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

Create Group Chat Modal Part 2

parent da4148ab
import React from "react";
import { Box } from "@chakra-ui/react";
import { CloseIcon } from "@chakra-ui/icons";
const UserBadgeItem = ({ user, handleFunction }) => {
return (
<Box
px={2}
py={1}
borderRadius="lg"
m={1}
mb={2}
variant="solid"
fontSize={12}
backgroundColor="purple"
color="white"
cursor="pointer"
onClick={handleFunction}
>
{user.name}
<CloseIcon pl={1}/>
</Box>
);
};
export default UserBadgeItem;
\ No newline at end of file
import React, { useState } from "react"; import React, { useState } from "react";
import { useDisclosure, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Button, useToast, FormControl, Input } from "@chakra-ui/react"; import { useDisclosure, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Button, useToast, FormControl, Input, Box } from "@chakra-ui/react";
import { ChatState } from "../../Context/ChatProvider"; import { ChatState } from "../../Context/ChatProvider";
import axios from "axios"; import axios from "axios";
import UserListItem from "../UserAvatar/UserListItem"; import UserListItem from "../UserAvatar/UserListItem";
import UserBadgeItem from "../UserAvatar/UserBadgeItem";
...@@ -50,9 +51,73 @@ const GroupChatModal = ({ children }) => { ...@@ -50,9 +51,73 @@ const GroupChatModal = ({ children }) => {
} }
}; };
const handleSubmit = () => { }; const handleSubmit = async () => {
if (!groupChatName || !selectedUsers) {
toast({
title: "Please fill all the feilds",
status: "warning",
duration: 5000,
isClosable: true,
position: "top",
});
return;
}
const handleGroup = () => { }; try {
const config = {
headers: {
Authorization: `Bearer ${user.token}`,
},
};
const { data } = await axios.post(
"/api/chat/group",
{
name: groupChatName,
users: JSON.stringify(selectedUsers.map((u) => u._id)),
},
config
);
setChats([data, ...chats]);
onClose();
toast({
title: "New Organization Chat Created!",
status: "success",
duration: 5000,
isClosable: true,
position:"bottom",
});
} catch (error) {
toast({
title: "Failed to Create the Organization Chat!",
description: error.response.data,
status: "error",
duration: 5000,
isClosable: true,
position:"bottom",
});
}
};
const handleDelete = (delUser) => {
setSelectedUsers(selectedUsers.filter((sel) => sel._id !== delUser._id));
};
const handleGroup = (userToAdd) => {
if (selectedUsers.includes(userToAdd)) {
toast({
title: "User already added",
status: "warning",
duration: 5000,
isClosable: true,
position: "top",
});
return;
}
setSelectedUsers([...selectedUsers, userToAdd]);
};
return ( return (
<> <>
...@@ -85,7 +150,16 @@ const GroupChatModal = ({ children }) => { ...@@ -85,7 +150,16 @@ const GroupChatModal = ({ children }) => {
onChange={(e) => handleSearch(e.target.value)} onChange={(e) => handleSearch(e.target.value)}
/> />
</FormControl> </FormControl>
{/* selected users */} <Box w="100%" display="flex" flexWrap="wrap">
{selectedUsers.map((u) => (
<UserBadgeItem
key={user._id}
user={u}
handleFunction={() => handleDelete(u)}
/>
))}
</Box>
{loading ? ( {loading ? (
<div>loading</div> <div>loading</div>
) : ( ) : (
......
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