|
@@ -3,72 +3,25 @@ import React, { FC } from 'react';
|
|
|
import { IPage } from '../../../interfaces/page';
|
|
import { IPage } from '../../../interfaces/page';
|
|
|
import { ItemNode } from './ItemNode';
|
|
import { ItemNode } from './ItemNode';
|
|
|
import Item from './Item';
|
|
import Item from './Item';
|
|
|
-
|
|
|
|
|
-/*
|
|
|
|
|
- * Mock data
|
|
|
|
|
- */
|
|
|
|
|
-const ancestors: (Partial<IPage> & {isTarget?: boolean})[] = [
|
|
|
|
|
- {
|
|
|
|
|
- path: '/',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- path: '/A',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- path: '/A/B',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- },
|
|
|
|
|
-];
|
|
|
|
|
-
|
|
|
|
|
-/*
|
|
|
|
|
- * Mock data
|
|
|
|
|
- */
|
|
|
|
|
-const targetAndSiblings: (Partial<IPage> & {isTarget?: boolean})[] = [
|
|
|
|
|
- {
|
|
|
|
|
- path: '/A/B/C',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- isTarget: true,
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- path: '/A/B/C2',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- path: '/A/B/C3',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- path: '/A/B/C4',
|
|
|
|
|
- isEmpty: false,
|
|
|
|
|
- grant: 1,
|
|
|
|
|
- },
|
|
|
|
|
-];
|
|
|
|
|
|
|
+import { useSWRxPageSiblings, useSWRxPageAncestors } from '../../../stores/page-listing';
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
* Utility to generate node tree and return the root node
|
|
* Utility to generate node tree and return the root node
|
|
|
*/
|
|
*/
|
|
|
const generateInitialTreeFromAncestors = (ancestors: Partial<IPage>[], siblings: Partial<IPage>[]): ItemNode => {
|
|
const generateInitialTreeFromAncestors = (ancestors: Partial<IPage>[], siblings: Partial<IPage>[]): ItemNode => {
|
|
|
- const rootPage = ancestors[0];
|
|
|
|
|
|
|
+ const rootPage = ancestors[ancestors.length - 1];
|
|
|
if (rootPage?.path !== '/') throw Error('/ not exist in ancestors');
|
|
if (rootPage?.path !== '/') throw Error('/ not exist in ancestors');
|
|
|
|
|
|
|
|
- const ancestorNodes = ancestors.map((page, i, array): ItemNode => {
|
|
|
|
|
- if (i === array.length - 1) {
|
|
|
|
|
|
|
+ const ancestorNodes = ancestors.map((page, i): ItemNode => {
|
|
|
|
|
+ if (i === 0) {
|
|
|
const siblingNodes = siblings.map(page => new ItemNode(page));
|
|
const siblingNodes = siblings.map(page => new ItemNode(page));
|
|
|
return new ItemNode(page, siblingNodes);
|
|
return new ItemNode(page, siblingNodes);
|
|
|
}
|
|
}
|
|
|
return new ItemNode(page, [], true);
|
|
return new ItemNode(page, [], true);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- const rootNode = ancestorNodes.reverse().reduce((child, parent) => {
|
|
|
|
|
|
|
+ const rootNode = ancestorNodes.reduce((child, parent) => {
|
|
|
parent.children = [child];
|
|
parent.children = [child];
|
|
|
return parent;
|
|
return parent;
|
|
|
});
|
|
});
|
|
@@ -80,8 +33,23 @@ const generateInitialTreeFromAncestors = (ancestors: Partial<IPage>[], siblings:
|
|
|
* ItemsTree
|
|
* ItemsTree
|
|
|
*/
|
|
*/
|
|
|
const ItemsTree: FC = () => {
|
|
const ItemsTree: FC = () => {
|
|
|
- // TODO: fetch ancestors, siblings using swr
|
|
|
|
|
- if (ancestors == null) return null;
|
|
|
|
|
|
|
+ // TODO: get from props
|
|
|
|
|
+ const path = '/Sandbox/Bootstrap4';
|
|
|
|
|
+ const id = '6181188ae38676152e464fc2';
|
|
|
|
|
+
|
|
|
|
|
+ const { data: ancestorsData, error: error1 } = useSWRxPageAncestors(path, id);
|
|
|
|
|
+ const { data: siblingsData, error: error2 } = useSWRxPageSiblings(path);
|
|
|
|
|
+
|
|
|
|
|
+ if (error1 != null || error2 != null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (ancestorsData == null || siblingsData == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const { ancestors } = ancestorsData;
|
|
|
|
|
+ const { targetAndSiblings } = siblingsData;
|
|
|
|
|
|
|
|
// create node tree
|
|
// create node tree
|
|
|
const rootNode = generateInitialTreeFromAncestors(ancestors, targetAndSiblings);
|
|
const rootNode = generateInitialTreeFromAncestors(ancestors, targetAndSiblings);
|