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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getFilePermissions from 'utils/filePermissions';
import { useModal, useSelectedFiles, useFileListing } from 'hooks/datafiles';
import { useSystemRole } from '../DataFilesProjectMembers/_cells/SystemRoleSelector';
import './DataFilesToolbar.scss';
import { useTrash } from 'hooks/datafiles/mutations';

export const ToolbarButton = ({ text, iconName, onClick, disabled }) => {
const iconClassName = `action icon-${iconName}`;
Expand Down Expand Up @@ -40,6 +41,7 @@ const DataFilesToolbar = ({ scheme, api }) => {
const { toggle } = useModal();
const { selectedFiles } = useSelectedFiles();
const { params } = useFileListing('FilesListing');
const { trash } = useTrash();

const history = useHistory();
const location = useLocation();
Expand Down Expand Up @@ -172,20 +174,15 @@ const DataFilesToolbar = ({ scheme, api }) => {
}
};

const trash = useCallback(() => {
const filteredSelected = selectedFiles.filter(
(f) => status[f.system + f.path] !== 'SUCCESS'
);
const homeDir = selectedSystem?.homeDir;

dispatch({
type: 'DATA_FILES_TRASH',
payload: {
src: filteredSelected,
homeDir: selectedSystem?.homeDir || '',
reloadCallback: reloadPage,
},
const trashCallback = useCallback(() => {
trash({
destSystem: selectedSystem.system,
homeDir: homeDir,
callback: reloadPage,
});
}, [selectedFiles, selectedSystem, reloadPage]);
}, [selectedFiles, reloadPage, status]);

const empty = () => {
dispatch({
Expand Down Expand Up @@ -271,7 +268,7 @@ const DataFilesToolbar = ({ scheme, api }) => {
<ToolbarButton
text={!inTrash ? 'Trash' : 'Empty'}
iconName="trash"
onClick={!inTrash ? trash : empty}
onClick={!inTrash ? trashCallback : empty}
disabled={!inTrash ? !canTrash : !canEmpty}
className={!inTrash ? '' : 'is-empty'}
/>
Expand Down
29 changes: 0 additions & 29 deletions client/src/hooks/datafiles/mutations/useTrash.js

This file was deleted.

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

return response.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 = ({
destSystem,
homeDir,
callback,
}: {
destSystem: string;
homeDir: any;
callback: any;
}) => {
const filteredSelected = selected.filter(
(f: any) => status[f.id] !== 'SUCCESS'
);
const trashCalls: Promise<any>[] = filteredSelected.map((file: any) => {
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: destSystem,
path: file.path,
homeDir: 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',
},
});

callback();
},
onError: () => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'ERROR',
key: (index: string) => index,
operation: 'trash',
},
});
},
}
);
});
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 };
}

export default useTrash;