Send Messages in chat

TODO:
- use socket to reload
- use Redux do avoid props drilling

Took 1 hour 44 minutes
This commit is contained in:
Lucàs
2023-04-02 23:50:46 +02:00
parent 2dfe2cac7b
commit 7f48ea2964
8 changed files with 3531 additions and 3456 deletions
+34
View File
@@ -0,0 +1,34 @@
import {Button, Flex, Input} from '@chakra-ui/react';
import {useState} from 'react';
import {User} from '@prisma/client';
type Props = {
user: User,
chatId: Props
}
export default function FormMessage(props: Props) {
const {user, chatId} = 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))
}
}
return (
<Flex gap={5} mt={5}>
<Input type={text} colorScheme={'purple'} onChange={(evt) => setText(evt.target.value) } value={text} />
<Button colorScheme={'purple'} onClick={handleSubmit} >Envoyer</Button>
</Flex>
)
}