diff --git a/client/src/components/Message.tsx b/client/src/components/Message.tsx index 1b551b2..8f7208b 100644 --- a/client/src/components/Message.tsx +++ b/client/src/components/Message.tsx @@ -3,7 +3,7 @@ import Avatar from '@components/Avatar'; import PopupMessage from './PopupMessage'; const Image = ({ image }: {image: string}) => { - if (image === '') { + if (image === '' || image === null) { return null; } @@ -33,7 +33,7 @@ const Message = ({ text = '', user, date, image = '' }: any) => { const id = '1'; // replace with real id in the future return ( <> -
+
{user && }
diff --git a/client/src/components/MessageWrapper.tsx b/client/src/components/MessageWrapper.tsx new file mode 100644 index 0000000..a3b87c7 --- /dev/null +++ b/client/src/components/MessageWrapper.tsx @@ -0,0 +1,45 @@ +import { useQuery } from "@tanstack/react-query"; +import { Cookies } from "react-cookie"; +import Message from "./Message"; + +const getMessages = async () => { + const token = new Cookies().get("token"); + const response = await fetch("/api/posts", { + method: "GET", + mode: "cors", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }); + return response.json(); +}; + +const MessageWrapper = () => { + const messages = useQuery(["messages"], getMessages, { + onSuccess: (data) => { + console.log(data); + + return data; + }, + onError: (error) => { + console.error(error); + }, + }); + + const user = { + firstName: 'Guillaume', + lastName: 'Dorce', + email: 'guillaume.dorce@bm-services.com', + }; + + return ( +
+ {messages.isLoading ? '' : messages.data?.map((message: any) => ( + + ))} +
+ ); +} + +export default MessageWrapper; \ No newline at end of file diff --git a/client/src/components/NewMessage.tsx b/client/src/components/NewMessage.tsx index 6406ac8..f8c95c3 100644 --- a/client/src/components/NewMessage.tsx +++ b/client/src/components/NewMessage.tsx @@ -1,11 +1,40 @@ -import { useMutation, useQuery } from '@tanstack/react-query'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useState } from 'react'; +import { Cookies } from 'react-cookie'; + +const sendMessage = async (message: string) => { + const token = new Cookies().get('token'); + const response = await fetch('/api/posts/new', { + method: 'POST', + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ content: message }), + }); + return response.json(); +}; + + const NewMessage = () => { const [message, setMessage] = useState(''); + const queryClient = useQueryClient(); + + const { mutate: send } = useMutation(sendMessage, { + onSuccess: (data) => { + console.log(data); + queryClient.invalidateQueries(['messages']); + }, + onError: (error) => { + console.error(error); + }, + }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); + send(message); setMessage(''); }; diff --git a/client/src/controllers/Messages.tsx b/client/src/controllers/Messages.tsx new file mode 100644 index 0000000..e69de29 diff --git a/client/src/routes/home.tsx b/client/src/routes/home.tsx index 2df3d45..62afb57 100644 --- a/client/src/routes/home.tsx +++ b/client/src/routes/home.tsx @@ -1,37 +1,12 @@ import AppHeader from '@components/AppHeader'; -import Message from '@components/Message'; +import MessageWrapper from '@components/MessageWrapper'; import NewMessage from '@components/NewMessage'; const Home = () => { - const user = { - firstName: 'Guillaume', - lastName: 'Dorce', - email: 'guillaume.dorce@bm-services.com', - }; - return (
-
- - - -
+
);