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 2 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.

107 changes: 107 additions & 0 deletions client/src/hooks/datafiles/mutations/useTrash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { useSelector, useDispatch, shallowEqual } from 'react-redux';
import { useMutation } from '@tanstack/react-query';
import { apiClient } from 'utils/apiClient';
import Cookies from 'js-cookie';

export async function trashUtil({
// Per TypeScript, declare variables...
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
api,
scheme,
system,
path,
homeDir,
}: {
// ...and their types for use in this function
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<{ name: string; path: string }>(url, {
method: 'PUT',
headers: {
'X-CSRFToken': Cookies.get('csrftoken'),
},
credentials: 'same-origin',
body: JSON.stringify({
homeDir: homeDir,
}),
});

return request.data;
}

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

const setStatus = (newStatus: any) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { status: newStatus, operation: 'trash' },
});
};
// Establish mutate using trashUtil as its mutation function
const { mutate } = useMutation({ mutationFn: trashUtil });

const trash = ({
// Per TypeScript, declare variables...
selection,
callback,
}: {
// ...and their types for use in this function
selection: any;
callback: (name: string, path: string) => any;
}) => {
dispatch({
type: 'DATA_FILES_TRASH',
payload: {
src: selection,
reloadCallback: callback,
},
});

// Establish the parameters of mutate
mutate(
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
{
api: selection.api,
scheme: selection.scheme,
system: selection.system,
path: '/' + selection.path,
homeDir: selection.homeDir,
},
{
// Sends the file to the Trash if successful
onSuccess: (response) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { status: 'SUCCESS', operation: 'trash' },
});
callback(response.name, response.path);
dispatch({
type: 'ADD_TOAST',
payload: {
message: `${selection} moved to Trash`,
},
});
},
// Sends an error message if it's not successful
onError: () => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { status: 'ERROR', operation: 'trash' },
});
},
}
);
};

return { trash, status, setStatus };
}

export default useTrash;
Loading