diff --git a/src/pages/Volunteers/VolunteersPage.jsx b/src/pages/Volunteers/VolunteersPage.jsx index 366dda7..a98a647 100644 --- a/src/pages/Volunteers/VolunteersPage.jsx +++ b/src/pages/Volunteers/VolunteersPage.jsx @@ -16,8 +16,8 @@ function VolunteerPage() { useEffect(() => { (async () => { - const ids = await getAllUser(); - const usersData = await Promise.all(ids.map(id => getUserById(id))); + const result = await getAllUser(); + const usersData = await Promise.all(result.data.map(id => getUserById(id))); setUsers(usersData); })() }, []) diff --git a/src/utils/users/deactivateUser.js b/src/utils/users/deactivateUser.js index 61ac0c7..fdf4c44 100644 --- a/src/utils/users/deactivateUser.js +++ b/src/utils/users/deactivateUser.js @@ -1,34 +1,13 @@ +import fetchWrapper from "../fetchWrapper.js"; import getXSRFToken from "../getXSRF.js"; export default async function deactivateUser(userId) { const csrfToken = await getXSRFToken(); - try { - const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users/${userId}/deactivate`, { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - 'X-XSRF-TOKEN': csrfToken, - 'Accept': 'application/json' - }, - body: JSON.stringify({ - id: userId - }) - }); - - const data = await response.json(); - - if (!response.ok) { - return { - status: response.status, - errors: data.errors || data.message - }; - } - - return { status: response.status, data }; - - } catch (error) { - return { status: 500, error }; - } + return fetchWrapper( + `/api/users/${userId}/deactivate`, + { id: userId }, + "POST", + { "X-XSRF-TOKEN": csrfToken } + ); } diff --git a/src/utils/users/deleteOtherUser.js b/src/utils/users/deleteOtherUser.js index 101cf94..ece6425 100644 --- a/src/utils/users/deleteOtherUser.js +++ b/src/utils/users/deleteOtherUser.js @@ -1,34 +1,13 @@ +import fetchWrapper from "../fetchWrapper.js"; import getXSRFToken from "../getXSRF.js"; export default async function deleteOtherUser(userId) { const csrfToken = await getXSRFToken(); - try { - const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users/${userId}`, { - method: 'DELETE', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - 'X-XSRF-TOKEN': csrfToken, - 'Accept': 'application/json' - }, - body: JSON.stringify({ - id: userId - }) - }); - - const data = await response.json(); - - if (!response.ok) { - return { - status: response.status, - errors: data.errors || data.message - }; - } - - return { status: response.status, data }; - - } catch (error) { - return { status: 500, error }; - } + return fetchWrapper( + `/api/users/${userId}`, + { id: userId }, + "DELETE", + { "X-XSRF-TOKEN": csrfToken } + ); } diff --git a/src/utils/users/deleteUser.js b/src/utils/users/deleteUser.js index 932167d..4cb9dcf 100644 --- a/src/utils/users/deleteUser.js +++ b/src/utils/users/deleteUser.js @@ -1,31 +1,13 @@ +import fetchWrapper from "../fetchWrapper.js"; import getXSRFToken from "../getXSRF.js"; export default async function deleteUser() { const csrfToken = await getXSRFToken(); - try { - const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users`, { - method: 'DELETE', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - 'X-XSRF-TOKEN': csrfToken, - 'Accept': 'application/json' - } - }); - - const data = await response.json(); - - if (!response.ok) { - return { - status: response.status, - errors: data.errors || data.message - }; - } - - return { status: response.status, data }; - - } catch (error) { - return { status: 500, error }; - } + return fetchWrapper( + "/api/users", + null, + "DELETE", + { "X-XSRF-TOKEN": csrfToken } + ); } diff --git a/src/utils/users/getAllUsers.js b/src/utils/users/getAllUsers.js index b31ff18..584c39f 100644 --- a/src/utils/users/getAllUsers.js +++ b/src/utils/users/getAllUsers.js @@ -1,21 +1,5 @@ +import fetchWrapper from "../fetchWrapper.js"; + export default async function getAllUser() { - try { - const res = await fetch(`${import.meta.env.VITE_API_URL}/api/users`, { - method: 'GET', - credentials: 'include', - headers: { - 'Accept': 'application/json' - } - }); - - if (!res.ok) { - throw new Error(`Erreur serveur : ${res.status}`); - } - - const data = await res.json(); - return data; - } catch (err) { - console.error('Erreur :', err); - return null; - } + return fetchWrapper("/api/users"); }