mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-12-05 17:30:50 +00:00
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:
@@ -1,18 +0,0 @@
|
||||
import {Card, Text, Flex} from '@chakra-ui/react';
|
||||
|
||||
type Props = {
|
||||
sender: 'me' | 'other'
|
||||
message: string
|
||||
}
|
||||
|
||||
export default function Message(props: Props) {
|
||||
const {align, message} = props;
|
||||
|
||||
return (
|
||||
<Flex alignItems={align === "me" ? 'end' : "start"}>
|
||||
<Card display={'flex'} px={2} py={1} bg={'purple.500'}>
|
||||
<Text>{message}</Text>
|
||||
</Card>
|
||||
</Flex>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import {Box, Flex, Text} from '@chakra-ui/react';
|
||||
import type {Message as MessageType, User} from "@prisma/client"
|
||||
|
||||
|
||||
type Props = {
|
||||
user: User
|
||||
message: MessageType
|
||||
}
|
||||
|
||||
export default function Message(props: Props) {
|
||||
const {message, user} = props;
|
||||
|
||||
return (
|
||||
<Flex justifyContent={user.id === message.UserID ? 'right' : 'left'}>
|
||||
<Text w={'fit-content'} bg={'purple.500'} borderRadius={'full'} color={'white'} px={"10px"} py={"5px"}>{message.text}</Text>
|
||||
</Flex>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type {Message as MessageType, User} from "@prisma/client";
|
||||
import Message from '@/components/chat/Message';
|
||||
import {Box, Flex} from '@chakra-ui/react';
|
||||
|
||||
type Props = {
|
||||
user: User
|
||||
messages: MessageType[]
|
||||
}
|
||||
|
||||
export default function MessageList(props: Props) {
|
||||
const {messages, user} = props;
|
||||
|
||||
return (
|
||||
<Flex direction={'column'} gap={1}>
|
||||
{messages.map((message, index) => <Message key={index} user={user} message={message}/>)}
|
||||
</Flex>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user