wip
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { APIError, errorCauses, fetchAPI } from '@/api';
|
||||
|
||||
import { Doc } from '../../doc-management';
|
||||
|
||||
export type DocsTreeParams = {
|
||||
docId: string;
|
||||
page: number;
|
||||
page_size?: number;
|
||||
};
|
||||
|
||||
export const getDocTree = async (params: DocsTreeParams): Promise<Doc[]> => {
|
||||
const { docId, page, page_size } = params;
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
if (page) {
|
||||
searchParams.set('page', page.toString());
|
||||
}
|
||||
if (page_size) {
|
||||
searchParams.set('page_size', page_size.toString());
|
||||
}
|
||||
|
||||
const response = await fetchAPI(
|
||||
`documents/${docId}/tree/?${searchParams.toString()}`,
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new APIError(
|
||||
'Failed to get the doc tree',
|
||||
await errorCauses(response),
|
||||
);
|
||||
}
|
||||
|
||||
return response.json() as Promise<Doc[]>;
|
||||
};
|
||||
|
||||
export const KEY_LIST_DOC_CHILDREN = 'doc-tree';
|
||||
|
||||
export function useDocTree(
|
||||
params: DocsTreeParams,
|
||||
queryConfig?: Omit<
|
||||
UseQueryOptions<Doc[], APIError, Doc[]>,
|
||||
'queryKey' | 'queryFn'
|
||||
>,
|
||||
) {
|
||||
return useQuery<Doc[], APIError, Doc[]>({
|
||||
queryKey: [KEY_LIST_DOC_CHILDREN, params],
|
||||
queryFn: () => getDocTree(params),
|
||||
...queryConfig,
|
||||
});
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import { useCunninghamTheme } from '@/cunningham';
|
||||
|
||||
import { Doc, useDoc } from '../../doc-management';
|
||||
import { SimpleDocItem } from '../../docs-grid';
|
||||
import { useInfiniteDocChildren } from '../api/useDocChildren';
|
||||
import { useDocTree } from '../api/useDocTree';
|
||||
import { TreeViewDataType, TreeViewMoveResult } from '../types/tree';
|
||||
|
||||
import { DocTreeItem } from './DocTreeItem';
|
||||
@@ -31,7 +31,7 @@ export const DocTree = ({ docId }: Props) => {
|
||||
treeData: treeDataStore,
|
||||
} = useTreeStore();
|
||||
|
||||
const { data, isLoading, isFetching, isRefetching } = useInfiniteDocChildren({
|
||||
const { data, isLoading, isFetching, isRefetching } = useDocTree({
|
||||
docId,
|
||||
page_size: 25,
|
||||
page: 1,
|
||||
@@ -62,7 +62,10 @@ export const DocTree = ({ docId }: Props) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const newChildren = data.pages.flatMap((page) => page.results);
|
||||
console.log(data);
|
||||
const newChildren = data ?? [];
|
||||
const root = newChildren.shift();
|
||||
console.log('root', root);
|
||||
const newData: DocTreeDataType[] = newChildren.map((child) => ({
|
||||
...child,
|
||||
childrenCount: child.numchild,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useModal } from '@openfun/cunningham-react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useState } from 'react';
|
||||
import { Fragment, useState } from 'react';
|
||||
import { NodeRendererProps } from 'react-arborist';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -102,7 +102,7 @@ export const DocTreeItem = ({ node, ...props }: DocTreeItemProps) => {
|
||||
},
|
||||
];
|
||||
return (
|
||||
<>
|
||||
<Fragment>
|
||||
<TreeViewNode
|
||||
onClick={() => router.push(`/docs/${node.data.id}`)}
|
||||
node={node}
|
||||
@@ -150,6 +150,6 @@ export const DocTreeItem = ({ node, ...props }: DocTreeItemProps) => {
|
||||
<ModalRenameDoc onClose={renameModal.onClose} doc={node.data} />,
|
||||
document.getElementById('modals') as HTMLElement,
|
||||
)}
|
||||
</>
|
||||
</Fragment>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user