Skip to content

Commit

Permalink
Refined trash mutation hook to use Axios and Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff McMillen authored and Jeff McMillen committed Dec 5, 2024
1 parent b46e99a commit 3796d48
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions client/src/hooks/datafiles/mutations/useTrash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useSelector, useDispatch, shallowEqual } from 'react-redux';
import { useMutation } from '@tanstack/react-query';
import { useSelectedFiles } from 'hooks/datafiles';
import Cookies from 'js-cookie';
import { apiClient } from 'utils/apiClient';

export async function trashUtil({
api,
Expand All @@ -17,23 +18,26 @@ export async function trashUtil({
homeDir: string;
}) {
const url = `/api/datafiles/${api}/trash/${scheme}/${system}/${path}/`;
// const request = await apiClient.put(url, body, {
// headers: {
// 'X-CSRFToken': Cookies.get('csrftoken' || ''),
// },
// withCredentials: true,
// });
const request = await fetch(url, {
method: 'PUT',
headers: { 'X-CSRFToken': Cookies.get('csrftoken') || '' },
credentials: 'same-origin',
body: JSON.stringify({
homeDir: homeDir,
}),
const body = {
homeDir: homeDir
};
const response = await apiClient.put(url, body, {
headers: {
'X-CSRFToken': Cookies.get('csrftoken' || ''),
},
withCredentials: true,
});
// const request = await fetch(url, {
// method: 'PUT',
// headers: { 'X-CSRFToken': Cookies.get('csrftoken') || '' },
// credentials: 'same-origin',
// body: JSON.stringify({
// homeDir: homeDir,
// }),
// });
// const request = await apiClient.put(url, body);

return request;
return response.data;
}

function useTrash() {
Expand Down Expand Up @@ -69,7 +73,7 @@ function useTrash() {
const filteredSelected = selected.filter(
(f: any) => status[f.id] !== 'SUCCESS'
);
const trashCalls: Promise<any>[] = filteredSelected.forEach((file: any) => {
const trashCalls: Promise<any>[] = filteredSelected.map((file: any) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
Expand All @@ -96,12 +100,7 @@ function useTrash() {
operation: 'trash',
},
});
dispatch({
type: 'ADD_TOAST',
payload: {
message: `File moved to Trash`,
},
});

callback();
},
onError: () => {
Expand All @@ -117,6 +116,18 @@ function useTrash() {
}
);
});
Promise.all(trashCalls).then(() => {
dispatch({
type: 'ADD_TOAST',
payload: {
message: `${
filteredSelected.length > 1
? `${filteredSelected.length} files moved to Trash`
: 'File moved to Trash'
}`,
},
});
});
};

return { trash, status, setStatus };
Expand Down

0 comments on commit 3796d48

Please sign in to comment.