Просмотр исходного кода

declear createPage updatePage saveAndReload functions

kaori 3 лет назад
Родитель
Сommit
3b34255097
1 измененных файлов с 98 добавлено и 2 удалено
  1. 98 2
      packages/app/src/stores/page.tsx

+ 98 - 2
packages/app/src/stores/page.tsx

@@ -2,18 +2,21 @@ import { IPageInfoForEntity, IPagePopulatedToShowRevision, Nullable } from '@gro
 import useSWR, { SWRResponse } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
-import { apiv3Get } from '~/client/util/apiv3-client';
+import { apiPost, apiGet } from '~/client/util/apiv1-client';
+import { apiv3Get, apiv3Post } from '~/client/util/apiv3-client';
 import {
   IPageInfo, IPageInfoForOperation, IPageInfoAll,
 } from '~/interfaces/page';
 import { IRecordApplicableGrant, IResIsGrantNormalized } from '~/interfaces/page-grant';
 import { IRevisionsForPagination } from '~/interfaces/revision';
+import loggerFactory from '~/utils/logger';
 
-import { apiGet } from '../client/util/apiv1-client';
 import { IPageTagsInfo } from '../interfaces/tag';
 
 import { useCurrentPageId } from './context';
 
+const logger = loggerFactory('growi:stores:page');
+
 export const useSWRxPage = (pageId?: string|null, shareLinkId?: string): SWRResponse<IPagePopulatedToShowRevision|null, Error> => {
   return useSWR<IPagePopulatedToShowRevision|null, Error>(
     pageId != null ? ['/page', pageId, shareLinkId] : null,
@@ -137,3 +140,96 @@ export const useSWRxApplicableGrant = (
     (endpoint, pageId) => apiv3Get(endpoint, { pageId }).then(response => response.data),
   );
 };
+
+export type IPageOperation = {
+  createPage: (pagePath, markdown, tmpParams) => Promise<any>
+  updatePage: (pageId, revisionId, markdown, tmpParams) => Promise<any>
+  saveAndReload: (optionsToSave, editorMode) => Promise<any>
+}
+
+export const useSWRxPageOperation = (pageId: string, path: string, revisionId: string): IPageOperation => {
+
+  const createPage = async(pagePath, markdown, tmpParams) => {
+    // const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
+
+    // clone
+    const params = Object.assign(tmpParams, {
+      path: pagePath,
+      body: markdown,
+    });
+
+    const res = await apiv3Post('/pages/', params);
+    const { page, tags, revision } = res.data;
+
+    return { page, tags, revision };
+  };
+
+  const updatePage = async(pageId, revisionId, markdown, tmpParams): Promise<any> => {
+    // const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
+
+    // clone
+    const params = Object.assign(tmpParams, {
+      page_id: pageId,
+      revision_id: revisionId,
+      body: markdown,
+    });
+
+    const res: any = await apiPost('/pages.update', params);
+    if (!res.ok) {
+      throw new Error(res.error);
+    }
+    return res;
+  };
+
+
+  const saveAndReload = async(optionsToSave, editorMode): Promise<void> => {
+    if (optionsToSave == null) {
+      const msg = '\'saveAndReload\' requires the \'optionsToSave\' param';
+      throw new Error(msg);
+    }
+
+    if (editorMode == null) {
+      logger.warn('\'saveAndReload\' requires the \'editorMode\' param');
+      return;
+    }
+
+    // const { pageId, path } = this.state;
+    // const { revisionId } = this.state;
+
+    const options = Object.assign({}, optionsToSave);
+
+    let markdown;
+    // if (editorMode === EditorMode.HackMD) {
+    //   const pageEditorByHackmd = this.appContainer.getComponentInstance('PageEditorByHackmd');
+    //   markdown = await pageEditorByHackmd.getMarkdown();
+    //   // set option to sync
+    //   options.isSyncRevisionToHackmd = true;
+    //   revisionId = this.state.revisionIdHackmdSynced;
+    // }
+    // else {
+    // const pageEditor = this.appContainer.getComponentInstance('PageEditor');
+    // markdown = pageEditor.getMarkdown();
+    // }
+
+    let res;
+    if (pageId == null) {
+      res = await createPage(path, markdown, options);
+    }
+    else {
+      res = await updatePage(pageId, revisionId, markdown, options);
+    }
+
+    // const editorContainer = this.appContainer.getContainer('EditorContainer');
+    // editorContainer.clearDraft(path);
+    window.location.href = path;
+
+    return res;
+  };
+
+
+  return {
+    createPage,
+    updatePage,
+    saveAndReload,
+  };
+};