/
index.jsx
381 lines (359 loc) · 14 KB
/
index.jsx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import React from 'react';
import styles from './styles.module.css';
import CloseIcon from '@site/src/icons/close';
const join = (...cs) => cs.join(' ');
export const initialData = {
currentStep: 1,
data: {
score: undefined,
comment: undefined,
customerType: undefined,
},
};
const fetchData = (initialData) => {
const localstorageKey = 'user-feedback';
return {
currentStep: 1,
...initialData,
data: {
score: undefined,
comment: undefined,
customerType: undefined,
...initialData?.data,
},
initialized: Date.now(),
closedOrCompleted: false,
};
// check localstorage
// populate if there is
};
const stateReducer = (state, message) => {
switch (message.kind) {
case 'clear':
return fetchData(seedData);
case 'set score':
return {
...state,
data: { ...state.data, score: message.data },
};
case 'set comment':
return {
...state,
data: { ...state.data, comment: message.data },
};
case 'set customer type':
return {
...state,
data: { ...state.data, customerType: message.data },
};
case 'step forward':
return {
...state,
currentStep: Math.min(state.currentStep + 1, 3),
};
case 'step back':
return {
...state,
currentStep: Math.max(state.currentStep - 1, 1),
};
}
return state;
};
export const FeedbackWrapper = ({ seedData, open }) => {
const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(open);
const [manuallyOpened, setManuallyOpened] = React.useState(open);
const [state, dispatch] = React.useReducer(
stateReducer,
seedData,
fetchData,
);
console.log(state, state.data);
const clear = () => dispatch({ kind: 'clear' });
const stepForward = () => {
console.log('stepping forward!');
dispatch({ kind: 'step forward' });
};
const stepBack = () => dispatch({ kind: 'step back' });
const setScore = (score) => dispatch({ kind: 'set score', data: score });
const setComment = (comment) =>
dispatch({ kind: 'set comment', data: comment });
const setCustomerType = (customerType) =>
dispatch({ kind: 'set customer type', data: customerType });
const submitFeedback = () => {
console.log('send feedback here ');
};
const visuallyHidden = (stepNumber) => state.currentStep !== stepNumber;
const isHidden = (stepNumber) =>
!feedbackIsOpen || visuallyHidden(stepNumber);
const Step1 = () => {
const hidden = isHidden(1);
const [newValue, setNewValue] = React.useState(state.data.score);
return (
<form
className={visuallyHidden(1) ? styles['invisible'] : ''}
onSubmit={(e) => {
e.preventDefault();
setScore(newValue);
stepForward();
}}
aria-hidden={hidden}
>
<fieldset disabled={hidden}>
<p>
<span className="visually-hidden">
On a scale from 1 to 5 where 1 is very unsatisfied
and 5 is very satisfied,
</span>{' '}
How would you rate your overall satisfaction with the
Unleash documentation?
</p>
<div className={styles['satisfaction-input-container']}>
<span
aria-hidden="true"
className={
styles['satisfaction-input-visual-label']
}
>
Very unsatisfied
</span>
<span className={styles['satisfaction-input-inputs']}>
{[1, 2, 3, 4, 5].map((n, i) => (
<span key={`input-group-${n}`}>
<input
className={join(
'visually-hidden',
styles[
'user-satisfaction-score-input'
],
)}
required
id={`user-satisfaction-score-${n}`}
name="satisfaction-level"
type="radio"
value={n}
defaultChecked={n === state.data.score}
onChange={(e) => {
const value = parseInt(
e.target.value,
);
console.log('the value is', value);
setNewValue(value);
}}
autoFocus={
manuallyOpened
? state.data.score
? state.data.score === n
: i === 0
: false
}
/>
<label
className={
styles[
'user-satisfaction-score-label'
]
}
htmlFor={`user-satisfaction-score-${n}`}
>
{n}
</label>
</span>
))}
</span>
<span
aria-hidden="true"
className={
styles['satisfaction-input-visual-label']
}
>
Very satisfied
</span>
</div>
<div className={styles['button-container']}>
<button type="submit">Next</button>
</div>
</fieldset>
</form>
);
};
const Step2 = () => {
const hidden = isHidden(2);
const textareaId = 'feedback-comment-input';
const saveComment = () =>
setComment(document.getElementById(textareaId).value);
return (
<form
className={visuallyHidden(2) ? styles['invisible'] : ''}
aria-hidden={hidden}
onSubmit={(e) => {
e.preventDefault();
saveComment();
stepForward();
}}
>
<fieldset disabled={hidden}>
<label htmlFor={textareaId}>
What would you like to see improved in the Unleash
documentation?
</label>
<textarea
id={textareaId}
name=""
rows="3"
autoFocus
defaultValue={state.data.comment}
></textarea>
<div className={styles['button-container']}>
<button type="submit">Next</button>
<button
className={styles['button-secondary']}
type="button"
onClick={() => {
saveComment();
stepForward();
}}
>
Skip
</button>
<button
className={styles['button-secondary']}
type="button"
onClick={() => {
saveComment();
stepBack();
}}
>
Back
</button>
</div>
</fieldset>
</form>
);
};
const Step3 = () => {
const hidden = isHidden(3);
const [value, setValue] = React.useState(state.data.customerType);
return (
<form
className={visuallyHidden(3) ? styles['invisible'] : ''}
aria-hidden={hidden}
onSubmit={(e) => {
e.preventDefault();
setCustomerType(value);
submitFeedback();
}}
>
<fieldset disabled={hidden}>
<span>
Finally, are you a paying customer or an open source
customer of Unleash?
</span>
<div className={styles['customer-type-inputs']}>
{[
['a', 'paying', 'paying'],
['an', 'open source', 'opensource'],
].map(([article, customerType, key], i) => (
<span key={`input-group-${key}`}>
<input
autoFocus={
state.data.customerType
? state.data.customerType === key
: i === 0
}
id={`customer-type-${key}`}
className={styles['customer-type-input']}
name="customer-type"
type="radio"
value={key}
defaultChecked={
key === state.data.customerType
}
onChange={(e) => {
const value = e.target.value;
setValue(value);
}}
/>
<label
className={styles['customer-type-label']}
htmlFor={`customer-type-${key}`}
>
I'm {article} {customerType} customer
</label>
</span>
))}
</div>
<div className={styles['button-container']}>
<button type="submit">Submit feedback</button>
<button
className={styles['button-secondary']}
type="button"
onClick={() => {
setCustomerType(value);
stepBack();
}}
>
Back
</button>
</div>
</fieldset>
</form>
);
};
const Step4 = () => {
return (
<div>
<p className={styles['thank-you']}>Thank you! 🙌</p>
<button
className={styles['button-secondary']}
onClick={() => setFeedbackIsOpen(false)}
>
close
</button>
</div>
);
};
return (
<div className={styles['user-feedback-container']}>
<button
aria-hidden={feedbackIsOpen}
className={join(
styles['open-feedback-button'],
styles['primary-button'],
)}
disabled={feedbackIsOpen}
onClick={() => {
setFeedbackIsOpen(true);
setManuallyOpened(true);
}}
>
<span>Feedback</span>
</button>
<article
aria-hidden={!feedbackIsOpen}
className={join(
styles['user-feedback'],
feedbackIsOpen ? '' : styles['invisible'],
)}
>
<div className={styles['close-button-row']}>
<button
onClick={() => setFeedbackIsOpen(false)}
className={styles['close-button']}
disabled={!feedbackIsOpen}
>
<span className="visually-hidden">
close feedback popup
</span>
<CloseIcon />
</button>
</div>
<div className={styles['form-section-container']}>
<Step1 />
<Step2 />
<Step3 />
{/* <Step4 /> */}
</div>
</article>
</div>
);
};