-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/WP-726: Mutation hook: Mkdir (#997)
* initial conversion to ts * liniting/formating * dispatch type change * extra dispatch removals * 500 error fix * removed redux test
- Loading branch information
Showing
3 changed files
with
90 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |