-
Notifications
You must be signed in to change notification settings - Fork 3
/
FeedbackForm.jsx
299 lines (282 loc) · 8.62 KB
/
FeedbackForm.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
import {
Button,
Card,
CardContent,
DialogActions,
DialogContent,
Fab,
Dialog as MuiDialog,
TextField,
Typography,
styled
} from "@mui/material";
import {
MessageOutlined,
SentimentSatisfiedAlt,
SentimentVeryDissatisfied,
TextsmsOutlined
} from "@mui/icons-material";
import React, { useState } from "react";
import DialogTitle from "../DialogTitle";
import PropTypes from "prop-types";
// styling for the dialog
const Dialog = styled(MuiDialog)(({ theme }) => ({
"& .MuiDialogContent-root": {
padding: theme.spacing(2)
}
}));
// feedback form component with a button to open a dialog for users to submit feedback on an app
export default function FeedbackForm({
dialogPosition = "bottomLeft",
onSubmit,
open = false
}) {
// state for the dialog
const [dialogOpen, setDialogOpen] = React.useState(open);
const [page, setPage] = useState(0);
// state for user input
const [description, setDescription] = useState("");
const [sentiment, setSentiment] = useState("");
const [title, setTitle] = useState("");
// position styling for the paper
const paperStyle = {
bottomLeft: { left: 20, m: 0, position: "fixed", top: 80 },
leftBottom: { m: 0, position: "fixed", right: 80, top: 20 },
leftTop: { bottom: 20, m: 0, position: "fixed", right: 80 },
rightTop: { left: 80, m: 0, position: "fixed", top: 20 }
};
// update dialog open state when prop changes
React.useEffect(() => {
setDialogOpen(open);
}, [open]);
// handle open button click
const handleClickOpen = () => {
setDialogOpen(true);
};
// handle dialog close
const handleDialogClose = () => {
// close dialog
setDialogOpen(false);
// reset form
handleReset();
};
// handle sentiment selection
const handleSentimentChange = sentimentSelection => {
// update sentiment state
setSentiment(sentimentSelection);
// move to next page in the dialog
setPage(page => page + 1);
};
// handle submit button click
const handleSubmit = e => {
// call onSubmit with the form values
onSubmit({ description, sentiment, title, url: window.location.href });
// close dialog
setDialogOpen(false);
// reset form
handleReset();
};
// reset form
const handleReset = () => {
setDescription("");
setSentiment("");
setTitle("");
setPage(0);
};
// render components
return (
<>
<Fab color="primary" aria-label="add" onClick={() => handleClickOpen()}>
<MessageOutlined data-testid="open-button" />
</Fab>
<Dialog
PaperProps={{ sx: { ...paperStyle[dialogPosition] } }}
onClose={handleDialogClose}
aria-labelledby="customized-dialog-title"
open={dialogOpen}
>
<DialogTitle
id="customized-dialog-title"
data-testid="dia-title"
onClose={handleDialogClose}
>
Feedback
</DialogTitle>
<DialogContent>
{page === 0 ? (
<>
<Card
variant="outlined"
sx={{
"&:hover": {
border: "1px solid #cccccc",
boxShadow: "0px 4px 8px rgba(38, 38, 38, 0.5)",
cursor: "pointer",
top: "-4px",
transition: "all 0.2s ease-out"
},
minWidth: 275
}}
onClick={() => handleSentimentChange("I like something")}
data-testid="sentiment-card-like"
>
<CardContent>
<Typography
sx={{
alignItems: "center",
display: "flex",
flexWrap: "wrap",
fontSize: 18
}}
color="text.primary"
gutterBottom
>
<SentimentSatisfiedAlt sx={{ fontSize: 40, mr: 1.5 }} />
<span>I like Something</span>
</Typography>
<Typography sx={{ mb: 1 }} color="text.primary">
We like to hear what we're doing right.
</Typography>
</CardContent>
</Card>
<Card
variant="outlined"
sx={{
"&:hover": {
border: "1px solid #cccccc",
boxShadow: "0px 4px 8px rgba(38, 38, 38, 0.5)",
cursor: "pointer",
top: "-4px",
transition: "all 0.2s ease-out"
},
minWidth: 275,
mt: 2
}}
onClick={() => handleSentimentChange("I don't like Something")}
data-testid="sentiment-card-dislike"
>
<CardContent>
<Typography
sx={{
alignItems: "center",
display: "flex",
flexWrap: "wrap",
fontSize: 18
}}
color="text.primary"
gutterBottom
>
<SentimentVeryDissatisfied sx={{ fontSize: 40, mr: 1.5 }} />
<span>I don't like Something</span>
</Typography>
<Typography sx={{ mb: 1 }} color="text.primary">
If something's not right, we want to know about it.
</Typography>
</CardContent>
</Card>
<Card
variant="outlined"
sx={{
"&:hover": {
border: "1px solid #cccccc",
boxShadow: "0px 4px 8px rgba(38, 38, 38, 0.5)",
cursor: "pointer",
top: "-4px",
transition: "all 0.2s ease-out"
},
minWidth: 275,
mt: 2
}}
onClick={() => handleSentimentChange("I have a suggestion")}
data-testid="sentiment-card-suggestion"
>
<CardContent>
<Typography
sx={{
alignItems: "center",
display: "flex",
flexWrap: "wrap",
fontSize: 18
}}
color="text.primary"
gutterBottom
>
<TextsmsOutlined sx={{ fontSize: 40, mr: 1.5 }} />
<span>I have a suggestion</span>
</Typography>
<Typography sx={{ mb: 1 }} color="text.primary">
Share an idea or improvement.
</Typography>
</CardContent>
</Card>
</>
) : null}
{page === 1 ? (
<>
<TextField
required
autoFocus
margin="dense"
id="name"
label="Title"
type="text"
fullWidth
name="title"
variant="outlined"
value={title}
onChange={evt => setTitle(evt.target.value)}
data-testid="feedback-title-field"
/>
<TextField
margin="dense"
id="standard-multiline-static"
label="Description"
multiline
fullWidth
required
rows={4}
variant="outlined"
name="description"
value={description}
onChange={evt => setDescription(evt.target.value)}
data-testid="feedback-description-field"
/>
</>
) : null}
</DialogContent>
{page === 1 ? (
<DialogActions>
<Button onClick={handleSubmit} data-testid="submit-button">
Submit
</Button>
</DialogActions>
) : null}
</Dialog>
</>
);
}
FeedbackForm.propTypes = {
/**
* The position of the Dialog.
*/
dialogPosition: PropTypes.oneOf([
"rightTop",
"bottomLeft",
"leftTop",
"leftBottom"
]),
/**
* Callback fired when user clicks submit button
*
* **Signature**
* ```
* function(value: any) => void
* ```
* value: Object containing fields for sentiment, title, description and url.
*/
onSubmit: PropTypes.func,
/**
* If true, the dialog will show with dialog initially open. Default is false.
*/
open: PropTypes.bool
};