diff --git a/client/src/App.tsx b/client/src/App.tsx index bf4d0d3..dba1ee4 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -33,6 +33,7 @@ export default () => { + } /> } /> } /> } /> diff --git a/client/src/controllers/Message.tsx b/client/src/controllers/Message.tsx new file mode 100644 index 0000000..6288c29 --- /dev/null +++ b/client/src/controllers/Message.tsx @@ -0,0 +1,69 @@ +import { Cookies } from "react-cookie"; + +const getMessages = async () => { + const token = new Cookies().get('token'); + const response = await fetch('/api/posts', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}` + } + }); + const data = await response.json(); + return data; +}; + +const newMessage = async (data: FormData) => { + const token = new Cookies().get('token'); + let body; + if (data.get('image')) { + body = data; + } else { + body = JSON.stringify({ + content: data.get('content') + }); + } + + const response = await fetch('/api/posts/new', { + method: 'POST', + body, + mode: 'cors', + headers: { + 'Authorization': `Bearer ${token}` + }, + }); + return response.json(); +}; + +const deleteMessage = async (id: string) => { + const token = new Cookies().get('token'); + const response = await fetch(`/api/posts/${id}`, { + method: 'DELETE', + headers: { + 'Authorization': `Bearer ${token}` + }, + }); + return response.json(); +}; + +const editMessage = async (id: string, data: FormData) => { + const token = new Cookies().get('token'); + let body; + if (data.get('image')) { + body = data; + } else { + body = JSON.stringify({ + content: data.get('content') + }); + } + + const response = await fetch(`/api/posts/${id}`, { + method: 'PUT', + body, + headers: { + 'Authorization': `Bearer ${token}` + }, + }); + return response.json(); +}; + +export { getMessages, newMessage, deleteMessage, editMessage }; \ No newline at end of file