use fetchWrapper for users routes
This commit is contained in:
1 parent
a0bba2deb4
commit
79e765dd96
5 files changed
+26
-102
No files matched your search
@@ -16,8 +16,8 @@ function VolunteerPage() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
const ids = await getAllUser();
|
const result = await getAllUser();
|
||||||
const usersData = await Promise.all(ids.map(id => getUserById(id)));
|
const usersData = await Promise.all(result.data.map(id => getUserById(id)));
|
||||||
setUsers(usersData);
|
setUsers(usersData);
|
||||||
})()
|
})()
|
||||||
}, [])
|
}, [])
|
||||||
|
|||||||
@@ -1,34 +1,13 @@
|
|||||||
|
import fetchWrapper from "../fetchWrapper.js";
|
||||||
import getXSRFToken from "../getXSRF.js";
|
import getXSRFToken from "../getXSRF.js";
|
||||||
|
|
||||||
export default async function deactivateUser(userId) {
|
export default async function deactivateUser(userId) {
|
||||||
const csrfToken = await getXSRFToken();
|
const csrfToken = await getXSRFToken();
|
||||||
|
|
||||||
try {
|
return fetchWrapper(
|
||||||
const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users/${userId}/deactivate`, {
|
`/api/users/${userId}/deactivate`,
|
||||||
method: 'POST',
|
{ id: userId },
|
||||||
credentials: 'include',
|
"POST",
|
||||||
headers: {
|
{ "X-XSRF-TOKEN": csrfToken }
|
||||||
'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 };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,34 +1,13 @@
|
|||||||
|
import fetchWrapper from "../fetchWrapper.js";
|
||||||
import getXSRFToken from "../getXSRF.js";
|
import getXSRFToken from "../getXSRF.js";
|
||||||
|
|
||||||
export default async function deleteOtherUser(userId) {
|
export default async function deleteOtherUser(userId) {
|
||||||
const csrfToken = await getXSRFToken();
|
const csrfToken = await getXSRFToken();
|
||||||
|
|
||||||
try {
|
return fetchWrapper(
|
||||||
const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users/${userId}`, {
|
`/api/users/${userId}`,
|
||||||
method: 'DELETE',
|
{ id: userId },
|
||||||
credentials: 'include',
|
"DELETE",
|
||||||
headers: {
|
{ "X-XSRF-TOKEN": csrfToken }
|
||||||
'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 };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,31 +1,13 @@
|
|||||||
|
import fetchWrapper from "../fetchWrapper.js";
|
||||||
import getXSRFToken from "../getXSRF.js";
|
import getXSRFToken from "../getXSRF.js";
|
||||||
|
|
||||||
export default async function deleteUser() {
|
export default async function deleteUser() {
|
||||||
const csrfToken = await getXSRFToken();
|
const csrfToken = await getXSRFToken();
|
||||||
|
|
||||||
try {
|
return fetchWrapper(
|
||||||
const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users`, {
|
"/api/users",
|
||||||
method: 'DELETE',
|
null,
|
||||||
credentials: 'include',
|
"DELETE",
|
||||||
headers: {
|
{ "X-XSRF-TOKEN": csrfToken }
|
||||||
'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 };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,5 @@
|
|||||||
|
import fetchWrapper from "../fetchWrapper.js";
|
||||||
|
|
||||||
export default async function getAllUser() {
|
export default async function getAllUser() {
|
||||||
try {
|
return fetchWrapper("/api/users");
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user