|
|
@@ -1,5 +1,7 @@
|
|
|
-import React, { memo } from 'react';
|
|
|
+import React, { memo, useCallback, useState } from 'react';
|
|
|
+
|
|
|
import { ItemNode } from './ItemNode';
|
|
|
+import { useSWRxPageChildren } from '../../../stores/page-listing';
|
|
|
|
|
|
|
|
|
interface ItemProps {
|
|
|
@@ -7,11 +9,29 @@ interface ItemProps {
|
|
|
isOpen?: boolean
|
|
|
}
|
|
|
|
|
|
-const Item = memo<ItemProps>((props: ItemProps) => {
|
|
|
+const Item = (props: ItemProps) => {
|
|
|
const { itemNode, isOpen = false } = props;
|
|
|
|
|
|
const { page, children } = itemNode;
|
|
|
|
|
|
+ const [currentChildren, setCurrentChildren] = useState(children);
|
|
|
+
|
|
|
+ const [shouldFetch, setShouldFetch] = useState(false);
|
|
|
+ const { data, error } = useSWRxPageChildren(shouldFetch ? page._id : null);
|
|
|
+
|
|
|
+ const hasChildren = useCallback((): boolean => {
|
|
|
+ return currentChildren != null && currentChildren?.length > 0;
|
|
|
+ }, [currentChildren]);
|
|
|
+
|
|
|
+ const onClickLoadChildren = useCallback(() => {
|
|
|
+ setShouldFetch(true);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ if (error == null && data != null) {
|
|
|
+ const { children } = data;
|
|
|
+ setCurrentChildren(ItemNode.generateNodesFromPages(children));
|
|
|
+ }
|
|
|
+
|
|
|
if (page == null) {
|
|
|
return null;
|
|
|
}
|
|
|
@@ -20,14 +40,12 @@ const Item = memo<ItemProps>((props: ItemProps) => {
|
|
|
const style = { margin: '10px', opacity: 1.0 };
|
|
|
if (page.isTarget) style.opacity = 0.7;
|
|
|
|
|
|
- /*
|
|
|
- * Normal render
|
|
|
- */
|
|
|
+ console.log('すべて', hasChildren(), currentChildren, itemNode);
|
|
|
return (
|
|
|
<div style={style}>
|
|
|
- <p>{page.path}</p>
|
|
|
+ <p>{page.path} <button type="button" onClick={onClickLoadChildren}>Load</button></p>
|
|
|
{
|
|
|
- itemNode.hasChildren() && (children as ItemNode[]).map(node => (
|
|
|
+ hasChildren() && currentChildren.map(node => (
|
|
|
<Item
|
|
|
key={node.page._id}
|
|
|
itemNode={node}
|
|
|
@@ -38,6 +56,6 @@ const Item = memo<ItemProps>((props: ItemProps) => {
|
|
|
</div>
|
|
|
);
|
|
|
|
|
|
-});
|
|
|
+};
|
|
|
|
|
|
export default Item;
|