Skip to content

Commit 8b84efc

Browse files
committed
[core] Extend core/i18n reducers with actions to set schema and uischema
1 parent 2ba0316 commit 8b84efc

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

packages/core/src/actions/index.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ export const REMOVE_FIELD: 'jsonforms/REMOVE_FIELD' = 'jsonforms/REMOVE_FIELD';
4040
export const SET_CONFIG : 'jsonforms/SET_CONFIG' = 'jsonforms/SET_CONFIG';
4141
export const ADD_UI_SCHEMA: 'jsonforms/ADD_UI_SCHEMA' = `jsonforms/ADD_UI_SCHEMA`;
4242
export const REMOVE_UI_SCHEMA: 'jsonforms/REMOVE_UI_SCHEMA' = `jsonforms/REMOVE_UI_SCHEMA`;
43+
export const SET_SCHEMA: 'jsonforms/SET_SCHEMA' = `jsonforms/SET_SCHEMA`;
44+
export const SET_UISCHEMA: 'jsonforms/SET_UISCHEMA' = `jsonforms/SET_UISCHEMA`;
4345

4446
export const SET_LOCALE: 'jsonforms/SET_LOCALE' = `jsonforms/SET_LOCALE`;
47+
export const SET_LOCALIZED_SCHEMAS: 'jsonforms/SET_LOCALIZED_SCHEMAS' =
48+
`jsonforms/SET_LOCALIZED_SCHEMAS`;
49+
export const SET_LOCALIZED_UISCHEMAS: 'jsonforms/SET_LOCALIZED_UISCHEMAS' =
50+
`jsonforms/SET_LOCALIZED_UISCHEMAS`;
4551

4652
export const ADD_DEFAULT_DATA: 'jsonforms/ADD_DEFAULT_DATA' = `jsonforms/ADD_DEFAULT_DATA`;
4753
export const REMOVE_DEFAULT_DATA: 'jsonforms/REMOVE_DEFAULT_DATA' = `jsonforms/REMOVE_DEFAULT_DATA`;
@@ -217,7 +223,7 @@ export const unregisterUISchema = (
217223
): RemoveUISchemaAction => {
218224
return {
219225
type: REMOVE_UI_SCHEMA,
220-
tester,
226+
tester
221227
};
222228
};
223229

@@ -231,3 +237,49 @@ export const setLocale = (locale: string): SetLocaleAction =>
231237
type: SET_LOCALE,
232238
locale,
233239
});
240+
241+
export interface SetLocalizedSchemasAction {
242+
type: 'jsonforms/SET_LOCALIZED_SCHEMAS';
243+
localizedSchemas: Map<string, JsonSchema>;
244+
}
245+
246+
export const setLocalizedSchemas =
247+
(localizedSchemas: Map<string, JsonSchema>): SetLocalizedSchemasAction =>
248+
({
249+
type: SET_LOCALIZED_SCHEMAS,
250+
localizedSchemas
251+
});
252+
253+
export interface SetSchemaAction {
254+
type: 'jsonforms/SET_SCHEMA';
255+
schema: JsonSchema;
256+
}
257+
258+
export const setSchema = (schema: JsonSchema): SetSchemaAction =>
259+
({
260+
type: SET_SCHEMA,
261+
schema
262+
});
263+
264+
export interface SetLocalizedUISchemasAction {
265+
type: 'jsonforms/SET_LOCALIZED_UISCHEMAS';
266+
localizedUISchemas: Map<string, UISchemaElement>;
267+
}
268+
269+
export const setLocalizedUISchemas =
270+
(localizedUISchemas: Map<string, UISchemaElement>): SetLocalizedUISchemasAction =>
271+
({
272+
type: SET_LOCALIZED_UISCHEMAS,
273+
localizedUISchemas
274+
});
275+
276+
export interface SetUISchemaAction {
277+
type: 'jsonforms/SET_UISCHEMA';
278+
uischema: UISchemaElement;
279+
}
280+
281+
export const setUISchema = (uischema: UISchemaElement): SetUISchemaAction =>
282+
({
283+
type: SET_UISCHEMA,
284+
uischema
285+
});

packages/core/src/reducers/core.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@
2424
*/
2525
import * as _ from 'lodash';
2626
import { ErrorObject, ValidateFunction } from 'ajv';
27-
import { INIT, InitAction, SET_AJV, SetAjvAction, UPDATE_DATA, UpdateAction } from '../actions';
27+
import {
28+
INIT,
29+
InitAction,
30+
SET_AJV,
31+
SET_SCHEMA,
32+
SET_UISCHEMA,
33+
SetAjvAction,
34+
SetSchemaAction,
35+
SetUISchemaAction,
36+
UPDATE_DATA,
37+
UpdateAction
38+
} from '../actions';
2839
import { createAjv } from '../util/validator';
2940
import { JsonSchema, UISchemaElement } from '..';
3041

@@ -63,7 +74,8 @@ const initState: JsonFormsCore = {
6374
validator: alwaysValid
6475
};
6576

66-
type ValidCoreActions = InitAction | UpdateAction | SetAjvAction;
77+
type ValidCoreActions =
78+
InitAction | UpdateAction | SetAjvAction | SetSchemaAction | SetUISchemaAction;
6779

6880
export const coreReducer = (
6981
state: JsonFormsCore = initState,
@@ -94,6 +106,18 @@ export const coreReducer = (
94106
errors
95107
};
96108
}
109+
case SET_SCHEMA: {
110+
return {
111+
...state,
112+
schema: action.schema
113+
};
114+
}
115+
case SET_UISCHEMA: {
116+
return {
117+
...state,
118+
uischema: action.uischema
119+
};
120+
}
97121
case UPDATE_DATA: {
98122

99123
if (action.path === undefined || action.path === null) {

packages/core/src/reducers/i18n.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,35 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import { SET_LOCALE } from '../actions';
25+
import { SET_LOCALE, SET_LOCALIZED_SCHEMAS } from '../actions';
26+
import { JsonSchema, SET_LOCALIZED_UISCHEMAS, UISchemaElement } from '..';
2627

2728
export interface JsonFormsLocaleState {
2829
locale?: string;
30+
localizedSchemas: Map<string, JsonSchema>;
31+
localizedUISchemas: Map<string, UISchemaElement>;
2932
}
3033

3134
const initState: JsonFormsLocaleState = {
32-
locale: undefined
35+
locale: undefined,
36+
localizedSchemas: new Map(),
37+
localizedUISchemas: new Map()
3338
};
3439

3540
export const i18nReducer = (
3641
state = initState,
3742
action: any) => {
3843
switch (action.type) {
44+
case SET_LOCALIZED_SCHEMAS:
45+
return {
46+
...state,
47+
localizedSchemas: action.localizedSchemas
48+
};
49+
case SET_LOCALIZED_UISCHEMAS:
50+
return {
51+
...state,
52+
localizedUISchemas: action.localizedUISchemas
53+
};
3954
case SET_LOCALE:
4055
return {
4156
...state,
@@ -51,4 +66,22 @@ export const fetchLocale = (state?: JsonFormsLocaleState) => {
5166
return undefined;
5267
}
5368
return state.locale;
54-
}
69+
};
70+
71+
export const findLocalizedSchema =
72+
(locale: string) =>
73+
(state?: JsonFormsLocaleState): JsonSchema => {
74+
if (state === undefined) {
75+
return undefined;
76+
}
77+
return state.localizedSchemas.get(locale);
78+
};
79+
80+
export const findLocalizedUISchema =
81+
(locale: string) =>
82+
(state?: JsonFormsLocaleState): UISchemaElement => {
83+
if (state === undefined) {
84+
return undefined;
85+
}
86+
return state.localizedUISchemas.get(locale);
87+
};

packages/core/src/reducers/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
import { JsonFormsState } from '../store';
4444
import { findMatchingUISchema, uischemaRegistryReducer, UISchemaTester } from './uischemas';
4545
import { Generate, JsonSchema, UISchemaElement } from '..';
46-
import { fetchLocale, i18nReducer } from './i18n';
46+
import { fetchLocale, findLocalizedSchema, i18nReducer, findLocalizedUISchema } from './i18n';
4747

4848
export {
4949
rendererReducer,
@@ -97,3 +97,9 @@ export const getSubErrorsAt = (instancePath: string) => (state: JsonFormsState)
9797
export const getConfig = (state: JsonFormsState) => state.jsonforms.config;
9898

9999
export const getLocale = (state: JsonFormsState) => fetchLocale(_.get(state, 'jsonforms.i18n'));
100+
101+
export const getLocalizedSchema = (locale: string) => (state: JsonFormsState): JsonSchema =>
102+
findLocalizedSchema(locale)(_.get(state, 'jsonforms.i18n'));
103+
104+
export const getLocalizedUISchema = (locale: string) => (state: JsonFormsState): UISchemaElement =>
105+
findLocalizedUISchema(locale)(_.get(state, 'jsonforms.i18n'));

0 commit comments

Comments
 (0)