|
|
@@ -8,16 +8,20 @@ import { toastSuccess, toastError } from '~/client/util/toastr';
|
|
|
import { useSWRMUTxCurrentPage } from '~/stores/page';
|
|
|
import { mutatePageTree, mutatePageList } from '~/stores/page-listing';
|
|
|
|
|
|
+type PagePathRenameHandler = (newPagePath: string, onRenameFinish?: () => void, onRenameFailure?: () => void) => Promise<void>;
|
|
|
+
|
|
|
export const usePagePathRenameHandler = (
|
|
|
- currentPage?: IPagePopulatedToShowRevision | null, onRenameFinish?: () => void, onRenameFailure?: () => void,
|
|
|
-): (newPagePath: string) => Promise<void> => {
|
|
|
+ currentPage?: IPagePopulatedToShowRevision | null,
|
|
|
+): PagePathRenameHandler => {
|
|
|
|
|
|
const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
const currentPagePath = currentPage?.path;
|
|
|
|
|
|
- const pagePathRenameHandler = useCallback(async(newPagePath: string) => {
|
|
|
+ const pagePathRenameHandler = useCallback(async(
|
|
|
+ newPagePath: string, onRenameFinish?: () => void, onRenameFailure?: () => void,
|
|
|
+ ) => {
|
|
|
|
|
|
const onRenamed = (fromPath: string | undefined, toPath: string) => {
|
|
|
mutatePageTree();
|
|
|
@@ -34,7 +38,6 @@ export const usePagePathRenameHandler = (
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- onRenameFinish?.();
|
|
|
await apiv3Put('/pages/rename', {
|
|
|
pageId: currentPage?._id,
|
|
|
revisionId: currentPage?.revision._id,
|
|
|
@@ -43,13 +46,15 @@ export const usePagePathRenameHandler = (
|
|
|
|
|
|
onRenamed(currentPage?.path, newPagePath);
|
|
|
|
|
|
+ onRenameFinish?.();
|
|
|
+
|
|
|
toastSuccess(t('renamed_pages', { path: currentPage?.path }));
|
|
|
}
|
|
|
catch (err) {
|
|
|
onRenameFailure?.();
|
|
|
toastError(err);
|
|
|
}
|
|
|
- }, [currentPage?._id, currentPage?.path, currentPage?.revision._id, currentPagePath, mutateCurrentPage, onRenameFailure, onRenameFinish, t]);
|
|
|
+ }, [currentPage?._id, currentPage?.path, currentPage?.revision._id, currentPagePath, mutateCurrentPage, t]);
|
|
|
|
|
|
return pagePathRenameHandler;
|
|
|
};
|