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-729: Mutation hook: Move files #996

Merged
merged 22 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
35 changes: 0 additions & 35 deletions client/src/hooks/datafiles/mutations/useMove.js

This file was deleted.

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

export async function moveFileUtil({
// Establish variables and their types per TypeScript
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
api,
scheme,
system,
path,
destSystem,
destPath,
}: {
api: string;
scheme: string;
system: string;
path: string;
destSystem: string;
destPath: string;
}): Promise<{ name: string; path: string }> {
const url = `/api/datafiles/${api}/move/${scheme}/${system}/${path}/`;
const request = await apiClient.put<{ name: string; path: string }>(url, {
method: 'PUT',
headers: {
'X-CSRFToken': Cookies.get('csrftoken'),
},
credentials: 'same-origin',
body: JSON.stringify({ dest_system: destSystem, dest_path: destPath }),
});
return request.data;
}

function useMove() {
const dispatch = useDispatch();
const { selectedFiles: selected } = useSelectedFiles();
const status = useSelector(
(state: any) => state.files.operationStatus.move,
shallowEqual
);

const setStatus = (newStatus: any) =>
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { operation: 'move', status: newStatus },
});

// Establish mutate using moveFileUtil as its mutation function
const { mutate } = useMutation({ mutationFn: moveFileUtil });

const move = ({
// Establish variables and their types per TypeScript
destSystem,
destPath,
callback,
}: {
destSystem: any;
destPath: any;
callback: (name: string, path: string) => any;
}) => {
const filteredSelected = selected.filter(
(f: any) => status[f.id] !== 'SUCCESS'
);
dispatch({
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
type: 'DATA_FILES_MOVE',
payload: {
dest: { system: destSystem, path: destPath },
src: filteredSelected,
reloadCallback: callback,
},
});
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved

// Establish the parameters of mutate
mutate(
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
{
api: filteredSelected.api,
scheme: filteredSelected.scheme,
system: filteredSelected.system,
path: filteredSelected.path,
destSystem,
destPath,
},
{
// Moves the file to a new location if successful
onSuccess: (response: any) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { status: 'SUCCESS', operation: 'move' },
});
callback(response.name, response.path);
dispatch({
type: 'ADD_TOAST',
payload: {
message: `File moved to ${destPath}`,
},
});
},
// Sends an error message if it's not successful
onError: () => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS',
payload: { status: 'ERROR', operation: 'move' },
});
},
}
);
};

return { move, status, setStatus };
}

export default useMove;
Loading