diff --git a/client/src/hooks/datafiles/mutations/useTrash.ts b/client/src/hooks/datafiles/mutations/useTrash.ts index 6d9d4a143..6a5454614 100644 --- a/client/src/hooks/datafiles/mutations/useTrash.ts +++ b/client/src/hooks/datafiles/mutations/useTrash.ts @@ -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, @@ -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() { @@ -69,7 +73,7 @@ function useTrash() { const filteredSelected = selected.filter( (f: any) => status[f.id] !== 'SUCCESS' ); - const trashCalls: Promise[] = filteredSelected.forEach((file: any) => { + const trashCalls: Promise[] = filteredSelected.map((file: any) => { dispatch({ type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY', payload: { @@ -96,12 +100,7 @@ function useTrash() { operation: 'trash', }, }); - dispatch({ - type: 'ADD_TOAST', - payload: { - message: `File moved to Trash`, - }, - }); + callback(); }, onError: () => { @@ -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 };