|
|
@@ -2,7 +2,6 @@ import React, {
|
|
|
useState, useEffect, useRef, useMemo, useCallback,
|
|
|
} from 'react';
|
|
|
|
|
|
-import type { IUser } from '@growi/core';
|
|
|
import { UserPicture } from '@growi/ui';
|
|
|
import CodeMirror from 'codemirror/lib/codemirror';
|
|
|
import { format } from 'date-fns';
|
|
|
@@ -11,11 +10,13 @@ import {
|
|
|
Modal, ModalHeader, ModalBody, ModalFooter,
|
|
|
} from 'reactstrap';
|
|
|
|
|
|
+import { useSaveOrUpdate } from '~/client/services/page-operation';
|
|
|
+import { toastError, toastSuccess } from '~/client/util/toastr';
|
|
|
import { OptionsToSave } from '~/interfaces/page-operation';
|
|
|
-import { useCurrentUser } from '~/stores/context';
|
|
|
-import { useSWRxCurrentPage } from '~/stores/page';
|
|
|
+import { useCurrentPageId, useCurrentPathname, useCurrentUser } from '~/stores/context';
|
|
|
+import { useCurrentPagePath, useSWRxCurrentPage } from '~/stores/page';
|
|
|
import {
|
|
|
- useRemoteRevisionBody, useRemoteRevisionId, useRemoteRevisionLastUpdatedAt, useRemoteRevisionLastUpdatUser,
|
|
|
+ useRemoteRevisionBody, useRemoteRevisionId, useRemoteRevisionLastUpdatedAt, useRemoteRevisionLastUpdatUser, useSetRemoteLatestPageData,
|
|
|
} from '~/stores/remote-latest-page';
|
|
|
import { useEditorMode } from '~/stores/ui';
|
|
|
|
|
|
@@ -35,6 +36,7 @@ type ConflictDiffModalProps = {
|
|
|
onClose?: (() => void);
|
|
|
markdownOnEdit: string;
|
|
|
optionsToSave: OptionsToSave | undefined;
|
|
|
+ afterResolvedHandler: () => void,
|
|
|
};
|
|
|
|
|
|
type ConflictDiffModalCoreProps = {
|
|
|
@@ -44,6 +46,7 @@ type ConflictDiffModalCoreProps = {
|
|
|
request: IRevisionOnConflictWithStringDate,
|
|
|
origin: IRevisionOnConflictWithStringDate,
|
|
|
latest: IRevisionOnConflictWithStringDate,
|
|
|
+ afterResolvedHandler: () => void,
|
|
|
};
|
|
|
|
|
|
type IRevisionOnConflictWithStringDate = Omit<IRevisionOnConflict, 'createdAt'> & {
|
|
|
@@ -52,17 +55,23 @@ type IRevisionOnConflictWithStringDate = Omit<IRevisionOnConflict, 'createdAt'>
|
|
|
|
|
|
const ConflictDiffModalCore = (props: ConflictDiffModalCoreProps): JSX.Element => {
|
|
|
const {
|
|
|
- onClose, request, origin, latest,
|
|
|
+ onClose, request, origin, latest, optionsToSave, afterResolvedHandler,
|
|
|
} = props;
|
|
|
|
|
|
- const { data: editorMode } = useEditorMode();
|
|
|
-
|
|
|
const { t } = useTranslation('');
|
|
|
const [resolvedRevision, setResolvedRevision] = useState<string>('');
|
|
|
const [isRevisionselected, setIsRevisionSelected] = useState<boolean>(false);
|
|
|
const [isModalExpanded, setIsModalExpanded] = useState<boolean>(false);
|
|
|
const [codeMirrorRef, setCodeMirrorRef] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
|
+ const { data: remoteRevisionId } = useRemoteRevisionId();
|
|
|
+ const { setRemoteLatestPageData } = useSetRemoteLatestPageData();
|
|
|
+ const { data: pageId } = useCurrentPageId();
|
|
|
+ const { data: currentPagePath } = useCurrentPagePath();
|
|
|
+ const { data: currentPathname } = useCurrentPathname();
|
|
|
+
|
|
|
+ const saveOrUpdate = useSaveOrUpdate();
|
|
|
+
|
|
|
const uncontrolledRef = useRef<CodeMirror>(null);
|
|
|
|
|
|
useEffect(() => {
|
|
|
@@ -95,15 +104,30 @@ const ConflictDiffModalCore = (props: ConflictDiffModalCoreProps): JSX.Element =
|
|
|
const codeMirrorVal = uncontrolledRef.current?.editor.doc.getValue();
|
|
|
|
|
|
try {
|
|
|
- // await pageContainer.resolveConflict(codeMirrorVal, editorMode, props.optionsToSave);
|
|
|
- // close();
|
|
|
- // pageContainer.showSuccessToastr();
|
|
|
+ // TODO: consider using saveOrUpdate or create new method
|
|
|
+ const { page } = await saveOrUpdate(
|
|
|
+ codeMirrorVal,
|
|
|
+ { pageId, path: currentPagePath || currentPathname || '', revisionId: remoteRevisionId },
|
|
|
+ optionsToSave,
|
|
|
+ );
|
|
|
+ const remotePageData = {
|
|
|
+ remoteRevisionId: page.revision._id,
|
|
|
+ remoteRevisionBody: page.revision.body,
|
|
|
+ remoteRevisionLastUpdateUser: page.lastUpdateUser,
|
|
|
+ remoteRevisionLastUpdatedAt: page.updatedAt,
|
|
|
+ };
|
|
|
+ setRemoteLatestPageData(remotePageData);
|
|
|
+ afterResolvedHandler();
|
|
|
+
|
|
|
+ close();
|
|
|
+
|
|
|
+ toastSuccess('Saved successfully');
|
|
|
}
|
|
|
catch (error) {
|
|
|
- // pageContainer.showErrorToastr(error);
|
|
|
+ toastError(`Error occured: ${error.message}`);
|
|
|
}
|
|
|
|
|
|
- }, []);
|
|
|
+ }, [afterResolvedHandler, close, currentPagePath, currentPathname, optionsToSave, pageId, remoteRevisionId, saveOrUpdate, setRemoteLatestPageData]);
|
|
|
|
|
|
const resizeAndCloseButtons = useMemo(() => (
|
|
|
<div className="d-flex flex-nowrap">
|
|
|
@@ -258,7 +282,9 @@ const ConflictDiffModalCore = (props: ConflictDiffModalCoreProps): JSX.Element =
|
|
|
|
|
|
|
|
|
export const ConflictDiffModal = (props: ConflictDiffModalProps): JSX.Element => {
|
|
|
- const { isOpen, onClose, optionsToSave } = props;
|
|
|
+ const {
|
|
|
+ isOpen, onClose, optionsToSave, afterResolvedHandler,
|
|
|
+ } = props;
|
|
|
const { data: currentUser } = useCurrentUser();
|
|
|
|
|
|
// state for current page
|
|
|
@@ -304,6 +330,7 @@ export const ConflictDiffModal = (props: ConflictDiffModalProps): JSX.Element =>
|
|
|
request,
|
|
|
origin,
|
|
|
latest,
|
|
|
+ afterResolvedHandler,
|
|
|
};
|
|
|
|
|
|
return <ConflictDiffModalCore {...propsForCore}/>;
|