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

Send Messages Logic FrontEnd

parent 47d2ae3e
import { Box, FormControl, IconButton, Input, Spinner, Text } from "@chakra-ui/react";
import { Box, FormControl, IconButton, Input, Spinner, Text, useToast } from "@chakra-ui/react";
import React, { useState } from "react";
import { ChatState } from "../Context/ChatProvider";
import { ArrowBackIcon } from "@chakra-ui/icons";
import { getSender, getSenderFull } from "../config/ChatLogics";
import ProfileModal from "./miscellaneous/ProfileModal";
import UpdateGroupChatModal from "./miscellaneous/UpdateGroupChatModal";
import axios from "axios";
const SingleChat = ({ fetchAgain, setFetchAgain }) => {
......@@ -13,10 +14,52 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
const [loading, setLoading] = useState(false);
const [newMessage, setNewMessage] = useState();
const toast = useToast();
const { user, selectedChat, setSelectedChat } = ChatState();
const sendMessage = () => { };
const typingHandler = () => { };
const sendMessage = async(event) => {
if (event.key === "Enter" && newMessage) {
try {
const config = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${user.token}`,
},
};
setNewMessage("");
const { data } = await axios.post(
"/api/message",
{
content: newMessage,
chatId: selectedChat._id,
},
config
);
console.log(data);
setMessages([...messages, data]);
} catch (error) {
toast({
title: "Error Occured!",
description: "Failed to send the Message",
status: "error",
duration: 5000,
isClosable: true,
position: "bottom",
});
}
}
};
const typingHandler = (e) => {
setNewMessage(e.target.value);
//Typing Indicator Logic
};
return (
<>
......
......@@ -36,7 +36,7 @@ const GroupChatModal = ({ children }) => {
};
const { data } = await axios.get(`/api/user?search=${search}`, config);
console.log(data);
setLoading(false);
setSearchResult(data);
} catch (error) {
......
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