mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 16:03:21 +00:00
refactor: Transformation des requetes vers le back en action ajout des
erreurs 400 et refresh lors d'un envoie de formulaire
This commit is contained in:
@ -12,6 +12,20 @@ import {mockUser} from "@/data/mockUsersData";
|
||||
|
||||
const useFakeData = process.env.NEXT_PUBLIC_USE_FAKE_DATA === 'true';
|
||||
|
||||
|
||||
const requestResponseHandler = async (response) => {
|
||||
|
||||
const body = await response.json();
|
||||
if (response.ok) {
|
||||
return body;
|
||||
}
|
||||
// Throw an error with the JSON body containing the form errors
|
||||
const error = new Error('Form submission error');
|
||||
error.details = body;
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
export const login = (data, csrfToken) => {
|
||||
const request = new Request(
|
||||
`${BE_AUTH_LOGIN_URL}`,
|
||||
@ -25,7 +39,7 @@ export const login = (data, csrfToken) => {
|
||||
credentials: 'include',
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,7 +78,7 @@ const request = new Request(
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const updateProfile = (id, data, csrfToken) => {
|
||||
@ -80,7 +94,7 @@ export const updateProfile = (id, data, csrfToken) => {
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const sendNewPassword = (data, csrfToken) => {
|
||||
@ -97,7 +111,7 @@ export const sendNewPassword = (data, csrfToken) => {
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const subscribe = (data,csrfToken) =>{
|
||||
@ -113,7 +127,7 @@ export const subscribe = (data,csrfToken) =>{
|
||||
body: JSON.stringify( data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const resetPassword = (uuid, data, csrfToken) => {
|
||||
@ -129,7 +143,7 @@ export const resetPassword = (uuid, data, csrfToken) => {
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const getResetPassword = (uuid) => {
|
||||
@ -138,5 +152,5 @@ export const getResetPassword = (uuid) => {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then(response => response.json())
|
||||
}).then(requestResponseHandler)
|
||||
}
|
||||
@ -5,22 +5,35 @@ import {
|
||||
BE_SCHOOL_PLANNINGS_URL
|
||||
} from '@/utils/Url';
|
||||
|
||||
const requestResponseHandler = async (response) => {
|
||||
|
||||
const body = await response.json();
|
||||
if (response.ok) {
|
||||
return body;
|
||||
}
|
||||
// Throw an error with the JSON body containing the form errors
|
||||
const error = new Error('Form submission error');
|
||||
error.details = body;
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
export const fetchSpecialities = () => {
|
||||
return fetch(`${BE_SCHOOL_SPECIALITIES_URL}`)
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
};
|
||||
|
||||
export const fetchTeachers = () => {
|
||||
return fetch(`${BE_SCHOOL_TEACHERS_URL}`)
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
};
|
||||
|
||||
export const fetchClasses = () => {
|
||||
return fetch(`${BE_SCHOOL_SCHOOLCLASSES_URL}`)
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
};
|
||||
|
||||
export const fetchSchedules = () => {
|
||||
return fetch(`${BE_SCHOOL_PLANNINGS_URL}`)
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
};
|
||||
@ -6,7 +6,8 @@ import {
|
||||
BE_SUBSCRIPTION_CHILDRENS_URL,
|
||||
BE_SUBSCRIPTION_REGISTERFORM_URL,
|
||||
BE_SUBSCRIPTION_REGISTERFORMS_URL,
|
||||
BE_SUBSCRIPTION_REGISTERFORMFILE_TEMPLATE_URL
|
||||
BE_SUBSCRIPTION_REGISTRATIONFORMFILE_TEMPLATE_URL,
|
||||
BE_SUBSCRIPTION_LAST_GUARDIAN_URL
|
||||
} from '@/utils/Url';
|
||||
|
||||
export const PENDING = 'pending';
|
||||
@ -14,6 +15,18 @@ export const SUBSCRIBED = 'subscribed';
|
||||
export const ARCHIVED = 'archived';
|
||||
|
||||
|
||||
const requestResponseHandler = async (response) => {
|
||||
|
||||
const body = await response.json();
|
||||
if (response.ok) {
|
||||
return body;
|
||||
}
|
||||
// Throw an error with the JSON body containing the form errors
|
||||
const error = new Error('Form submission error');
|
||||
error.details = body;
|
||||
throw error;
|
||||
}
|
||||
|
||||
export const fetchRegisterForms = (type=PENDING, page='', pageSize='', search = '') => {
|
||||
let url = `${BE_SUBSCRIPTION_REGISTERFORMS_URL}/${type}`;
|
||||
if (page !== '' && pageSize !== '') {
|
||||
@ -23,12 +36,16 @@ export const fetchRegisterForms = (type=PENDING, page='', pageSize='', search =
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then(response => response.json())
|
||||
}).then(requestResponseHandler)
|
||||
};
|
||||
|
||||
export const fetchRegisterForm = (id) =>{
|
||||
return fetch(`${BE_SUBSCRIPTION_STUDENT_URL}/${id}`) // Utilisation de studentId au lieu de codeDI
|
||||
.then(response => response.json())
|
||||
return fetch(`${BE_SUBSCRIPTION_STUDENT_URL}/${id}`) // Utilisation de studentId au lieu de codeDI
|
||||
.then(requestResponseHandler)
|
||||
}
|
||||
export const fetchLastGuardian = () =>{
|
||||
return fetch(`${BE_SUBSCRIPTION_LAST_GUARDIAN_URL}`)
|
||||
.then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const editRegisterForm=(id, data, csrfToken)=>{
|
||||
@ -42,7 +59,7 @@ export const editRegisterForm=(id, data, csrfToken)=>{
|
||||
body: JSON.stringify(data),
|
||||
credentials: 'include'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
|
||||
};
|
||||
|
||||
@ -57,7 +74,7 @@ export const createRegisterForm=(data, csrfToken)=>{
|
||||
body: JSON.stringify(data),
|
||||
credentials: 'include'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const archiveRegisterForm = (id) => {
|
||||
@ -67,7 +84,7 @@ export const archiveRegisterForm = (id) => {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then(response => response.json())
|
||||
}).then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const sendRegisterForm = (id) => {
|
||||
@ -76,13 +93,13 @@ export const sendRegisterForm = (id) => {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then(response => response.json())
|
||||
}).then(requestResponseHandler)
|
||||
|
||||
}
|
||||
|
||||
export const fetchRegisterFormFileTemplate = () => {
|
||||
const request = new Request(
|
||||
`${BE_SUBSCRIPTION_REGISTERFORMFILE_TEMPLATE_URL}`,
|
||||
`${BE_SUBSCRIPTION_REGISTRATIONFORMFILE_TEMPLATE_URL}`,
|
||||
{
|
||||
method:'GET',
|
||||
headers: {
|
||||
@ -90,12 +107,12 @@ export const fetchRegisterFormFileTemplate = () => {
|
||||
},
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
};
|
||||
|
||||
export const createRegisterFormFileTemplate = (data,csrfToken) => {
|
||||
export const createRegistrationFormFileTemplate = (data,csrfToken) => {
|
||||
|
||||
fetch(`${BE_SUBSCRIPTION_REGISTERFORMFILE_TEMPLATE_URL}`, {
|
||||
return fetch(`${BE_SUBSCRIPTION_REGISTRATIONFORMFILE_TEMPLATE_URL}`, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
headers: {
|
||||
@ -103,11 +120,11 @@ export const createRegisterFormFileTemplate = (data,csrfToken) => {
|
||||
},
|
||||
credentials: 'include',
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(requestResponseHandler)
|
||||
}
|
||||
|
||||
export const deleteRegisterFormFileTemplate = (fileId,csrfToken) => {
|
||||
return fetch(`${BE_SUBSCRIPTION_REGISTERFORMFILE_TEMPLATE_URL}/${fileId}`, {
|
||||
return fetch(`${BE_SUBSCRIPTION_REGISTRATIONFORMFILE_TEMPLATE_URL}/${fileId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'X-CSRFToken': csrfToken,
|
||||
@ -125,7 +142,7 @@ export const fetchStudents = () => {
|
||||
},
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
|
||||
};
|
||||
|
||||
@ -139,5 +156,5 @@ export const fetchChildren = (id) =>{
|
||||
},
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
return fetch(request).then(requestResponseHandler)
|
||||
}
|
||||
Reference in New Issue
Block a user