Skip to content

Commit

Permalink
Task/WP-726: Mutation hook: Mkdir (#997)
Browse files Browse the repository at this point in the history
* initial conversion to ts

* liniting/formating

* dispatch type change

* extra dispatch removals

* 500 error fix

* removed redux test
  • Loading branch information
jalowe13 authored Dec 4, 2024
1 parent e5c17d8 commit 46cf216
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,7 @@ describe('DataFilesCopyModal', () => {
const submitButton = getByText('Create Folder');
fireEvent.click(submitButton);
});

expect(store.getActions()).toEqual([
{
type: 'DATA_FILES_MKDIR',
payload: {
api: 'tapis',
scheme: 'private',
system: 'test.system',
path: '/',
dirname: 'abc123',
reloadCallback: expect.any(Function),
},
},
]);
// TODO: New test needed for react redux call for mkdir
});

it('Error message on invalid input', async () => {
Expand Down
34 changes: 0 additions & 34 deletions client/src/hooks/datafiles/mutations/useMkdir.js

This file was deleted.

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

export async function mkdirUtil({
api,
scheme,
system,
path,
dirname,
}: {
api: string;
scheme: string;
system: string;
path: string;
dirname: string;
}): Promise<{ name: string; path: string }> {
let apiPath = !path || path[0] === '/' ? path : `/${path}`;
if (apiPath === '/') {
apiPath = '';
}
let url = `/api/datafiles/${api}/mkdir/${scheme}/${system}/${apiPath}/`;
url = url.replace(/\/{2,}/g, '/');
const response = await apiClient.put<{ name: string; path: string }>(url, {
dir_name: dirname,
});

return response.data;
}

function useMkdir() {
const dispatch = useDispatch();
const status = useSelector(
(state: any) => state.files.operationStatus.mkdir,
shallowEqual
);

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

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

const mkdir = ({
api,
scheme,
system,
path,
dirname,
reloadCallback,
}: {
api: string;
scheme: string;
system: string;
path: string;
dirname: string;
reloadCallback: any;
}) => {
mutate(
{
api,
scheme,
system,
path,
dirname,
},
{
onSuccess: () => {
dispatch({
type: 'DATA_FILES_TOGGLE_MODAL',
payload: {
operation: 'mkdir',
props: {},
},
});
reloadCallback();
},
onError: () => {},
}
);
};

return { mkdir, status, setStatus };
}

export default useMkdir;

0 comments on commit 46cf216

Please sign in to comment.