forked from Ammaar-Alam/tigertype
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedbackModal.jsx
More file actions
172 lines (155 loc) · 4.83 KB
/
Copy pathFeedbackModal.jsx
File metadata and controls
172 lines (155 loc) · 4.83 KB
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
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import Modal from './Modal';
import './FeedbackModal.css';
import { useAuth } from '../context/AuthContext';
const CATEGORY_OPTIONS = [
{ value: 'feedback', label: 'General feedback' },
{ value: 'bug', label: 'Report a bug' },
{ value: 'idea', label: 'Feature request' },
{ value: 'other', label: 'Something else' }
];
function FeedbackModal({ isOpen, onClose }) {
const { authenticated, user } = useAuth();
const [category, setCategory] = useState('feedback');
const [message, setMessage] = useState('');
const [contactInfo, setContactInfo] = useState('');
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState('');
const [submitted, setSubmitted] = useState(false);
useEffect(() => {
if (isOpen) {
setCategory('feedback');
setMessage('');
setError('');
setSubmitted(false);
if (authenticated && user?.netid) {
setContactInfo(`${user.netid}@princeton.edu`);
} else {
setContactInfo('');
}
}
}, [isOpen, authenticated, user]);
const closeIfAllowed = () => {
if (!submitting) {
onClose();
}
};
const handleSubmit = async (event) => {
event.preventDefault();
if (submitting) return;
const trimmedMessage = message.trim();
if (trimmedMessage.length < 10) {
setError('Please include at least a few details so we can help.');
return;
}
setSubmitting(true);
setError('');
try {
const response = await fetch('/api/feedback', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
category,
message: trimmedMessage,
contactInfo: contactInfo.trim() || null,
pagePath: typeof window !== 'undefined' ? window.location.pathname : null
})
});
if (!response.ok) {
const data = await response.json().catch(() => ({}));
throw new Error(data.error || 'Unable to send feedback right now.');
}
setSubmitted(true);
setMessage('');
} catch (err) {
setError(err.message || 'Unable to send feedback right now.');
} finally {
setSubmitting(false);
}
};
return (
<Modal
isOpen={isOpen}
onClose={closeIfAllowed}
title={submitted ? 'Thanks for your feedback!' : 'Send Feedback'}
showCloseButton
isLarge={!submitted}
>
{submitted ? (
<div className="feedback-success">
<p>We appreciate you taking the time to help improve TigerType.</p>
<button
type="button"
className="feedback-primary-button"
onClick={closeIfAllowed}
>
Close
</button>
</div>
) : (
<form className="feedback-form" onSubmit={handleSubmit}>
<label>
Category
<select
value={category}
onChange={(event) => setCategory(event.target.value)}
disabled={submitting}
>
{CATEGORY_OPTIONS.map(option => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</label>
<label>
Describe what happened
<textarea
value={message}
onChange={(event) => setMessage(event.target.value)}
disabled={submitting}
rows={8}
maxLength={2000}
placeholder="Share details, steps to reproduce, or anything else we should know."
/>
<span className="feedback-hint">{message.trim().length}/2000 characters</span>
</label>
<label>
Contact (optional)
<input
type="email"
value={contactInfo}
onChange={(event) => setContactInfo(event.target.value)}
disabled={submitting}
placeholder="we'll follow up here if we need more info"
/>
</label>
{error && <p className="feedback-error">{error}</p>}
<div className="feedback-actions">
<button
type="button"
className="feedback-secondary-button"
onClick={closeIfAllowed}
disabled={submitting}
>
Cancel
</button>
<button
type="submit"
className="feedback-primary-button"
disabled={submitting}
>
{submitting ? 'Sending…' : 'Send feedback'}
</button>
</div>
</form>
)}
</Modal>
);
}
FeedbackModal.propTypes = {
isOpen: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired
};
export default FeedbackModal;