add message controller to wrap methods
This commit is contained in:
parent
7fa328d1f2
commit
67c5edd6cb
|
|
@ -33,6 +33,7 @@ export default () => {
|
|||
<QueryClientProvider client={queryClient}>
|
||||
<CookiesProvider>
|
||||
<Routes>
|
||||
<Route path="/" element={<Auth route="home"/>} />
|
||||
<Route path="/login" element={<Auth route="login"/>} />
|
||||
<Route path="/signup" element={<Auth route="signup"/>} />
|
||||
<Route path="/home" element={<Auth route="home"/>} />
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
Loading…
Reference in New Issue