Create event function
This commit is contained in:
1 parent
9cd56a8472
commit
67b02c297f
1 file changed
+36
@@ -0,0 +1,36 @@
|
|||||||
|
import getXSRFToken from "../getXSRF.js";
|
||||||
|
|
||||||
|
export default async function createEvent(name, description) {
|
||||||
|
const csrfToken = await getXSRFToken();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`http://${import.meta.env.VITE_API_URL}/api/events/create`, {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-XSRF-TOKEN': csrfToken,
|
||||||
|
'Accept': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ name, description }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
if (response.status === 422) {
|
||||||
|
return {
|
||||||
|
status: 422,
|
||||||
|
errors: data.errors
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Erreur HTTP ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: response.status, data };
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
return { status: "error", error };
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user