Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/WP-731: Mutation hook: Trash file/folder #994

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@emotion/styled": "^11.13.0",
"@mui/material": "^6.1.1",
"@niivue/niivue": "^0.32.0",
"@tanstack/react-query": "^5.59.16",
"@tanstack/react-query": "^5.59.19",
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
"axios": "^1.7.7",
"bowser": "^2.9.0",
"cross-fetch": "^3.1.4",
Expand Down
29 changes: 0 additions & 29 deletions client/src/hooks/datafiles/mutations/useTrash.js

This file was deleted.

119 changes: 119 additions & 0 deletions client/src/hooks/datafiles/mutations/useTrash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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,
scheme,
system,
path,
homeDir,
}: {
api: string;
scheme: string;
system: string;
path: string;
homeDir: string;
}): Promise<{ name: string; path: string }> {
const url = `/api/datafiles/${api}/trash/${scheme}/${system}/${path}/`;
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
const request = await apiClient.put(url, {
headers: {
'X-CSRFToken': Cookies.get('csrftoken') || '',
},
withCredentials: true,
body: JSON.stringify({
homeDir: homeDir,
}),
});

return request.data;
}

function useTrash() {
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
const dispatch = useDispatch();
const { selectedFiles: selected } = useSelectedFiles();
const status = useSelector(
(state: any) => state.files.operationStatus.trash,
shallowEqual
);

const { api, scheme } = useSelector(
(state: any) => state.files.params.FilesListing
);
const setStatus = (newStatus: any) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { status: newStatus, operation: 'trash' },
});
};

const { mutateAsync } = useMutation({ mutationFn: trashUtil });

const trash = ({
selection,
callback,
}: {
selection: any;
callback: (name: string, path: string) => any;
}) => {
const filteredSelected = selected.filter(
(f: any) => status[f.id] !== 'SUCCESS'
);
const trashCalls: Promise<any>[] = filteredSelected.forEach((file: any) => {
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'RUNNING',
key: (index: string) => index,
operation: 'trash',
},
});
return mutateAsync(
{
api: api,
scheme: scheme,
system: selection.system,
path: selection.path,
homeDir: selection.homeDir,
},
{
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
onSuccess: (response: any) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'SUCCESS',
key: (index: string) => index,
operation: 'trash',
},
});
dispatch({
type: 'ADD_TOAST',
payload: {
message: `${selection} moved to Trash`,
},
});
callback(response.name, response.path);
},
onError: () => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'ERROR',
key: (index: string) => index,
operation: 'trash',
},
});
},
}
);
});
// filteredSelected.forEach(() => {
// });
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
};

return { trash, status, setStatus };
}

export default useTrash;