@@ -7,56 +7,41 @@ import {
7
7
SnackbarContent ,
8
8
TextField ,
9
9
} from '@mui/material' ;
10
+ import Link from 'next/link' ;
10
11
import React , { useEffect , useState } from 'react' ;
11
12
12
13
export default function FeedbackPopup ( ) {
13
- const [ feedbackOpen , setFeedbackOpen ] = useState ( false ) ;
14
+ const [ open , setOpen ] = useState ( false ) ;
14
15
15
16
//Open after 60 seconds if not already asked/close (based on localStorage)
16
17
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' ) {
26
23
ask = true ;
27
24
}
28
25
}
29
26
if ( ask ) {
30
27
const timer = setTimeout ( ( ) => {
31
- setFeedbackOpen ( true ) ;
28
+ setOpen ( true ) ;
32
29
} , 1000 * 60 ) ; //60 seconds
33
30
return ( ) => clearTimeout ( timer ) ;
34
31
}
35
32
} , [ ] ) ;
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
49
34
50
35
//Error submitting control
51
- const [ feedbackErrorOpen , setFeedbackErrorOpen ] = useState ( false ) ;
52
- function handlefeedbackErrorClose (
36
+ const [ errorOpen , setErrorOpen ] = useState ( false ) ;
37
+ function handleErrorClose (
53
38
event ?: React . SyntheticEvent | Event ,
54
39
reason ?: string ,
55
40
) {
56
41
if ( reason === 'clickaway' ) {
57
42
return ;
58
43
}
59
- setFeedbackErrorOpen ( false ) ;
44
+ setErrorOpen ( false ) ;
60
45
}
61
46
62
47
//Survey answer storage
@@ -65,46 +50,43 @@ export default function FeedbackPopup() {
65
50
66
51
//Send response to API, store result in localStorage
67
52
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
+ ) ;
76
73
} )
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
+ } ) ;
103
85
}
104
86
105
87
return (
106
88
< >
107
- < Snackbar open = { feedbackOpen } >
89
+ < Snackbar open = { open } >
108
90
< SnackbarContent
109
91
className = "bg-white dark:bg-haiti text-haiti dark:text-white"
110
92
sx = { {
@@ -139,14 +121,13 @@ export default function FeedbackPopup() {
139
121
/>
140
122
< p className = "text-xs" >
141
123
Visit our{ ' ' }
142
- < a
124
+ < Link
143
125
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"
146
127
target = "_blank"
147
128
>
148
129
GitHub
149
- </ a > { ' ' }
130
+ </ Link > { ' ' }
150
131
for more detailed issue reporting.
151
132
</ p >
152
133
</ div >
@@ -156,12 +137,12 @@ export default function FeedbackPopup() {
156
137
size = "small"
157
138
variant = "outlined"
158
139
onClick = { ( ) => {
159
- setFeedbackOpen ( false ) ;
140
+ setOpen ( false ) ;
160
141
localStorage . setItem (
161
142
'feedback' ,
162
143
JSON . stringify ( {
163
144
value : 'closed' ,
164
- cacheIndex : cacheIndexFeedback ,
145
+ cacheIndex : cacheIndex ,
165
146
} ) ,
166
147
) ;
167
148
} }
@@ -174,14 +155,8 @@ export default function FeedbackPopup() {
174
155
variant = "contained"
175
156
disabled = { feedbackRating === null }
176
157
onClick = { ( ) => {
177
- setFeedbackOpen ( false ) ;
178
- sendFeedback ( )
179
- . then ( ( ) => {
180
- setFeedbackSuccessOpen ( true ) ;
181
- } )
182
- . catch ( ( ) => {
183
- setFeedbackErrorOpen ( true ) ;
184
- } ) ;
158
+ setOpen ( false ) ;
159
+ sendFeedback ( ) ;
185
160
} }
186
161
>
187
162
Submit
@@ -192,25 +167,12 @@ export default function FeedbackPopup() {
192
167
/>
193
168
</ Snackbar >
194
169
< 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 }
209
171
autoHideDuration = { 6000 }
210
- onClose = { handlefeedbackErrorClose }
172
+ onClose = { handleErrorClose }
211
173
>
212
174
< Alert
213
- onClose = { handlefeedbackErrorClose }
175
+ onClose = { handleErrorClose }
214
176
severity = "error"
215
177
sx = { { width : '100%' } }
216
178
>
0 commit comments