|
| 1 | +import React, { useState, useRef, useEffect } from 'react'; |
| 2 | +import { useSelector } from 'react-redux'; |
| 3 | +import styles from './FeedbackModal.module.css'; |
| 4 | + |
| 5 | +const ActivityFeedbackModal = ({ onClose }) => { |
| 6 | + const [rating, setRating] = useState(0); |
| 7 | + const [hover, setHover] = useState(0); |
| 8 | + const [comment, setComment] = useState(''); |
| 9 | + const [moreDetails, setMoreDetails] = useState(''); |
| 10 | + const [showMore, setShowMore] = useState(false); |
| 11 | + const [error, setError] = useState(''); |
| 12 | + const [loading, setLoading] = useState(false); |
| 13 | + const [submitted, setSubmitted] = useState(false); |
| 14 | + |
| 15 | + const darkMode = useSelector(state => state.theme.darkMode); |
| 16 | + |
| 17 | + const modalRef = useRef(null); |
| 18 | + const errorRef = useRef(null); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + const getFocusableElements = () => |
| 22 | + Array.from(modalRef.current?.querySelectorAll('button, textarea, [role="button"]') || []); |
| 23 | + |
| 24 | + modalRef.current?.focus(); |
| 25 | + |
| 26 | + const trap = e => { |
| 27 | + if (e.key !== 'Tab') return; |
| 28 | + const focusable = getFocusableElements(); |
| 29 | + const firstEl = focusable[0]; |
| 30 | + const lastEl = focusable[focusable.length - 1]; |
| 31 | + |
| 32 | + if (!firstEl || !lastEl) return; |
| 33 | + |
| 34 | + if (e.shiftKey && document.activeElement === firstEl) { |
| 35 | + e.preventDefault(); |
| 36 | + lastEl.focus(); |
| 37 | + } else if (!e.shiftKey && document.activeElement === lastEl) { |
| 38 | + e.preventDefault(); |
| 39 | + firstEl.focus(); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const escClose = e => { |
| 44 | + if (e.key === 'Escape') onClose(); |
| 45 | + }; |
| 46 | + |
| 47 | + document.addEventListener('keydown', trap); |
| 48 | + document.addEventListener('keydown', escClose); |
| 49 | + |
| 50 | + return () => { |
| 51 | + document.removeEventListener('keydown', trap); |
| 52 | + document.removeEventListener('keydown', escClose); |
| 53 | + }; |
| 54 | + }, [onClose]); |
| 55 | + |
| 56 | + const handleSubmit = () => { |
| 57 | + if (!rating) { |
| 58 | + setError('Please select a rating.'); |
| 59 | + setTimeout(() => errorRef.current?.scrollIntoView({ behavior: 'smooth' }), 100); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + setError(''); |
| 64 | + setLoading(true); |
| 65 | + |
| 66 | + setTimeout(() => { |
| 67 | + setSubmitted(true); |
| 68 | + setLoading(false); |
| 69 | + setTimeout(() => onClose(), 1200); |
| 70 | + }, 900); |
| 71 | + }; |
| 72 | + |
| 73 | + const FilledStar = ( |
| 74 | + <svg viewBox="0 0 24 24" className={styles.starFilled}> |
| 75 | + <path d="M12 .587l3.668 7.568L24 9.748l-6 5.848L19.335 24 12 19.897 4.665 24 6 15.596 0 9.748l8.332-1.593z" /> |
| 76 | + </svg> |
| 77 | + ); |
| 78 | + |
| 79 | + const EmptyStar = ( |
| 80 | + <svg viewBox="0 0 24 24" className={styles.starEmpty}> |
| 81 | + <path |
| 82 | + d="M12 .587l3.668 7.568L24 9.748l-6 5.848L19.335 24 12 19.897 4.665 24 6 15.596 0 9.748l8.332-1.593z" |
| 83 | + strokeWidth="2" |
| 84 | + /> |
| 85 | + </svg> |
| 86 | + ); |
| 87 | + |
| 88 | + return ( |
| 89 | + <div |
| 90 | + className={styles.overlay} |
| 91 | + role="presentation" |
| 92 | + onMouseDown={e => { |
| 93 | + if (e.target === e.currentTarget) onClose(); |
| 94 | + }} |
| 95 | + > |
| 96 | + <div |
| 97 | + ref={modalRef} |
| 98 | + className={darkMode ? styles.modalDark : styles.modalLight} |
| 99 | + role="dialog" |
| 100 | + aria-modal="true" |
| 101 | + tabIndex={-1} |
| 102 | + > |
| 103 | + <button className={styles.closeBtn} onClick={onClose} aria-label="Close feedback form"> |
| 104 | + ✕ |
| 105 | + </button> |
| 106 | + |
| 107 | + <h2 className={styles.heading}>Activity Feedback</h2> |
| 108 | + |
| 109 | + {submitted && <div className={styles.success}>Feedback submitted!</div>} |
| 110 | + |
| 111 | + <div className={styles.starContainer}> |
| 112 | + {[1, 2, 3, 4, 5].map(star => { |
| 113 | + const filled = star <= (hover || rating); |
| 114 | + return ( |
| 115 | + <span |
| 116 | + key={star} |
| 117 | + className={styles.starWrapper} |
| 118 | + tabIndex={0} |
| 119 | + role="button" |
| 120 | + aria-label={`Rate ${star} stars`} |
| 121 | + onMouseEnter={() => setHover(star)} |
| 122 | + onMouseLeave={() => setHover(0)} |
| 123 | + onClick={() => { |
| 124 | + setRating(star); |
| 125 | + setError(''); |
| 126 | + }} |
| 127 | + onKeyDown={e => { |
| 128 | + if (e.key === 'ArrowRight' && rating < 5) setRating(rating + 1); |
| 129 | + if (e.key === 'ArrowLeft' && rating > 1) setRating(rating - 1); |
| 130 | + if (e.key === 'Enter') setRating(star); |
| 131 | + }} |
| 132 | + > |
| 133 | + {filled ? FilledStar : EmptyStar} |
| 134 | + </span> |
| 135 | + ); |
| 136 | + })} |
| 137 | + </div> |
| 138 | + |
| 139 | + {error && ( |
| 140 | + <div ref={errorRef} className={styles.error}> |
| 141 | + {error} |
| 142 | + </div> |
| 143 | + )} |
| 144 | + |
| 145 | + <textarea |
| 146 | + className={styles.commentBox} |
| 147 | + maxLength={300} |
| 148 | + placeholder="Optional: Add your feedback" |
| 149 | + value={comment} |
| 150 | + onChange={e => setComment(e.target.value)} |
| 151 | + /> |
| 152 | + |
| 153 | + <div className={comment.length > 250 ? styles.charCountWarning : styles.charCount}> |
| 154 | + {comment.length}/300 |
| 155 | + </div> |
| 156 | + |
| 157 | + <button className={styles.moreDetailsBtn} onClick={() => setShowMore(!showMore)}> |
| 158 | + {showMore ? 'Hide Additional Details' : 'Add More Details'} |
| 159 | + </button> |
| 160 | + |
| 161 | + {showMore && ( |
| 162 | + <textarea |
| 163 | + className={styles.moreDetailsBox} |
| 164 | + placeholder="Additional optional information..." |
| 165 | + value={moreDetails} |
| 166 | + onChange={e => setMoreDetails(e.target.value)} |
| 167 | + /> |
| 168 | + )} |
| 169 | + |
| 170 | + <button |
| 171 | + className={rating ? styles.submitButton : styles.submitButtonDisabled} |
| 172 | + onClick={handleSubmit} |
| 173 | + aria-disabled={loading || !rating} |
| 174 | + > |
| 175 | + {loading ? 'Submitting...' : 'Submit Feedback'} |
| 176 | + </button> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + ); |
| 180 | +}; |
| 181 | + |
| 182 | +export default ActivityFeedbackModal; |
0 commit comments