-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserSettings.js
107 lines (99 loc) · 3.19 KB
/
UserSettings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React from 'react'
import Link from '@mui/material/Link'
import Typography from '@mui/material/Typography'
import {createOrderedMap} from '@ui-schema/ui-schema/Utils/createMap'
import {UIStoreProvider, createStore} from '@ui-schema/ui-schema/UIStore'
import {storeUpdater} from '@ui-schema/ui-schema/storeUpdater'
import {injectPluginStack} from '@ui-schema/ui-schema';
import {GridContainer} from '@ui-schema/ds-material/GridContainer';
const schema1 = {
type: "object",
title: "headline",
properties: {
call_count: {
type: "number",
minimum: 2,
maximum: 10,
view: {
sizeMd: 3
}
},
privacy: {
type: "boolean",
default: true,
view: {
sizeMd: 12
}
},
spam: {
type: "boolean",
view: {
sizeMd: 12
}
},
accepted: {
type: "boolean",
view: {
sizeMd: 12
}
},
type: {
type: "string",
widget: "Select",
default: "customer",
view: {
sizeMd: 3
},
enum: [
'customer',
'supplier',
'buyer',
'business',
'partner',
],
},
},
required: [
'call_count',
'type'
]
};
const GridStack = injectPluginStack(GridContainer)
const UserSettings = () => {
const [store, setStore] = React.useState(() => {
let data = false;
try {
data = JSON.parse(window.localStorage.getItem('user_settings'));
} catch(e) {
// not existing user_settings
}
return createStore(createOrderedMap(typeof data === 'object' ? data : {}))
});
const [schema,/* setSchema */] = React.useState(createOrderedMap(schema1));
const onChange = React.useCallback((actions) => {
setStore(prevStore => {
const newStore = storeUpdater(actions)(prevStore)
// if using a big schema this can be performance problematic!
// if using strings, throttle the `toJS` operation!
window.localStorage.setItem('user_settings', JSON.stringify(newStore.getValues().toJS()));
return newStore
})
}, [setStore])
return <React.Fragment>
<UIStoreProvider
store={store}
onChange={onChange}
showValidity
>
<GridStack isRoot schema={schema}/>
{/*
add children that should be under the schema editor,
they can use the UIStoreContext and UIConfigContext
*/}
</UIStoreProvider>
<Typography component={'p'} variant={'body1'} style={{marginTop: 24, marginBottom: 24}}>
This form saves the values onChange in the browsers <code>localStorage</code> and restores it at component mount, code in <Link href={'https://github.com/ui-schema/demo-cra/blob/master/src/Schema/UserSettings.js'}>src/Schema/UserSettings.js</Link>
</Typography>
</React.Fragment>;
};
export default UserSettings;