Send messages with sockets ❤️

Took 3 hours 51 minutes
This commit is contained in:
Lucàs
2023-04-03 16:35:25 +02:00
parent c591a0eb3d
commit c91f5f3dfb
13 changed files with 501 additions and 242 deletions
+16 -11
View File
@@ -1,27 +1,32 @@
import {Button, Flex, Input} from '@chakra-ui/react';
import {useState} from 'react';
import {User} from '@prisma/client';
import {io, Socket} from 'socket.io-client';
import Message from '@/components/chat/Message';
type Props = {
user: User,
chatId: Props
chatId: Props,
socket: any
}
export default function FormMessage(props: Props) {
const {user, chatId} = props;
const {user, socket} = props;
const [text, setText] = useState("");
const handleSubmit = () => {
if (text !== "") {
fetch("/api/messages", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
text,
'User': {"connect": {'id': user.id}},
'Chat': {"connect": {'id': chatId}}
})
}).then(res => res.json()).then(res => console.log(res)).catch(err => console.error(err))
socket.emit("createdMessage", {text, sender: user.id})
// fetch("/api/messages", {
// method: "POST",
// headers: {'Content-Type': 'application/json'},
// body: JSON.stringify({
// text,
// 'User': {"connect": {'id': user.id}},
// 'Chat': {"connect": {'id': chatId}}
// })
// }).then(res => res.json()).then(res => console.log(res)).catch(err => console.error(err))
}
}