-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
form.tsx
177 lines (157 loc) · 4.61 KB
/
form.tsx
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React, {
ChangeEvent,
useCallback,
useContext,
useMemo,
useState,
} from 'react'
import clsx from 'clsx'
import {
DecisionsFormProps,
IntegrationConfigOptions,
} from '@consent-manager/core'
import { Trans } from './trans'
import { Integration } from './integration'
import { Styles, ButtonProps, IconProps, SwitchProps } from './index'
import { ConsentManagerDefaultInterfaceContext } from './context'
export interface ConsentFormProps extends DecisionsFormProps {
styles: Styles
ToggleIcon: React.FC<IconProps>
CloseIcon: React.FC<IconProps>
Switch: React.FC<SwitchProps>
Button: React.FC<ButtonProps>
}
interface FormState {
[key: string]: boolean
}
const ConsentForm: React.FC<ConsentFormProps> = ({
integrations,
initialValues,
onSubmit,
CloseIcon,
ToggleIcon,
styles,
Switch,
Button,
}) => {
const { setFormVisible } = useContext(ConsentManagerDefaultInterfaceContext)
const initialState = useMemo(() => {
const initialState: FormState = {}
for (const integration of integrations) {
initialState[integration.id] = initialValues.enabled.includes(
integration.id
)
}
return initialState
}, [integrations])
const defaultState = useMemo(() => {
const defaultState: FormState = {}
for (const integration of integrations) {
defaultState[integration.id] = integration.enabledByDefault || false
}
return defaultState
}, [integrations])
const [formState, setFormState] = useState<FormState>(initialState)
const cbOnSubmit = useCallback(() => {
const enabled = []
for (const [key, value] of Object.entries(formState)) {
if (value) {
enabled.push(key)
}
}
setFormVisible(false)
onSubmit({ enabled })
}, [onSubmit, setFormVisible, formState])
const cbReset = useCallback(
() => setFormState({ ...defaultState }),
[setFormState, defaultState]
)
const cbEnableAll = useCallback(() => {
setFormState((state) => {
Object.keys(state).forEach((key) => (state[key] = true))
return { ...state }
})
}, [setFormState])
const cbDisableAll = useCallback(() => {
setFormState((state) => {
Object.keys(state).forEach((key) => (state[key] = false))
return { ...state }
})
}, [setFormState])
const cbToggleIntegration = useCallback(
(e: ChangeEvent<HTMLInputElement>) =>
setFormState((state) => ({
...state,
[e.target.name]: !state[e.target.name],
})),
[]
)
const cbOnClose = useCallback(() => setFormVisible(false), [setFormVisible])
const controls = useMemo(
() => (
<div className={clsx(styles.formControls)}>
<Button type="button" onClick={cbReset}>
<Trans id="consent-manager.form.reset" />
</Button>
<Button type="button" onClick={cbDisableAll}>
<Trans id="consent-manager.form.disable-all" />
</Button>
<Button type="button" onClick={cbEnableAll}>
<Trans id="consent-manager.form.enable-all" />
</Button>
<Button type="submit" data-button-style="primary">
<Trans id="consent-manager.form.save" />
</Button>
</div>
),
[]
)
return (
<>
<form onSubmit={cbOnSubmit}>
<div className={clsx(styles.formIntro)}>
<div className={clsx(styles.formContent)}>
<h1 className={clsx(styles.formTitle)}>
<ToggleIcon className={clsx(styles.icon, styles.formIcon)} />
<Trans id="consent-manager.form.headline" />
</h1>
<Trans id="consent-manager.form.description" />
</div>
</div>
{controls}
<div className={clsx(styles.formIntegrations)}>
<div className={clsx(styles.formIntegrationsList)}>
{integrations.map((integration: IntegrationConfigOptions) => (
<Integration
styles={styles}
key={integration.id}
Switch={Switch}
checked={formState[integration.id]}
onChange={cbToggleIntegration}
{...integration}
/>
))}
</div>
</div>
{controls}
</form>
<Trans
id="consent-manager.close"
render={({ message }) => (
<button
className={clsx(
styles.buttonReset,
styles.buttonClose,
styles.formButtonClose
)}
onClick={cbOnClose}
title={message}
>
<CloseIcon className={clsx(styles.buttonCloseIcon)} />
</button>
)}
/>
</>
)
}
export default ConsentForm