1
1
import React , { useCallback , useContext , useMemo } from 'react' ;
2
2
import { useStore , useSelector , useDispatch } from 'react-redux' ;
3
- import { pathOr } from 'ramda' ;
3
+ import { concat , pathOr } from 'ramda' ;
4
4
5
5
import { DashLayoutPath } from '../types/component' ;
6
6
import { LoadingPayload } from '../actions/loading' ;
7
7
8
+ type LoadingFilterFunc = ( loading : LoadingPayload ) => boolean ;
9
+
10
+ type LoadingOptions = {
11
+ /**
12
+ *
13
+ */
14
+ extraPath ?: DashLayoutPath ;
15
+ /**
16
+ *
17
+ */
18
+ rawPath ?: boolean ;
19
+ /**
20
+ * Function used to filter the properties of the loading component.
21
+ */
22
+ filterFunc ?: LoadingFilterFunc ;
23
+ } ;
24
+
8
25
type DashContextType = {
9
26
componentPath : DashLayoutPath ;
10
27
11
- isLoading : ( ) => boolean ;
12
- useLoading : ( ) => boolean ;
28
+ isLoading : ( options ?: LoadingOptions ) => boolean ;
29
+ useLoading : ( options ?: LoadingOptions ) => boolean ;
13
30
14
31
// Give access to the right store.
15
32
useSelector : typeof useSelector ;
16
33
useDispatch : typeof useDispatch ;
17
34
useStore : typeof useStore ;
18
35
} ;
19
36
20
- type LoadingFilterFunc = ( loading : LoadingPayload ) => boolean ;
21
-
22
37
export const DashContext = React . createContext < DashContextType > ( { } as any ) ;
23
38
24
39
type DashContextProviderProps = {
@@ -35,19 +50,42 @@ export function DashContextProvider(props: DashContextProviderProps) {
35
50
) ;
36
51
const store = useStore ( ) ;
37
52
38
- const isLoading = useCallback ( ( ) => {
39
- const loading = pathOr (
40
- [ ] ,
41
- [ stringPath ] ,
42
- ( store . getState ( ) as any ) . loading
43
- ) ;
44
- return loading . length > 0 ;
45
- } , [ stringPath ] ) ;
53
+ const isLoading = useCallback (
54
+ ( options ?: LoadingOptions ) => {
55
+ const { extraPath, rawPath, filterFunc} = options || { } ;
56
+ let loadingPath = [ stringPath ] ;
57
+ if ( extraPath ) {
58
+ loadingPath = [
59
+ JSON . stringify ( concat ( componentPath , extraPath ) )
60
+ ] ;
61
+ } else if ( rawPath ) {
62
+ loadingPath = [ JSON . stringify ( rawPath ) ] ;
63
+ }
64
+ const loading = pathOr (
65
+ [ ] ,
66
+ loadingPath ,
67
+ ( store . getState ( ) as any ) . loading
68
+ ) ;
69
+ return filterFunc
70
+ ? loading . filter ( filterFunc ) . length > 0
71
+ : loading . length > 0 ;
72
+ } ,
73
+ [ stringPath ]
74
+ ) ;
46
75
47
76
const useLoading = useCallback (
48
- ( filterFunc ?: LoadingFilterFunc ) => {
77
+ ( options ?: LoadingOptions ) => {
78
+ const { filterFunc, extraPath, rawPath} = options || { } ;
49
79
return useSelector ( ( state : any ) => {
50
- const load = pathOr ( [ ] , [ stringPath ] , state . loading ) ;
80
+ let loadingPath = [ stringPath ] ;
81
+ if ( extraPath ) {
82
+ loadingPath = [
83
+ JSON . stringify ( concat ( componentPath , extraPath ) )
84
+ ] ;
85
+ } else if ( rawPath ) {
86
+ loadingPath = [ JSON . stringify ( rawPath ) ] ;
87
+ }
88
+ const load = pathOr ( [ ] , loadingPath , state . loading ) ;
51
89
if ( filterFunc ) {
52
90
return load . filter ( filterFunc ) . length > 0 ;
53
91
}
0 commit comments