Răsfoiți Sursa

commonize updateStateAfterSave

Yuken Tezuka 3 ani în urmă
părinte
comite
28731ce446

+ 28 - 0
packages/app/src/client/services/page-operation.ts

@@ -2,7 +2,10 @@ import { SubscriptionStatusType, Nullable } from '@growi/core';
 import urljoin from 'url-join';
 
 import { OptionsToSave } from '~/interfaces/page-operation';
+import { useCurrentPageId } from '~/stores/context';
 import { useIsEnabledUnsavedWarning } from '~/stores/editor';
+import { useSWRxCurrentPage } from '~/stores/page';
+import { useSetRemoteLatestPageData } from '~/stores/remote-latest-page';
 import loggerFactory from '~/utils/logger';
 
 import { toastError } from '../util/apiNotification';
@@ -171,3 +174,28 @@ export const useSaveOrUpdate = (): SaveOrUpdateFunction => {
     return res;
   };
 };
+
+export const updateStateAfterSave = async(pageId: string) => {
+  const { mutate: mutateCurrentPageId } = useCurrentPageId();
+  const { mutate: mutateCurrentPage } = useSWRxCurrentPage();
+  const { setRemoteLatestPageData } = useSetRemoteLatestPageData();
+
+  // update swr 'currentPageId', 'currentPage', remote states
+
+  await mutateCurrentPageId(pageId);
+  const updatedPage = await mutateCurrentPage();
+
+  if (updatedPage == null) { return }
+
+  const remoterevisionData = {
+    remoteRevisionId: updatedPage.revision._id,
+    remoteRevisionBody: updatedPage.revision.body,
+    remoteRevisionLastUpdateUser: updatedPage.lastUpdateUser,
+    remoteRevisionLastUpdatedAt: updatedPage.updatedAt,
+    revisionIdHackmdSynced: updatedPage.revisionHackmdSynced.toString(),
+    hasDraftOnHackmd: updatedPage.hasDraftOnHackmd,
+  };
+
+  setRemoteLatestPageData(remoterevisionData);
+
+};

+ 1 - 23
packages/app/src/components/PageEditor.tsx

@@ -13,7 +13,7 @@ import { useTranslation } from 'next-i18next';
 import { useRouter } from 'next/router';
 import { throttle, debounce } from 'throttle-debounce';
 
-import { useSaveOrUpdate } from '~/client/services/page-operation';
+import { updateStateAfterSave, useSaveOrUpdate } from '~/client/services/page-operation';
 import { apiGet, apiPostForm } from '~/client/util/apiv1-client';
 import { toastError, toastSuccess } from '~/client/util/toastr';
 import { IEditorMethods } from '~/interfaces/editor-methods';
@@ -234,28 +234,6 @@ const PageEditor = React.memo((): JSX.Element => {
   // eslint-disable-next-line max-len
   }, [grantData, isSlackEnabled, currentPathname, slackChannels, pageTags, saveOrUpdate, pageId, currentPagePath, currentRevisionId]);
 
-  const updateStateAfterSave = useCallback(async(pageId: string) => {
-
-    // update swr 'currentPageId', 'currentPage', remote states
-
-    await mutateCurrentPageId(pageId);
-    const updatedPage = await mutateCurrentPage();
-
-    if (updatedPage == null) { return }
-
-    const remoterevisionData = {
-      remoteRevisionId: updatedPage.revision._id,
-      remoteRevisionBody: updatedPage.revision.body,
-      remoteRevisionLastUpdateUser: updatedPage.lastUpdateUser,
-      remoteRevisionLastUpdatedAt: updatedPage.updatedAt,
-      revisionIdHackmdSynced: updatedPage.revisionHackmdSynced.toString(),
-      hasDraftOnHackmd: updatedPage.hasDraftOnHackmd,
-    };
-
-    setRemoteLatestPageData(remoterevisionData);
-
-  }, [mutateCurrentPage, mutateCurrentPageId, setRemoteLatestPageData]);
-
   const saveAndReturnToViewHandler = useCallback(async(opts?: {overwriteScopesOfDescendants: boolean}) => {
     if (editorMode !== EditorMode.Editor) {
       return;

+ 1 - 23
packages/app/src/components/PageEditorByHackmd.tsx

@@ -10,7 +10,7 @@ import { useRouter } from 'next/router';
 import { useTranslation } from 'react-i18next';
 import urljoin from 'url-join';
 
-import { useSaveOrUpdate } from '~/client/services/page-operation';
+import { updateStateAfterSave, useSaveOrUpdate } from '~/client/services/page-operation';
 import { toastError, toastSuccess } from '~/client/util/apiNotification';
 import { apiPost } from '~/client/util/apiv1-client';
 import { IResHackmdIntegrated, IResHackmdDiscard } from '~/interfaces/hackmd';
@@ -92,28 +92,6 @@ export const PageEditorByHackmd = (): JSX.Element => {
 
   const hackmdEditorRef = useRef<HackEditorRef>(null);
 
-  const updateStateAfterSave = useCallback(async(pageId: string) => {
-
-    // update swr 'currentPageId', 'currentPage', remote states
-
-    await mutateCurrentPageId(pageId);
-    const updatedPage = await mutatePageData();
-
-    if (updatedPage == null) { return }
-
-    const remoterevisionData = {
-      remoteRevisionId: updatedPage.revision._id,
-      remoteRevisionBody: updatedPage.revision.body,
-      remoteRevisionLastUpdateUser: updatedPage.lastUpdateUser,
-      remoteRevisionLastUpdatedAt: updatedPage.updatedAt,
-      revisionIdHackmdSynced: updatedPage.revisionHackmdSynced.toString(),
-      hasDraftOnHackmd: updatedPage.hasDraftOnHackmd,
-    };
-
-    setRemoteLatestPageData(remoterevisionData);
-
-  }, [mutateCurrentPageId, mutatePageData, setRemoteLatestPageData]);
-
   const saveAndReturnToViewHandler = useCallback(async(opts?: {overwriteScopesOfDescendants: boolean}) => {
     if (editorMode !== EditorMode.HackMD) { return }