Skip to content

Commit 341bf38

Browse files
committed
Clean up feedback popup
1 parent 2b6ac8b commit 341bf38

File tree

1 file changed

+54
-92
lines changed

1 file changed

+54
-92
lines changed

Diff for: src/components/common/FeedbackPopup/feedbackPopup.tsx

+54-92
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,41 @@ import {
77
SnackbarContent,
88
TextField,
99
} from '@mui/material';
10+
import Link from 'next/link';
1011
import React, { useEffect, useState } from 'react';
1112

1213
export default function FeedbackPopup() {
13-
const [feedbackOpen, setFeedbackOpen] = useState(false);
14+
const [open, setOpen] = useState(false);
1415

1516
//Open after 60 seconds if not already asked/close (based on localStorage)
1617
useEffect(() => {
17-
const previousFeedback = localStorage.getItem('feedback');
18-
let ask = previousFeedback === null;
19-
if (previousFeedback !== null) {
20-
const parsedFeedback = JSON.parse(previousFeedback);
21-
if (
22-
parsedFeedback !== null &&
23-
parsedFeedback.value !== 'closed' &&
24-
parsedFeedback.value !== 'submitted'
25-
) {
18+
const previous = localStorage.getItem('feedback');
19+
let ask = previous === null;
20+
if (previous !== null) {
21+
const parsed = JSON.parse(previous);
22+
if (parsed !== null && parsed.value === 'error') {
2623
ask = true;
2724
}
2825
}
2926
if (ask) {
3027
const timer = setTimeout(() => {
31-
setFeedbackOpen(true);
28+
setOpen(true);
3229
}, 1000 * 60); //60 seconds
3330
return () => clearTimeout(timer);
3431
}
3532
}, []);
36-
const cacheIndexFeedback = 0; //Increment this to request feedback from all users on next deployment
37-
38-
//Successfully submitted control
39-
const [feedbackSuccessOpen, setFeedbackSuccessOpen] = useState(false);
40-
function handleFeedbackSuccessClose(
41-
event?: React.SyntheticEvent | Event,
42-
reason?: string,
43-
) {
44-
if (reason === 'clickaway') {
45-
return;
46-
}
47-
setFeedbackSuccessOpen(false);
48-
}
33+
const cacheIndex = 0; //Increment this to request feedback from all users on next deployment
4934

5035
//Error submitting control
51-
const [feedbackErrorOpen, setFeedbackErrorOpen] = useState(false);
52-
function handlefeedbackErrorClose(
36+
const [errorOpen, setErrorOpen] = useState(false);
37+
function handleErrorClose(
5338
event?: React.SyntheticEvent | Event,
5439
reason?: string,
5540
) {
5641
if (reason === 'clickaway') {
5742
return;
5843
}
59-
setFeedbackErrorOpen(false);
44+
setErrorOpen(false);
6045
}
6146

6247
//Survey answer storage
@@ -65,46 +50,43 @@ export default function FeedbackPopup() {
6550

6651
//Send response to API, store result in localStorage
6752
function sendFeedback() {
68-
return new Promise<void>((resolve, reject) => {
69-
fetch('/api/postFeedback', {
70-
method: 'POST',
71-
body: JSON.stringify({
72-
rating: feedbackRating,
73-
extra: feedbackExtra,
74-
env: process.env.NEXT_PUBLIC_VERCEL_ENV,
75-
}),
53+
fetch('/api/postFeedback', {
54+
method: 'POST',
55+
body: JSON.stringify({
56+
rating: feedbackRating,
57+
extra: feedbackExtra,
58+
env: process.env.NEXT_PUBLIC_VERCEL_ENV,
59+
}),
60+
})
61+
.then((response) => response.json())
62+
.then((data) => {
63+
if (data.message !== 'success') {
64+
throw new Error(data.message);
65+
}
66+
localStorage.setItem(
67+
'feedback',
68+
JSON.stringify({
69+
value: 'submitted',
70+
cacheIndex: cacheIndex,
71+
}),
72+
);
7673
})
77-
.then((response) => response.json())
78-
.then((data) => {
79-
if (data.message !== 'success') {
80-
throw new Error(data.message);
81-
}
82-
localStorage.setItem(
83-
'feedback',
84-
JSON.stringify({
85-
value: 'submitted',
86-
cacheIndex: cacheIndexFeedback,
87-
}),
88-
);
89-
resolve();
90-
})
91-
.catch((error) => {
92-
localStorage.setItem(
93-
'feedback',
94-
JSON.stringify({
95-
value: 'error',
96-
cacheIndex: cacheIndexFeedback,
97-
}),
98-
);
99-
console.error('Feedback', error);
100-
reject();
101-
});
102-
});
74+
.catch((error) => {
75+
setErrorOpen(true);
76+
localStorage.setItem(
77+
'feedback',
78+
JSON.stringify({
79+
value: 'error',
80+
cacheIndex: cacheIndex,
81+
}),
82+
);
83+
console.error('Feedback', error);
84+
});
10385
}
10486

10587
return (
10688
<>
107-
<Snackbar open={feedbackOpen}>
89+
<Snackbar open={open}>
10890
<SnackbarContent
10991
className="bg-white dark:bg-haiti text-haiti dark:text-white"
11092
sx={{
@@ -139,14 +121,13 @@ export default function FeedbackPopup() {
139121
/>
140122
<p className="text-xs">
141123
Visit our{' '}
142-
<a
124+
<Link
143125
className="underline text-blue-600 hover:text-blue-800 visited:text-purple-600"
144-
href="https://github.com/UTDNebula/utd-trends"
145-
rel="noopener noreferrer"
126+
href="https://github.com/UTDNebula/utd-trends/issues/new/choose"
146127
target="_blank"
147128
>
148129
GitHub
149-
</a>{' '}
130+
</Link>{' '}
150131
for more detailed issue reporting.
151132
</p>
152133
</div>
@@ -156,12 +137,12 @@ export default function FeedbackPopup() {
156137
size="small"
157138
variant="outlined"
158139
onClick={() => {
159-
setFeedbackOpen(false);
140+
setOpen(false);
160141
localStorage.setItem(
161142
'feedback',
162143
JSON.stringify({
163144
value: 'closed',
164-
cacheIndex: cacheIndexFeedback,
145+
cacheIndex: cacheIndex,
165146
}),
166147
);
167148
}}
@@ -174,14 +155,8 @@ export default function FeedbackPopup() {
174155
variant="contained"
175156
disabled={feedbackRating === null}
176157
onClick={() => {
177-
setFeedbackOpen(false);
178-
sendFeedback()
179-
.then(() => {
180-
setFeedbackSuccessOpen(true);
181-
})
182-
.catch(() => {
183-
setFeedbackErrorOpen(true);
184-
});
158+
setOpen(false);
159+
sendFeedback();
185160
}}
186161
>
187162
Submit
@@ -192,25 +167,12 @@ export default function FeedbackPopup() {
192167
/>
193168
</Snackbar>
194169
<Snackbar
195-
open={feedbackSuccessOpen}
196-
autoHideDuration={6000}
197-
onClose={handleFeedbackSuccessClose}
198-
>
199-
<Alert
200-
onClose={handleFeedbackSuccessClose}
201-
severity="success"
202-
sx={{ width: '100%' }}
203-
>
204-
Feedback submitted. Thank you!
205-
</Alert>
206-
</Snackbar>
207-
<Snackbar
208-
open={feedbackErrorOpen}
170+
open={errorOpen}
209171
autoHideDuration={6000}
210-
onClose={handlefeedbackErrorClose}
172+
onClose={handleErrorClose}
211173
>
212174
<Alert
213-
onClose={handlefeedbackErrorClose}
175+
onClose={handleErrorClose}
214176
severity="error"
215177
sx={{ width: '100%' }}
216178
>

0 commit comments

Comments
 (0)