@@ -10,27 +10,44 @@ import { FunctionComponent } from 'react';
10
10
import {
11
11
ClientConfiguration ,
12
12
createClient ,
13
- ClientInterface
13
+ ClientInterface ,
14
+ createNavigationByFoldersFetcher ,
15
+ createNavigationByTopicsFetcher
14
16
} from '@crystallize/js-api-client' ;
17
+ import type { TreeFetcher } from '@crystallize/js-api-client' ;
15
18
16
19
const StateContext = React . createContext < State | undefined > ( undefined ) ;
17
20
const DispatchContext = React . createContext < Dispatch | undefined > ( undefined ) ;
18
21
19
- const initialState = ( configuration : ClientConfiguration ) : State => {
22
+ const initialState = (
23
+ configuration : ClientConfiguration ,
24
+ language : string
25
+ ) : State => {
20
26
return {
21
27
loading : false ,
28
+ language : language ,
22
29
configuration : configuration
23
30
} ;
24
31
} ;
25
32
26
33
const CrystallizeProvider : FunctionComponent < {
34
+ language : string ;
27
35
tenantIdentifier : string ;
28
36
accessTokenId ?: string ;
29
37
accessTokenSecret ?: string ;
30
- } > = ( { tenantIdentifier, accessTokenId, accessTokenSecret, children } ) => {
38
+ } > = ( {
39
+ tenantIdentifier,
40
+ accessTokenId,
41
+ accessTokenSecret,
42
+ language,
43
+ children
44
+ } ) => {
31
45
const [ state , dispatch ] = React . useReducer (
32
46
Reducer ,
33
- initialState ( { tenantIdentifier, accessTokenId, accessTokenSecret } )
47
+ initialState (
48
+ { tenantIdentifier, accessTokenId, accessTokenSecret } ,
49
+ language
50
+ )
34
51
) ;
35
52
return (
36
53
< StateContext . Provider value = { state } >
@@ -61,17 +78,61 @@ function useCrystallizeDispatch() {
61
78
return context ;
62
79
}
63
80
81
+ export type LanguageAwareTreeFetcher = (
82
+ path : string ,
83
+ depth : number ,
84
+ extraQuery ?: any ,
85
+ perLevel ?: ( currentLevel : number ) => any
86
+ ) => Promise < any > ;
87
+
64
88
function useCrystallize ( ) : {
89
+ helpers : {
90
+ createNavigationByFoldersFetcher : LanguageAwareTreeFetcher ;
91
+ createNavigationByTopicsFetcher : LanguageAwareTreeFetcher ;
92
+ } ;
65
93
apiClient : ClientInterface ;
66
94
state : State ;
67
95
dispatch : Actions ;
68
96
} {
69
97
const actions = mapToReducerActions ( useCrystallizeDispatch ( ) ) ;
70
98
const state = useCrystallizeState ( ) ;
99
+
100
+ const apiClient = createClient ( {
101
+ tenantIdentifier : state . configuration . tenantIdentifier
102
+ } ) ;
103
+
104
+ const helpers = {
105
+ createNavigationByFoldersFetcher : (
106
+ path : string ,
107
+ depth : number = 1 ,
108
+ extraQuery ?: any ,
109
+ perLevel ?: ( currentLevel : number ) => any
110
+ ) =>
111
+ createNavigationByFoldersFetcher ( apiClient ) (
112
+ path ,
113
+ state . language ,
114
+ depth ,
115
+ extraQuery ,
116
+ perLevel
117
+ ) ,
118
+ createNavigationByTopicsFetcher : (
119
+ path : string ,
120
+ depth : number = 1 ,
121
+ extraQuery ?: any ,
122
+ perLevel ?: ( currentLevel : number ) => any
123
+ ) =>
124
+ createNavigationByFoldersFetcher ( apiClient ) (
125
+ path ,
126
+ state . language ,
127
+ depth ,
128
+ extraQuery ,
129
+ perLevel
130
+ )
131
+ } ;
132
+
71
133
return {
72
- apiClient : createClient ( {
73
- tenantIdentifier : state . configuration . tenantIdentifier
74
- } ) ,
134
+ helpers,
135
+ apiClient,
75
136
state,
76
137
dispatch : actions
77
138
} ;
0 commit comments