add message controller to wrap methods

This commit is contained in:
Guillaume Dorce 2022-09-28 15:09:40 +02:00
parent 7fa328d1f2
commit 67c5edd6cb
2 changed files with 70 additions and 0 deletions

View File

@ -33,6 +33,7 @@ export default () => {
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<CookiesProvider> <CookiesProvider>
<Routes> <Routes>
<Route path="/" element={<Auth route="home"/>} />
<Route path="/login" element={<Auth route="login"/>} /> <Route path="/login" element={<Auth route="login"/>} />
<Route path="/signup" element={<Auth route="signup"/>} /> <Route path="/signup" element={<Auth route="signup"/>} />
<Route path="/home" element={<Auth route="home"/>} /> <Route path="/home" element={<Auth route="home"/>} />

View File

@ -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 };