mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
119 lines
3.1 KiB
JavaScript
119 lines
3.1 KiB
JavaScript
import { BE_PLANNING_PLANNINGS_URL, BE_PLANNING_EVENTS_URL } from '@/utils/Url';
|
|
|
|
const requestResponseHandler = async (response) => {
|
|
const body = response.status !== 204 ? await response?.json() : {};
|
|
if (response.ok) {
|
|
return body;
|
|
}
|
|
// Throw an error with the JSON body containing the form errors
|
|
const error = new Error(
|
|
body?.errorMessage ||
|
|
`Une erreur est survenue code de retour : ${response.status}`
|
|
);
|
|
error.details = body;
|
|
throw error;
|
|
};
|
|
|
|
const getData = (url) => {
|
|
return fetch(`${url}`).then(requestResponseHandler);
|
|
};
|
|
|
|
const createDatas = (url, newData, csrfToken) => {
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRFToken': csrfToken,
|
|
},
|
|
body: JSON.stringify(newData),
|
|
credentials: 'include',
|
|
}).then(requestResponseHandler);
|
|
};
|
|
|
|
const updateDatas = (url, updatedData, csrfToken) => {
|
|
return fetch(`${url}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRFToken': csrfToken,
|
|
},
|
|
body: JSON.stringify(updatedData),
|
|
credentials: 'include',
|
|
}).then(requestResponseHandler);
|
|
};
|
|
|
|
const removeDatas = (url, csrfToken) => {
|
|
return fetch(`${url}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRFToken': csrfToken,
|
|
},
|
|
credentials: 'include',
|
|
}).then(requestResponseHandler);
|
|
};
|
|
|
|
export const fetchPlannings = (establishment_id=null,planningMode=null) => {
|
|
let url = `${BE_PLANNING_PLANNINGS_URL}`;
|
|
if (establishment_id) {
|
|
url += `?establishment_id=${establishment_id}`;
|
|
}
|
|
if (planningMode) {
|
|
url += `&planning_mode=${planningMode}`;
|
|
}
|
|
return getData(url);
|
|
};
|
|
|
|
export const getPlanning = (id) => {
|
|
return getData(`${BE_PLANNING_PLANNINGS_URL}/${id}`);
|
|
};
|
|
|
|
export const createPlanning = (newData, csrfToken) => {
|
|
return createDatas(`${BE_PLANNING_PLANNINGS_URL}`, newData, csrfToken);
|
|
};
|
|
|
|
export const updatePlanning = (id, newData, csrfToken) => {
|
|
return updateDatas(`${BE_PLANNING_PLANNINGS_URL}/${id}`, newData, csrfToken);
|
|
};
|
|
|
|
export const deletePlanning = (id, csrfToken) => {
|
|
return removeDatas(`${BE_PLANNING_PLANNINGS_URL}/${id}`, csrfToken);
|
|
};
|
|
|
|
export const fetchEvents = (establishment_id=null, planningMode=null) => {
|
|
let url = `${BE_PLANNING_EVENTS_URL}`;
|
|
if (establishment_id) {
|
|
url += `?establishment_id=${establishment_id}`;
|
|
}
|
|
if (planningMode) {
|
|
url += `&planning_mode=${planningMode}`;
|
|
}
|
|
|
|
return getData(url);
|
|
|
|
};
|
|
|
|
export const getEvent = (id) => {
|
|
return getData(`${BE_PLANNING_EVENTS_URL}/${id}`);
|
|
};
|
|
|
|
export const createEvent = (newData, csrfToken) => {
|
|
return createDatas(`${BE_PLANNING_EVENTS_URL}`, newData, csrfToken);
|
|
};
|
|
|
|
export const updateEvent = (id, newData, csrfToken) => {
|
|
return updateDatas(`${BE_PLANNING_EVENTS_URL}/${id}`, newData, csrfToken);
|
|
};
|
|
|
|
export const deleteEvent = (id, csrfToken) => {
|
|
return removeDatas(`${BE_PLANNING_EVENTS_URL}/${id}`, csrfToken);
|
|
};
|
|
|
|
export const fetchUpcomingEvents = (establishment_id=null) => {
|
|
let url = `${BE_PLANNING_EVENTS_URL}/upcoming`;
|
|
if (establishment_id) {
|
|
url += `?establishment_id=${establishment_id}`;
|
|
}
|
|
return getData(`${url}`);
|
|
};
|