Skip to content

Commit a7df111

Browse files
committed
✨ Inject language and helpers in useCrystallize
1 parent 6891571 commit a7df111

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ It provides a `CrystallizeProvider` on which you can get:
2424
import { CrystallizeProvider } from '@crystallize/reactjs-hooks';
2525
ReactDOM.render(
2626
<React.StrictMode>
27-
<CrystallizeProvider tenantIdentifier="furniture">
27+
<CrystallizeProvider language="en" tenantIdentifier="furniture">
2828
<App />
2929
</CrystallizeProvider>
3030
</React.StrictMode>,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
},
3333
"license": "MIT",
3434
"name": "@crystallize/reactjs-hooks",
35-
"version": "0.2.1"
35+
"version": "0.3.0"
3636
}

src/core/MainProvider/MainProvider.tsx

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,44 @@ import { FunctionComponent } from 'react';
1010
import {
1111
ClientConfiguration,
1212
createClient,
13-
ClientInterface
13+
ClientInterface,
14+
createNavigationByFoldersFetcher,
15+
createNavigationByTopicsFetcher
1416
} from '@crystallize/js-api-client';
17+
import type { TreeFetcher } from '@crystallize/js-api-client';
1518

1619
const StateContext = React.createContext<State | undefined>(undefined);
1720
const DispatchContext = React.createContext<Dispatch | undefined>(undefined);
1821

19-
const initialState = (configuration: ClientConfiguration): State => {
22+
const initialState = (
23+
configuration: ClientConfiguration,
24+
language: string
25+
): State => {
2026
return {
2127
loading: false,
28+
language: language,
2229
configuration: configuration
2330
};
2431
};
2532

2633
const CrystallizeProvider: FunctionComponent<{
34+
language: string;
2735
tenantIdentifier: string;
2836
accessTokenId?: string;
2937
accessTokenSecret?: string;
30-
}> = ({ tenantIdentifier, accessTokenId, accessTokenSecret, children }) => {
38+
}> = ({
39+
tenantIdentifier,
40+
accessTokenId,
41+
accessTokenSecret,
42+
language,
43+
children
44+
}) => {
3145
const [state, dispatch] = React.useReducer(
3246
Reducer,
33-
initialState({ tenantIdentifier, accessTokenId, accessTokenSecret })
47+
initialState(
48+
{ tenantIdentifier, accessTokenId, accessTokenSecret },
49+
language
50+
)
3451
);
3552
return (
3653
<StateContext.Provider value={state}>
@@ -61,17 +78,61 @@ function useCrystallizeDispatch() {
6178
return context;
6279
}
6380

81+
export type LanguageAwareTreeFetcher = (
82+
path: string,
83+
depth: number,
84+
extraQuery?: any,
85+
perLevel?: (currentLevel: number) => any
86+
) => Promise<any>;
87+
6488
function useCrystallize(): {
89+
helpers: {
90+
createNavigationByFoldersFetcher: LanguageAwareTreeFetcher;
91+
createNavigationByTopicsFetcher: LanguageAwareTreeFetcher;
92+
};
6593
apiClient: ClientInterface;
6694
state: State;
6795
dispatch: Actions;
6896
} {
6997
const actions = mapToReducerActions(useCrystallizeDispatch());
7098
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+
71133
return {
72-
apiClient: createClient({
73-
tenantIdentifier: state.configuration.tenantIdentifier
74-
}),
134+
helpers,
135+
apiClient,
75136
state,
76137
dispatch: actions
77138
};

src/core/MainProvider/Reducer.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import { ClientConfiguration } from '@crystallize/js-api-client';
22

33
type Action =
44
| { type: 'LOADING'; state: boolean }
5-
| { type: 'UPDATE_CONFIGURATION'; configuration: ClientConfiguration };
5+
| { type: 'UPDATE_CONFIGURATION'; configuration: ClientConfiguration }
6+
| { type: 'CHANGE_LANGUAGE'; language: string };
67

78
export type Actions = {
89
loading: (state: boolean) => void;
910
updateConfiguration: (configuration: ClientConfiguration) => void;
11+
changeLanguage: (language: string) => void;
1012
};
1113

1214
export type Dispatch = (action: Action) => void;
1315

1416
export type State = {
1517
configuration: ClientConfiguration;
1618
loading: boolean;
19+
language: string;
1720
};
1821

1922
export function Reducer(state: State, action: Action) {
@@ -24,6 +27,12 @@ export function Reducer(state: State, action: Action) {
2427
loading: action.state
2528
};
2629
}
30+
case 'CHANGE_LANGUAGE': {
31+
return {
32+
...state,
33+
language: action.language
34+
};
35+
}
2736
case 'UPDATE_CONFIGURATION': {
2837
return {
2938
...state,
@@ -39,6 +48,8 @@ export function Reducer(state: State, action: Action) {
3948
export function mapToReducerActions(dispatch: Dispatch): Actions {
4049
return {
4150
loading: (state: boolean) => dispatch({ type: 'LOADING', state }),
51+
changeLanguage: (language: string) =>
52+
dispatch({ type: 'CHANGE_LANGUAGE', language }),
4253
updateConfiguration: (configuration: ClientConfiguration) =>
4354
dispatch({ type: 'UPDATE_CONFIGURATION', configuration })
4455
};

0 commit comments

Comments
 (0)