|
@@ -1,10 +1,11 @@
|
|
|
-import { useCallback } from 'react';
|
|
|
|
|
|
|
+import { useCallback, useState } from 'react';
|
|
|
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import { useRouter } from 'next/router';
|
|
import { useRouter } from 'next/router';
|
|
|
|
|
|
|
|
-import { createPage } from '~/client/services/page-operation';
|
|
|
|
|
|
|
+import { createPage, exist } from '~/client/services/page-operation';
|
|
|
import { toastError } from '~/client/util/toastr';
|
|
import { toastError } from '~/client/util/toastr';
|
|
|
|
|
+import { LabelType } from '~/interfaces/template';
|
|
|
import { useIsNotFound } from '~/stores/page';
|
|
import { useIsNotFound } from '~/stores/page';
|
|
|
import { EditorMode, useEditorMode } from '~/stores/ui';
|
|
import { EditorMode, useEditorMode } from '~/stores/ui';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
@@ -56,3 +57,47 @@ export const useOnPageEditorModeButtonClicked = (
|
|
|
mutateEditorMode(editorMode);
|
|
mutateEditorMode(editorMode);
|
|
|
}, [isNotFound, mutateEditorMode, path, router, setIsCreating, t]);
|
|
}, [isNotFound, mutateEditorMode, path, router, setIsCreating, t]);
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+export const useOnTemplateButtonClicked = (
|
|
|
|
|
+ currentPagePath: string,
|
|
|
|
|
+): {
|
|
|
|
|
+ onClickHandler: (label: LabelType) => Promise<void>,
|
|
|
|
|
+ isPageCreating: boolean
|
|
|
|
|
+} => {
|
|
|
|
|
+ const router = useRouter();
|
|
|
|
|
+ const [isPageCreating, setIsPageCreating] = useState(false);
|
|
|
|
|
+
|
|
|
|
|
+ const onClickHandler = useCallback(async(label: LabelType) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ setIsPageCreating(true);
|
|
|
|
|
+
|
|
|
|
|
+ const path = currentPagePath == null || currentPagePath === '/'
|
|
|
|
|
+ ? `/${label}`
|
|
|
|
|
+ : `${currentPagePath}/${label}`;
|
|
|
|
|
+
|
|
|
|
|
+ const params = {
|
|
|
|
|
+ isSlackEnabled: false,
|
|
|
|
|
+ slackChannels: '',
|
|
|
|
|
+ grant: 4,
|
|
|
|
|
+ // grant: currentPage?.grant || 1,
|
|
|
|
|
+ // grantUserGroupId: currentPage?.grantedGroup?._id,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const res = await exist(JSON.stringify([path]));
|
|
|
|
|
+ if (!res.pages[path]) {
|
|
|
|
|
+ await createPage(path, '', params);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ router.push(`${path}#edit`);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.warn(err);
|
|
|
|
|
+ toastError(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ setIsPageCreating(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [currentPagePath, router]);
|
|
|
|
|
+
|
|
|
|
|
+ return { onClickHandler, isPageCreating };
|
|
|
|
|
+};
|