|
|
@@ -1,15 +1,20 @@
|
|
|
-import React, { FC } from 'react';
|
|
|
+import type { FC } from 'react';
|
|
|
+import { useState, useCallback } from 'react';
|
|
|
+
|
|
|
+import nodePath from 'path';
|
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import {
|
|
|
Modal, ModalHeader, ModalBody, ModalFooter, Button,
|
|
|
} from 'reactstrap';
|
|
|
|
|
|
+import type { IPageForItem } from '~/interfaces/page';
|
|
|
import { useTargetAndAncestors, useIsGuestUser, useIsReadOnlyUser } from '~/stores/context';
|
|
|
import { usePageSelectModal } from '~/stores/modal';
|
|
|
-import { useCurrentPagePath, useCurrentPageId } from '~/stores/page';
|
|
|
+import { useCurrentPagePath, useCurrentPageId, useSWRxCurrentPage } from '~/stores/page';
|
|
|
|
|
|
import { ItemsTree } from '../ItemsTree';
|
|
|
+import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
|
|
|
|
|
|
import { TreeItemForModal } from './TreeItemForModal';
|
|
|
|
|
|
@@ -22,6 +27,8 @@ export const PageSelectModal: FC = () => {
|
|
|
|
|
|
const isOpened = PageSelectModalData?.isOpened ?? false;
|
|
|
|
|
|
+ const [clickedParentPagePath, setClickedParentPagePath] = useState<string | null>(null);
|
|
|
+
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
const { data: isGuestUser } = useIsGuestUser();
|
|
|
@@ -29,19 +36,48 @@ export const PageSelectModal: FC = () => {
|
|
|
const { data: currentPath } = useCurrentPagePath();
|
|
|
const { data: targetId } = useCurrentPageId();
|
|
|
const { data: targetAndAncestorsData } = useTargetAndAncestors();
|
|
|
+ const { data: currentPage } = useSWRxCurrentPage();
|
|
|
+
|
|
|
+ const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
|
|
|
+
|
|
|
+ const onClickTreeItem = useCallback((page: IPageForItem) => {
|
|
|
+ const parentPagePath = page.path;
|
|
|
+
|
|
|
+ if (parentPagePath == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ setClickedParentPagePath(parentPagePath);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const onClickCancel = useCallback(() => {
|
|
|
+ setClickedParentPagePath(null);
|
|
|
+ closeModal();
|
|
|
+ }, [closeModal]);
|
|
|
+
|
|
|
+ const onClickDone = useCallback(() => {
|
|
|
+ if (clickedParentPagePath != null) {
|
|
|
+ const currentPageTitle = nodePath.basename(currentPage?.path ?? '') || '/';
|
|
|
+ const newPagePath = nodePath.resolve(clickedParentPagePath, currentPageTitle);
|
|
|
+
|
|
|
+ pagePathRenameHandler(newPagePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ closeModal();
|
|
|
+ }, [clickedParentPagePath, closeModal, currentPage?.path, pagePathRenameHandler]);
|
|
|
|
|
|
const targetPathOrId = targetId || currentPath;
|
|
|
|
|
|
+ const path = currentPath || '/';
|
|
|
+
|
|
|
if (isGuestUser == null) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- const path = currentPath || '/';
|
|
|
-
|
|
|
return (
|
|
|
<Modal
|
|
|
isOpen={isOpened}
|
|
|
- toggle={() => closeModal()}
|
|
|
+ toggle={closeModal}
|
|
|
centered
|
|
|
size="sm"
|
|
|
>
|
|
|
@@ -54,10 +90,12 @@ export const PageSelectModal: FC = () => {
|
|
|
targetPath={path}
|
|
|
targetPathOrId={targetPathOrId}
|
|
|
targetAndAncestorsData={targetAndAncestorsData}
|
|
|
+ onClickTreeItem={onClickTreeItem}
|
|
|
/>
|
|
|
</ModalBody>
|
|
|
<ModalFooter>
|
|
|
- <Button color="primary" onClick={closeModal}>{t('Done')}</Button>{' '}
|
|
|
+ <Button color="secondary" onClick={onClickCancel}>{t('Cancel')}</Button>
|
|
|
+ <Button color="primary" onClick={onClickDone}>{t('Done')}</Button>
|
|
|
</ModalFooter>
|
|
|
</Modal>
|
|
|
);
|