|
|
@@ -2,11 +2,12 @@ import React, {
|
|
|
useCallback, useState, FC, useEffect, memo,
|
|
|
} from 'react';
|
|
|
import nodePath from 'path';
|
|
|
+import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
import { ItemNode } from './ItemNode';
|
|
|
import { useSWRxPageChildren } from '../../../stores/page-listing';
|
|
|
import { usePageId } from '../../../stores/context';
|
|
|
-import { useCreateModalStatus } from '../../../stores/ui';
|
|
|
+import ClosableTextInput, { AlertInfo, AlertType } from '../../Common/ClosableTextInput';
|
|
|
|
|
|
|
|
|
interface ItemProps {
|
|
|
@@ -27,12 +28,12 @@ const markTarget = (children: ItemNode[], targetId: string): void => {
|
|
|
};
|
|
|
|
|
|
type ItemControlProps = {
|
|
|
- onClickOpenModalButtonHandler?(): void
|
|
|
+ onClickPlusButtonHandler?(): void
|
|
|
}
|
|
|
|
|
|
const ItemControl: FC<ItemControlProps> = memo((props: ItemControlProps) => {
|
|
|
const onClickHandler = () => {
|
|
|
- const { onClickOpenModalButtonHandler: handler } = props;
|
|
|
+ const { onClickPlusButtonHandler: handler } = props;
|
|
|
if (handler == null) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -71,6 +72,7 @@ const ItemCount: FC = () => {
|
|
|
};
|
|
|
|
|
|
const Item: FC<ItemProps> = (props: ItemProps) => {
|
|
|
+ const { t } = useTranslation();
|
|
|
const { itemNode, isOpen: _isOpen = false } = props;
|
|
|
|
|
|
const { page, children } = itemNode;
|
|
|
@@ -78,11 +80,11 @@ const Item: FC<ItemProps> = (props: ItemProps) => {
|
|
|
const [currentChildren, setCurrentChildren] = useState(children);
|
|
|
const [isOpen, setIsOpen] = useState(_isOpen);
|
|
|
|
|
|
+ const [isNewPageInputShown, setNewPageInputShown] = useState(false);
|
|
|
+
|
|
|
const { data: targetId } = usePageId();
|
|
|
const { data, error } = useSWRxPageChildren(isOpen ? page._id : null);
|
|
|
|
|
|
- const { open: openCreateModal } = useCreateModalStatus();
|
|
|
-
|
|
|
const hasChildren = useCallback((): boolean => {
|
|
|
return currentChildren != null && currentChildren.length > 0;
|
|
|
}, [currentChildren]);
|
|
|
@@ -91,9 +93,21 @@ const Item: FC<ItemProps> = (props: ItemProps) => {
|
|
|
setIsOpen(!isOpen);
|
|
|
}, [isOpen]);
|
|
|
|
|
|
- const onClickOpenModalButtonHandler = useCallback(() => {
|
|
|
- openCreateModal(page.path);
|
|
|
- }, [openCreateModal, page]);
|
|
|
+ const inputValidator = (title: string | null): AlertInfo | null => {
|
|
|
+ if (title == null || title === '') {
|
|
|
+ return {
|
|
|
+ type: AlertType.ERROR,
|
|
|
+ message: t('Page title is required'),
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ };
|
|
|
+
|
|
|
+ // TODO: go to create page page
|
|
|
+ const onPressEnterHandler = () => {
|
|
|
+ console.log('Enter key was pressed!');
|
|
|
+ };
|
|
|
|
|
|
// didMount
|
|
|
useEffect(() => {
|
|
|
@@ -144,9 +158,17 @@ const Item: FC<ItemProps> = (props: ItemProps) => {
|
|
|
<ItemCount />
|
|
|
</div>
|
|
|
<div className="grw-pagetree-control d-none">
|
|
|
- <ItemControl onClickOpenModalButtonHandler={onClickOpenModalButtonHandler} />
|
|
|
+ <ItemControl onClickPlusButtonHandler={() => { setNewPageInputShown(true) }} />
|
|
|
</div>
|
|
|
</div>
|
|
|
+
|
|
|
+ <ClosableTextInput
|
|
|
+ isShown={isNewPageInputShown}
|
|
|
+ placeholder={t('Input title')}
|
|
|
+ onClickOutside={() => { setNewPageInputShown(false) }}
|
|
|
+ onPressEnter={onPressEnterHandler}
|
|
|
+ inputValidator={inputValidator}
|
|
|
+ />
|
|
|
{
|
|
|
isOpen && hasChildren() && currentChildren.map(node => (
|
|
|
<Item
|