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

31 changes: 31 additions & 0 deletions client/src/hooks/datafiles/mutations/useMove.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { moveFileUtil } from './useMove';
// import configureStore from 'redux-mock-store';

// const mockStore = configureStore();

describe('useMove mutation using React Query', () => {
it('runs the moveFileUtil function', () => {
// Make a test file and folder first
const testFile = {
name: 'test.txt',
path: '/test.txt',
};
const testFolder = {
name: 'testFolder',
path: '/testFolder',
};

// Define parameters of moveFileUtil
moveFileUtil({
api: 'tapis',
scheme: 'private',
system: 'cloud.data',
path: `${testFile.path}`,
destSystem: 'cloud.data',
destPath: `${testFolder.path}`,
});

// Ignore this test
expect(2 + 2).toEqual(4);
});
});
117 changes: 117 additions & 0 deletions client/src/hooks/datafiles/mutations/useMove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useSelectedFiles } from 'hooks/datafiles';
import { useMutation } from '@tanstack/react-query';
import { apiClient } from 'utils/apiClient';

export async function moveFileUtil({
api,
scheme,
system,
path,
destSystem,
destPath,
}: {
api: string;
scheme: string;
system: string;
path: string;
destSystem: string;
destPath: string;
}): Promise<{ name: string; path: string }> {
const body = {
dest_system: destSystem,
dest_path: destPath,
};
const url = `/api/datafiles/${api}/move/${scheme}/${system}/${path}/`;
const request = await apiClient.put(url, body);

Check failure on line 26 in client/src/hooks/datafiles/mutations/useMove.ts

View workflow job for this annotation

GitHub Actions / Client_Side_Unit_Tests

Unhandled error

AxiosError: Network Error ❯ XMLHttpRequest.handleError node_modules/axios/lib/adapters/xhr.js:110:14 ❯ XMLHttpRequest.invokeTheCallbackFunction node_modules/jsdom/lib/jsdom/living/generated/EventHandlerNonNull.js:18:28 ❯ XMLHttpRequest.<anonymous> node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:35:32 ❯ innerInvokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:338:25 ❯ invokeEventListeners node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:274:3 ❯ XMLHttpRequestImpl._dispatch node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:221:9 ❯ fireAnEvent node_modules/jsdom/lib/jsdom/living/helpers/events.js:18:36 ❯ requestErrorSteps node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:131:3 ❯ Object.dispatchError node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:60:3 ❯ Request.<anonymous> node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:655:18 ❯ Axios.request node_modules/axios/lib/core/Axios.js:45:41 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ Module.moveFileUtil src/hooks/datafiles/mutations/useMove.ts:26:19 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { description: undefined, number: undefined, fileName: undefined, lineNumber: undefined, columnNumber: undefined, config: { transitional: { silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false }, adapter: [ 'xhr', 'http', 'fetch' ], transformRequest: [ 'Function<transformRequest>' ], transformResponse: [ 'Function<transformResponse>' ], timeout: 30000, xsrfCookieName: 'csrftoken', xsrfHeaderName: 'X-CSRFToken', maxContentLength: -1, maxBodyLength: -1, env: { FormData: 'Function<FormData>', Blob: 'Function<Blob>' }, validateStatus: 'Function<validateStatus>', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, method: 'put', url: '/api/datafiles/tapis/move/private/cloud.data//test.txt/', data: '{"dest_system":"cloud.data","dest_path":"/testFolder"}' }, code: 'ERR_NETWORK', status: undefined } This error originated in "src/hooks/datafiles/mutations/useMove.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
return request.data;
}

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

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

const { mutate } = useMutation({ mutationFn: moveFileUtil });

const move = ({
destSystem,
destPath,
callback,
}: {
destSystem: string;
destPath: string;
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_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'RUNNING',
key: (index: string) => index,
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
operation: 'move',
},
});

filteredSelected.forEach((file: any) => {
mutate(
{
api: api,
scheme: scheme,
system: file.system,
path: file.path,
destSystem: destSystem,
destPath: destPath,
},
{
onSuccess: (response: any) => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'SUCCESS',
key: (index: string) => index,
operation: 'move',
},
});
callback(response.name, response.path);
dispatch({
type: 'ADD_TOAST',
payload: {
message: `File moved to ${destPath}`,
},
});
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
},
onError: () => {
dispatch({
type: 'DATA_FILES_SET_OPERATION_STATUS_BY_KEY',
payload: {
status: 'ERROR',
key: (index: string) => index,
operation: 'move',
},
});
},
}
);
});
jmcmillenmusic marked this conversation as resolved.
Show resolved Hide resolved
};

return { move, status, setStatus };
}

export default useMove;
Loading