1+ import { createSelector } from '@reduxjs/toolkit' ;
2+ import { skipToken } from '@reduxjs/toolkit/query' ;
3+
4+ import { isEntityWithMergedImplementation } from '../../../containers/Tenant/utils/schema' ;
5+ import type { EPathType } from '../../../types/api/schema' ;
6+ import type { IDescribeData } from '../../../types/store/describe' ;
7+ import type { RootState } from '../../defaultStore' ;
18import { api } from '../api' ;
29
310export const overviewApi = api . injectEndpoints ( {
411 endpoints : ( build ) => ( {
5- getOverview : build . query ( {
12+ getMultiOverview : build . query ( {
613 queryFn : async ( { paths, database} : { paths : string [ ] ; database : string } , { signal} ) => {
714 try {
8- const [ data , ... additionalData ] = await Promise . all (
15+ const data = await Promise . all (
916 paths . map ( ( p ) =>
1017 window . api . getDescribe (
1118 {
@@ -16,7 +23,25 @@ export const overviewApi = api.injectEndpoints({
1623 ) ,
1724 ) ,
1825 ) ;
19- return { data : { data, additionalData} } ;
26+ return { data} ;
27+ } catch ( error ) {
28+ return { error} ;
29+ }
30+ } ,
31+ keepUnusedDataFor : 0 ,
32+ providesTags : [ 'All' ] ,
33+ } ) ,
34+ getOverview : build . query ( {
35+ queryFn : async ( { path, database} : { path : string ; database : string } , { signal} ) => {
36+ try {
37+ const data = await window . api . getDescribe (
38+ {
39+ path,
40+ database,
41+ } ,
42+ { signal} ,
43+ ) ;
44+ return { data} ;
2045 } catch ( error ) {
2146 return { error} ;
2247 }
@@ -26,3 +51,86 @@ export const overviewApi = api.injectEndpoints({
2651 } ) ,
2752 } ) ,
2853} ) ;
54+
55+ const getOverviewSelector = createSelector (
56+ ( path : string ) => path ,
57+ ( _path : string , database : string ) => database ,
58+ ( path , database ) => overviewApi . endpoints . getOverview . select ( { path, database} ) ,
59+ ) ;
60+
61+ const selectGetOverview = createSelector (
62+ ( state : RootState ) => state ,
63+ ( _state : RootState , path : string , database : string ) => getOverviewSelector ( path , database ) ,
64+ ( state , selectOverview ) => selectOverview ( state ) . data ,
65+ ) ;
66+
67+ const selectOverviewChildren = ( state : RootState , path : string , database : string ) =>
68+ selectGetOverview ( state , path , database ) ?. PathDescription ?. Children ;
69+
70+ export const selectSchemaMergedChildrenPaths = createSelector (
71+ [
72+ ( _ , path : string ) => path ,
73+ ( _ , _path , type : EPathType | undefined ) => type ,
74+ ( state , path , _type , database : string ) => selectOverviewChildren ( state , path , database ) ,
75+ ] ,
76+ ( path , type , children ) => {
77+ return isEntityWithMergedImplementation ( type )
78+ ? children ?. map ( ( { Name} ) => path + '/' + Name )
79+ : undefined ;
80+ } ,
81+ ) ;
82+
83+ //this hook is done not to refetch mainPath describe for entities with merged implementation
84+ export function useGetMultiOverviewQuery ( {
85+ paths,
86+ database,
87+ autoRefreshInterval,
88+ } : {
89+ paths : string [ ] ;
90+ database : string ;
91+ autoRefreshInterval ?: number ;
92+ } ) {
93+ const [ mainPath , ...additionalPaths ] = paths ;
94+
95+ const {
96+ currentData : mainDescribe ,
97+ isFetching : mainDescribeIsFetching ,
98+ error : mainDescribeError ,
99+ } = overviewApi . useGetOverviewQuery (
100+ {
101+ path : mainPath ,
102+ database,
103+ } ,
104+ {
105+ pollingInterval : autoRefreshInterval ,
106+ } ,
107+ ) ;
108+
109+ const {
110+ currentData : currentChindrenDescribe = [ ] ,
111+ isFetching : childrenDescribeIsFetching ,
112+ error : childrenDescribeError ,
113+ } = overviewApi . useGetMultiOverviewQuery (
114+ additionalPaths . length ? { paths : additionalPaths , database} : skipToken ,
115+ {
116+ pollingInterval : autoRefreshInterval ,
117+ } ,
118+ ) ;
119+
120+ const childrenDescribeLoading =
121+ childrenDescribeIsFetching && currentChindrenDescribe === undefined ;
122+ const mainDescribeLoading = mainDescribeIsFetching && mainDescribe === undefined ;
123+
124+ const loading = mainDescribeLoading || childrenDescribeLoading ;
125+
126+ const describe = [ mainDescribe , ...currentChindrenDescribe ] ;
127+
128+ const mergedDescribe = describe . reduce < IDescribeData > ( ( acc , item ) => {
129+ if ( item ?. Path ) {
130+ acc [ item . Path ] = item ;
131+ }
132+ return acc ;
133+ } , { } ) ;
134+
135+ return { loading, error : mainDescribeError || childrenDescribeError , mergedDescribe} ;
136+ }
0 commit comments