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-726: Mutation hook: Mkdir #997

Merged
merged 13 commits into from
Dec 4, 2024
34 changes: 0 additions & 34 deletions client/src/hooks/datafiles/mutations/useMkdir.js

This file was deleted.

100 changes: 100 additions & 0 deletions client/src/hooks/datafiles/mutations/useMkdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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.replace(/\/{2,}/g, '/');
const response = await apiClient.put<{ name: string; path: string }>(url, {
dirname: 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;
}) => {
dispatch({
type: 'DATA_FILES_MKDIR',
payload: {
api,
scheme,
system,
path,
dirname,
reloadCallback,
},
});
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;
Loading