Browse Source

move hooks

ryoji-s 2 years ago
parent
commit
658df2b25b
1 changed files with 80 additions and 1 deletions
  1. 80 1
      apps/app/src/components/Navbar/hooks.tsx

+ 80 - 1
apps/app/src/components/Navbar/hooks.tsx

@@ -3,7 +3,7 @@ import { useCallback } from 'react';
 import { useTranslation } from 'next-i18next';
 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 { useIsNotFound } from '~/stores/page';
 import { EditorMode, useEditorMode } from '~/stores/ui';
@@ -56,3 +56,82 @@ export const useOnPageEditorModeButtonClicked = (
     mutateEditorMode(editorMode);
   }, [isNotFound, mutateEditorMode, path, router, setIsCreating, t]);
 };
+
+export const useOnTemplateForChildrenButtonClicked = (
+    setIsCreating: React.Dispatch<React.SetStateAction<boolean>>,
+    currentPagePath: string,
+): () => Promise<void> => {
+  const router = useRouter();
+
+  return useCallback(async() => {
+    try {
+      setIsCreating(true);
+
+      const path = currentPagePath == null || currentPagePath === '/'
+        ? '/_template'
+        : `${currentPagePath}/_template`;
+
+      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 {
+      setIsCreating(false);
+    }
+  }, [currentPagePath, router, setIsCreating]);
+};
+
+
+export const useOnTemplateForDescendantsButtonClicked = (
+    setIsCreating: React.Dispatch<React.SetStateAction<boolean>>,
+    currentPagePath: string,
+): () => Promise<void> => {
+  const router = useRouter();
+
+  return useCallback(async() => {
+    try {
+      setIsCreating(true);
+
+      const path = currentPagePath == null || currentPagePath === '/'
+        ? '/__template'
+        : `${currentPagePath}/__template`;
+
+      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 {
+      setIsCreating(false);
+    }
+  }, [currentPagePath, router, setIsCreating]);
+};