|
@@ -1,56 +1,38 @@
|
|
|
-import { useCallback, useState } from 'react';
|
|
|
|
|
|
|
+import { useCallback } from 'react';
|
|
|
|
|
|
|
|
import { isCreatablePage } from '@growi/core/dist/utils/page-path-utils';
|
|
import { isCreatablePage } from '@growi/core/dist/utils/page-path-utils';
|
|
|
-import { useRouter } from 'next/router';
|
|
|
|
|
|
|
+import { normalizePath } from '@growi/core/dist/utils/path-utils';
|
|
|
|
|
|
|
|
-import { createPage, exist } from '~/client/services/page-operation';
|
|
|
|
|
import type { LabelType } from '~/interfaces/template';
|
|
import type { LabelType } from '~/interfaces/template';
|
|
|
-import { EditorMode, useEditorMode } from '~/stores/ui';
|
|
|
|
|
|
|
+import { useCurrentPagePath } from '~/stores/page';
|
|
|
|
|
|
|
|
-export const useCreateTemplatePage = (
|
|
|
|
|
- currentPagePath?: string,
|
|
|
|
|
- isLoading?: boolean,
|
|
|
|
|
-): {
|
|
|
|
|
- isCreatable: boolean,
|
|
|
|
|
- isPageCreating: boolean,
|
|
|
|
|
- create?: (label: LabelType) => Promise<void>,
|
|
|
|
|
-} => {
|
|
|
|
|
- const router = useRouter();
|
|
|
|
|
-
|
|
|
|
|
- const { mutate: mutateEditorMode } = useEditorMode();
|
|
|
|
|
|
|
+import { useCreatePageAndTransit } from './use-create-page-and-transit';
|
|
|
|
|
|
|
|
- const [isPageCreating, setIsPageCreating] = useState(false);
|
|
|
|
|
-
|
|
|
|
|
- const isCreatable = currentPagePath != null && isCreatablePage(`${currentPagePath}/_template`);
|
|
|
|
|
|
|
+type UseCreateTemplatePage = () => {
|
|
|
|
|
+ isCreatable: boolean,
|
|
|
|
|
+ isCreating: boolean,
|
|
|
|
|
+ createTemplate?: (label: LabelType) => Promise<void>,
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- const create = useCallback(async(label: LabelType) => {
|
|
|
|
|
- if (isLoading || !isCreatable) return;
|
|
|
|
|
|
|
+export const useCreateTemplatePage: UseCreateTemplatePage = () => {
|
|
|
|
|
|
|
|
- try {
|
|
|
|
|
- setIsPageCreating(true);
|
|
|
|
|
|
|
+ const { data: currentPagePath, isLoading: isLoadingPagePath } = useCurrentPagePath();
|
|
|
|
|
|
|
|
- const templatePagePath = `${currentPagePath}/${label}`;
|
|
|
|
|
- const res = await exist(JSON.stringify([templatePagePath]));
|
|
|
|
|
- const isExists = res.pages[templatePagePath];
|
|
|
|
|
|
|
+ const { isCreating, createAndTransit } = useCreatePageAndTransit();
|
|
|
|
|
+ const isCreatable = currentPagePath != null && isCreatablePage(normalizePath(`${currentPagePath}/_template`));
|
|
|
|
|
|
|
|
- if (!isExists) {
|
|
|
|
|
- await createPage({ path: templatePagePath });
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const createTemplate = useCallback(async(label: LabelType) => {
|
|
|
|
|
+ if (isLoadingPagePath || !isCreatable) return;
|
|
|
|
|
|
|
|
- router.push(`${templatePagePath}#edit`);
|
|
|
|
|
- mutateEditorMode(EditorMode.Editor);
|
|
|
|
|
- }
|
|
|
|
|
- catch (err) {
|
|
|
|
|
- throw err;
|
|
|
|
|
- }
|
|
|
|
|
- finally {
|
|
|
|
|
- setIsPageCreating(false);
|
|
|
|
|
- }
|
|
|
|
|
- }, [currentPagePath, isCreatable, isLoading, mutateEditorMode, router]);
|
|
|
|
|
|
|
+ return createAndTransit(
|
|
|
|
|
+ { path: normalizePath(`${currentPagePath}/${label}`) },
|
|
|
|
|
+ { shouldCheckPageExists: true },
|
|
|
|
|
+ );
|
|
|
|
|
+ }, [currentPagePath, isCreatable, isLoadingPagePath, createAndTransit]);
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
isCreatable,
|
|
isCreatable,
|
|
|
- isPageCreating,
|
|
|
|
|
- create: isCreatable ? create : undefined,
|
|
|
|
|
|
|
+ isCreating,
|
|
|
|
|
+ createTemplate: isCreatable ? createTemplate : undefined,
|
|
|
};
|
|
};
|
|
|
};
|
|
};
|