|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import Modal from './Modal'; |
| 4 | +import './FeedbackModal.css'; |
| 5 | +import { useAuth } from '../context/AuthContext'; |
| 6 | + |
| 7 | +const CATEGORY_OPTIONS = [ |
| 8 | + { value: 'feedback', label: 'General feedback' }, |
| 9 | + { value: 'bug', label: 'Report a bug' }, |
| 10 | + { value: 'idea', label: 'Feature request' }, |
| 11 | + { value: 'other', label: 'Something else' } |
| 12 | +]; |
| 13 | + |
| 14 | +function FeedbackModal({ isOpen, onClose }) { |
| 15 | + const { authenticated, user } = useAuth(); |
| 16 | + const [category, setCategory] = useState('feedback'); |
| 17 | + const [message, setMessage] = useState(''); |
| 18 | + const [contactInfo, setContactInfo] = useState(''); |
| 19 | + const [submitting, setSubmitting] = useState(false); |
| 20 | + const [error, setError] = useState(''); |
| 21 | + const [submitted, setSubmitted] = useState(false); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + if (isOpen) { |
| 25 | + setCategory('feedback'); |
| 26 | + setMessage(''); |
| 27 | + setError(''); |
| 28 | + setSubmitted(false); |
| 29 | + if (authenticated && user?.netid) { |
| 30 | + setContactInfo(`${user.netid}@princeton.edu`); |
| 31 | + } else { |
| 32 | + setContactInfo(''); |
| 33 | + } |
| 34 | + } |
| 35 | + }, [isOpen, authenticated, user]); |
| 36 | + |
| 37 | + const closeIfAllowed = () => { |
| 38 | + if (!submitting) { |
| 39 | + onClose(); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const handleSubmit = async (event) => { |
| 44 | + event.preventDefault(); |
| 45 | + if (submitting) return; |
| 46 | + |
| 47 | + const trimmedMessage = message.trim(); |
| 48 | + if (trimmedMessage.length < 10) { |
| 49 | + setError('Please include at least a few details so we can help.'); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + setSubmitting(true); |
| 54 | + setError(''); |
| 55 | + |
| 56 | + try { |
| 57 | + const response = await fetch('/api/feedback', { |
| 58 | + method: 'POST', |
| 59 | + headers: { 'Content-Type': 'application/json' }, |
| 60 | + body: JSON.stringify({ |
| 61 | + category, |
| 62 | + message: trimmedMessage, |
| 63 | + contactInfo: contactInfo.trim() || null, |
| 64 | + pagePath: typeof window !== 'undefined' ? window.location.pathname : null |
| 65 | + }) |
| 66 | + }); |
| 67 | + |
| 68 | + if (!response.ok) { |
| 69 | + const data = await response.json().catch(() => ({})); |
| 70 | + throw new Error(data.error || 'Unable to send feedback right now.'); |
| 71 | + } |
| 72 | + |
| 73 | + setSubmitted(true); |
| 74 | + setMessage(''); |
| 75 | + } catch (err) { |
| 76 | + setError(err.message || 'Unable to send feedback right now.'); |
| 77 | + } finally { |
| 78 | + setSubmitting(false); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <Modal |
| 84 | + isOpen={isOpen} |
| 85 | + onClose={closeIfAllowed} |
| 86 | + title={submitted ? 'Thanks for your feedback!' : 'Send Feedback'} |
| 87 | + showCloseButton |
| 88 | + isLarge={!submitted} |
| 89 | + > |
| 90 | + {submitted ? ( |
| 91 | + <div className="feedback-success"> |
| 92 | + <p>We appreciate you taking the time to help improve TigerType.</p> |
| 93 | + <button |
| 94 | + type="button" |
| 95 | + className="feedback-primary-button" |
| 96 | + onClick={closeIfAllowed} |
| 97 | + > |
| 98 | + Close |
| 99 | + </button> |
| 100 | + </div> |
| 101 | + ) : ( |
| 102 | + <form className="feedback-form" onSubmit={handleSubmit}> |
| 103 | + <label> |
| 104 | + Category |
| 105 | + <select |
| 106 | + value={category} |
| 107 | + onChange={(event) => setCategory(event.target.value)} |
| 108 | + disabled={submitting} |
| 109 | + > |
| 110 | + {CATEGORY_OPTIONS.map(option => ( |
| 111 | + <option key={option.value} value={option.value}> |
| 112 | + {option.label} |
| 113 | + </option> |
| 114 | + ))} |
| 115 | + </select> |
| 116 | + </label> |
| 117 | + |
| 118 | + <label> |
| 119 | + Describe what happened |
| 120 | + <textarea |
| 121 | + value={message} |
| 122 | + onChange={(event) => setMessage(event.target.value)} |
| 123 | + disabled={submitting} |
| 124 | + rows={8} |
| 125 | + maxLength={2000} |
| 126 | + placeholder="Share details, steps to reproduce, or anything else we should know." |
| 127 | + /> |
| 128 | + <span className="feedback-hint">{message.trim().length}/2000 characters</span> |
| 129 | + </label> |
| 130 | + |
| 131 | + <label> |
| 132 | + Contact (optional) |
| 133 | + <input |
| 134 | + type="email" |
| 135 | + value={contactInfo} |
| 136 | + onChange={(event) => setContactInfo(event.target.value)} |
| 137 | + disabled={submitting} |
| 138 | + placeholder="we'll follow up here if we need more info" |
| 139 | + /> |
| 140 | + </label> |
| 141 | + |
| 142 | + {error && <p className="feedback-error">{error}</p>} |
| 143 | + |
| 144 | + <div className="feedback-actions"> |
| 145 | + <button |
| 146 | + type="button" |
| 147 | + className="feedback-secondary-button" |
| 148 | + onClick={closeIfAllowed} |
| 149 | + disabled={submitting} |
| 150 | + > |
| 151 | + Cancel |
| 152 | + </button> |
| 153 | + <button |
| 154 | + type="submit" |
| 155 | + className="feedback-primary-button" |
| 156 | + disabled={submitting} |
| 157 | + > |
| 158 | + {submitting ? 'Sending…' : 'Send feedback'} |
| 159 | + </button> |
| 160 | + </div> |
| 161 | + </form> |
| 162 | + )} |
| 163 | + </Modal> |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +FeedbackModal.propTypes = { |
| 168 | + isOpen: PropTypes.bool.isRequired, |
| 169 | + onClose: PropTypes.func.isRequired |
| 170 | +}; |
| 171 | + |
| 172 | +export default FeedbackModal; |
0 commit comments