3232 :tickeable =" true"
3333 :ui-namespace =" localUiNamespace"
3434 :i18n-scope =" localI18n"
35- :nodes =" props. items"
35+ :nodes =" items"
3636 :node-types =" []"
3737 />
3838 <q-btn
4747</template >
4848
4949<script setup lang="ts">
50- import type { LinidQBtnProps } from ' @linagora/linid-im-front-corelib' ;
51- import { LinidFilterValue } from ' @linagora/linid-im-front-corelib' ;
52- import { useScopedI18n , useUiDesign } from ' @linagora/linid-im-front-corelib' ;
50+ import type {
51+ LinidQBtnProps ,
52+ Page ,
53+ Pagination ,
54+ TreeNode ,
55+ } from ' @linagora/linid-im-front-corelib' ;
56+ import {
57+ useNotify ,
58+ getHttpClient ,
59+ LinidFilterValue ,
60+ useScopedI18n ,
61+ useUiDesign ,
62+ } from ' @linagora/linid-im-front-corelib' ;
5363import GenericTree from ' ../tree/GenericTree.vue' ;
5464import type { TreeSearchFilterProps } from ' ../../types/TreeSearchFilterPanel' ;
55- import { ref } from ' vue' ;
65+ import { onMounted , ref } from ' vue' ;
5666import type { LinidFilterPanelSearchOutputs } from ' ../../types/linidFilterPanel' ;
5767
5868const props = defineProps <TreeSearchFilterProps >();
@@ -64,6 +74,8 @@ const { t } = useScopedI18n(localI18n);
6474const localUiNamespace = ` ${props .uiNamespace }.tree-search-filter-panel ` ;
6575
6676const tickedNodeKeys = ref <string []>([]);
77+ const items = ref <TreeNode <Record <string , unknown >>[]>([]);
78+ const { Notify } = useNotify ();
6779
6880const uiProps = {
6981 searchButton: ui <LinidQBtnProps >(localUiNamespace , ' q-btn' ),
@@ -83,6 +95,121 @@ function onSearch() {
8395 values: filterValues ,
8496 });
8597}
98+
99+ /**
100+ * Fetches nodes from the specified URL with pagination.
101+ * @param pagination - Pagination parameters for the request.
102+ * @returns A promise that resolves to a page of nodes.
103+ */
104+ async function getNodes(pagination : Pagination ) {
105+ const response = await getHttpClient ().get <Page <Record <string , unknown >>>(
106+ props .url ,
107+ { params: { ... pagination } }
108+ );
109+ if (! response || ! response .data ) {
110+ return { content: [], last: true };
111+ }
112+ const { data } = response ;
113+ return data ;
114+ }
115+
116+ /**
117+ * Fetches all nodes from the specified URL by iterating through paginated results.
118+ * @returns A promise that resolves to an array of all nodes.
119+ */
120+ async function getAllNodes(): Promise <Record <string , unknown >[]> {
121+ const result: Record <string , unknown >[] = [];
122+ let page = 0 ;
123+ let isLast = false ;
124+
125+ while (! isLast ) {
126+ const response = await getNodes ({ page , size: props .nodesQuerySize });
127+
128+ if (! response || ! response .content ) {
129+ break ;
130+ }
131+
132+ result .push (... response .content );
133+ isLast = response .last ?? true ;
134+ page ++ ;
135+ }
136+
137+ return result ;
138+ }
139+
140+ /**
141+ * Maps an array of nodes to an array of TreeNode objects.
142+ * @param nodes - The array of nodes to map.
143+ * @param idKey - The key used to identify each node.
144+ * @returns An array of TreeNode objects.
145+ */
146+ function toTreeNode(
147+ nodes : Record <string , unknown >[],
148+ idKey : string
149+ ): TreeNode <Record <string , unknown >>[] {
150+ const childNodes = nodes .filter ((item ) => {
151+ const parentValue = item [props .parentsKey ];
152+ return (
153+ Array .isArray (parentValue ) &&
154+ parentValue .some ((parent ) => parent [props .parentIdKey ] === idKey )
155+ );
156+ });
157+ if (childNodes && childNodes .length > 0 ) {
158+ const result: TreeNode <Record <string , unknown >>[] = [];
159+ childNodes .forEach ((childNode : Record <string , unknown >) => {
160+ result .push ({
161+ key: String (childNode [props .idKey ]),
162+ value: childNode ,
163+ nodes: toTreeNode (nodes , String (childNode [props .idKey ])),
164+ type:
165+ (childNode [props .typeKey ] as string ) || props .defaultTypeValue || ' ' ,
166+ });
167+ });
168+ return result ;
169+ }
170+ return [];
171+ }
172+
173+ /**
174+ * Fetches all nodes, identifies the root node, and constructs the tree structure for the component.
175+ */
176+ async function loadData() {
177+ try {
178+ const nodes: Record <string , unknown >[] = await getAllNodes ();
179+
180+ if (! nodes || nodes .length === 0 ) {
181+ return ;
182+ }
183+
184+ const root = nodes .find ((item ) => {
185+ const parentValue = item [props .parentsKey ];
186+ return (
187+ Array .isArray (parentValue ) &&
188+ parentValue .length > 0 &&
189+ parentValue [0 ][props .parentIdKey ] === null
190+ );
191+ });
192+
193+ if (root ) {
194+ items .value .push ({
195+ key: String (root [props .idKey ]),
196+ value: root ,
197+ nodes: toTreeNode (nodes , String (root [props .idKey ])),
198+ type: ' root' ,
199+ });
200+ }
201+ } catch (error ) {
202+ Notify ({
203+ type: ' negative' ,
204+ message: t (' errorLoadingData' ),
205+ });
206+ throw error ;
207+ }
208+ }
209+
210+ onMounted (() => {
211+ loadData ();
212+ });
86213 </script >
87214
88215<style scoped></style >
0 commit comments