|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { ChevronDown, MessageSquare, Send } from 'lucide-react'; |
| 4 | +import { useTranslations } from 'next-intl'; |
| 5 | +import { useEffect, useRef, useState } from 'react'; |
| 6 | + |
| 7 | +interface FeedbackFormProps { |
| 8 | + userName?: string | null; |
| 9 | + userEmail?: string | null; |
| 10 | +} |
| 11 | + |
| 12 | +export function FeedbackForm({ userName, userEmail }: FeedbackFormProps) { |
| 13 | + const t = useTranslations('dashboard.feedback'); |
| 14 | + const [loading, setLoading] = useState(false); |
| 15 | + const [status, setStatus] = useState<'idle' | 'success' | 'error'>('idle'); |
| 16 | + const [categoryOpen, setCategoryOpen] = useState(false); |
| 17 | + const [category, setCategory] = useState(''); |
| 18 | + const [categoryError, setCategoryError] = useState(false); |
| 19 | + const categoryRef = useRef<HTMLDivElement>(null); |
| 20 | + const successTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 21 | + |
| 22 | + const categories = [ |
| 23 | + { value: 'Bug Report', label: t('categoryBug') }, |
| 24 | + { value: 'Suggestion', label: t('categorySuggestion') }, |
| 25 | + { value: 'Question', label: t('categoryQuestion') }, |
| 26 | + { value: 'Other', label: t('categoryOther') }, |
| 27 | + ]; |
| 28 | + |
| 29 | + const selectedLabel = categories.find(c => c.value === category)?.label; |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + const handleClickOutside = (event: MouseEvent) => { |
| 33 | + if ( |
| 34 | + categoryRef.current && |
| 35 | + !categoryRef.current.contains(event.target as Node) |
| 36 | + ) { |
| 37 | + setCategoryOpen(false); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const handleSmoothScroll = (e: MouseEvent) => { |
| 42 | + const target = e.target as HTMLElement; |
| 43 | + const anchor = target.closest('a[href="#feedback"]'); |
| 44 | + if (anchor) { |
| 45 | + e.preventDefault(); |
| 46 | + document |
| 47 | + .getElementById('feedback') |
| 48 | + ?.scrollIntoView({ behavior: 'smooth' }); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + document.addEventListener('mousedown', handleClickOutside); |
| 53 | + document.addEventListener('click', handleSmoothScroll); |
| 54 | + return () => { |
| 55 | + document.removeEventListener('mousedown', handleClickOutside); |
| 56 | + document.removeEventListener('click', handleSmoothScroll); |
| 57 | + if (successTimerRef.current) clearTimeout(successTimerRef.current); |
| 58 | + }; |
| 59 | + }, []); |
| 60 | + |
| 61 | + const cardStyles = ` |
| 62 | + relative overflow-hidden rounded-2xl |
| 63 | + border border-gray-200 dark:border-white/10 |
| 64 | + bg-white/60 dark:bg-neutral-900/60 backdrop-blur-xl |
| 65 | + p-4 sm:p-6 md:p-8 transition-all hover:border-(--accent-primary)/30 dark:hover:border-(--accent-primary)/30 |
| 66 | + `; |
| 67 | + |
| 68 | + const inputStyles = |
| 69 | + 'w-full rounded-xl border border-gray-200 dark:border-white/10 bg-white/50 dark:bg-neutral-800/50 px-4 py-3 text-sm text-gray-900 dark:text-white placeholder-gray-400 dark:placeholder-gray-500 outline-none transition-colors focus:border-(--accent-primary) focus:ring-1 focus:ring-(--accent-primary)'; |
| 70 | + |
| 71 | + const primaryBtnStyles = ` |
| 72 | + group relative inline-flex items-center justify-center gap-2 rounded-full |
| 73 | + px-8 py-3 text-sm font-semibold tracking-widest uppercase text-white |
| 74 | + bg-(--accent-primary) hover:bg-(--accent-hover) |
| 75 | + transition-all hover:scale-105 disabled:opacity-50 disabled:hover:scale-100 |
| 76 | + `; |
| 77 | + |
| 78 | + async function onSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 79 | + e.preventDefault(); |
| 80 | + if (!category) { |
| 81 | + setCategoryError(true); |
| 82 | + return; |
| 83 | + } |
| 84 | + setCategoryError(false); |
| 85 | + setLoading(true); |
| 86 | + setStatus('idle'); |
| 87 | + |
| 88 | + const accessKey = process.env.NEXT_PUBLIC_WEB3FORMS_KEY; |
| 89 | + if (!accessKey) { |
| 90 | + console.error( |
| 91 | + 'FeedbackForm: NEXT_PUBLIC_WEB3FORMS_KEY is not defined. Add it to your .env file.' |
| 92 | + ); |
| 93 | + setStatus('error'); |
| 94 | + setLoading(false); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const formData = new FormData(e.currentTarget); |
| 99 | + |
| 100 | + const data = { |
| 101 | + access_key: accessKey, |
| 102 | + subject: `DevLovers Feedback: ${formData.get('category')}`, |
| 103 | + from_name: formData.get('name'), |
| 104 | + email: formData.get('email'), |
| 105 | + category: formData.get('category'), |
| 106 | + message: formData.get('message'), |
| 107 | + botcheck: '', |
| 108 | + }; |
| 109 | + |
| 110 | + try { |
| 111 | + const res = await fetch('https://api.web3forms.com/submit', { |
| 112 | + method: 'POST', |
| 113 | + headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, |
| 114 | + body: JSON.stringify(data), |
| 115 | + }); |
| 116 | + |
| 117 | + const result = await res.json(); |
| 118 | + |
| 119 | + if (result.success) { |
| 120 | + setStatus('success'); |
| 121 | + setCategory(''); |
| 122 | + (e.target as HTMLFormElement).reset(); |
| 123 | + successTimerRef.current = setTimeout(() => setStatus('idle'), 5000); |
| 124 | + } else { |
| 125 | + setStatus('error'); |
| 126 | + } |
| 127 | + } catch { |
| 128 | + setStatus('error'); |
| 129 | + } finally { |
| 130 | + setLoading(false); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return ( |
| 135 | + <section className={cardStyles} aria-labelledby="feedback-heading"> |
| 136 | + <div className="mb-6 flex items-center gap-3"> |
| 137 | + <div |
| 138 | + className="rounded-full bg-gray-100 p-3 dark:bg-neutral-800/50" |
| 139 | + aria-hidden="true" |
| 140 | + > |
| 141 | + <MessageSquare className="h-5 w-5 text-(--accent-primary)" /> |
| 142 | + </div> |
| 143 | + <div> |
| 144 | + <h3 |
| 145 | + id="feedback-heading" |
| 146 | + className="text-xl font-bold text-gray-900 dark:text-white" |
| 147 | + > |
| 148 | + {t('title')} |
| 149 | + </h3> |
| 150 | + <p className="text-sm text-gray-500 dark:text-gray-400"> |
| 151 | + {t('description')} |
| 152 | + </p> |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + |
| 156 | + {status === 'success' ? ( |
| 157 | + <div className="rounded-xl border border-emerald-200 bg-emerald-50 p-4 text-sm font-medium text-emerald-700 dark:border-emerald-500/20 dark:bg-emerald-500/10 dark:text-emerald-400"> |
| 158 | + {t('success')} |
| 159 | + </div> |
| 160 | + ) : ( |
| 161 | + <form onSubmit={onSubmit} className="space-y-4"> |
| 162 | + <input type="hidden" name="botcheck" className="hidden" /> |
| 163 | + |
| 164 | + <div className="grid gap-4 sm:grid-cols-2"> |
| 165 | + <input |
| 166 | + name="name" |
| 167 | + type="text" |
| 168 | + placeholder={t('name')} |
| 169 | + defaultValue={userName ?? ''} |
| 170 | + required |
| 171 | + onInvalid={e => (e.target as HTMLInputElement).setCustomValidity(t('requiredField'))} |
| 172 | + onInput={e => (e.target as HTMLInputElement).setCustomValidity('')} |
| 173 | + className={inputStyles} |
| 174 | + /> |
| 175 | + <input |
| 176 | + name="email" |
| 177 | + type="email" |
| 178 | + placeholder={t('email')} |
| 179 | + defaultValue={userEmail ?? ''} |
| 180 | + required |
| 181 | + onInvalid={e => (e.target as HTMLInputElement).setCustomValidity(t('requiredField'))} |
| 182 | + onInput={e => (e.target as HTMLInputElement).setCustomValidity('')} |
| 183 | + className={inputStyles} |
| 184 | + /> |
| 185 | + </div> |
| 186 | + |
| 187 | + <input type="hidden" name="category" value={category} /> |
| 188 | + <div className="relative" ref={categoryRef}> |
| 189 | + <button |
| 190 | + type="button" |
| 191 | + onClick={() => setCategoryOpen(!categoryOpen)} |
| 192 | + className={`flex w-full items-center justify-between rounded-xl border bg-gray-50/50 px-4 py-3 text-left text-sm font-medium text-gray-700 transition-colors hover:bg-gray-100 dark:bg-neutral-800/50 dark:text-gray-300 dark:hover:bg-neutral-800 ${categoryError ? 'border-red-400 dark:border-red-500/50' : 'border-gray-200 dark:border-white/5'}`} |
| 193 | + > |
| 194 | + <span className={selectedLabel ? '' : 'text-gray-400 dark:text-gray-500'}> |
| 195 | + {selectedLabel ?? t('category')} |
| 196 | + </span> |
| 197 | + <ChevronDown |
| 198 | + className={`h-4 w-4 transition-transform ${categoryOpen ? 'rotate-180' : ''}`} |
| 199 | + /> |
| 200 | + </button> |
| 201 | + {categoryOpen && ( |
| 202 | + <div className="absolute left-0 z-50 mt-1 w-full rounded-xl border border-gray-200 bg-white py-1 shadow-lg dark:border-neutral-800 dark:bg-neutral-900"> |
| 203 | + {categories.map(c => ( |
| 204 | + <button |
| 205 | + key={c.value} |
| 206 | + type="button" |
| 207 | + onClick={() => { |
| 208 | + setCategory(c.value); |
| 209 | + setCategoryError(false); |
| 210 | + setCategoryOpen(false); |
| 211 | + }} |
| 212 | + className={`block w-full px-4 py-2 text-left text-sm transition-colors ${ |
| 213 | + category === c.value |
| 214 | + ? 'bg-(--accent-primary)/10 font-medium text-(--accent-primary)' |
| 215 | + : 'text-gray-700 hover:bg-gray-50 dark:text-gray-300 dark:hover:bg-neutral-800' |
| 216 | + }`} |
| 217 | + > |
| 218 | + {c.label} |
| 219 | + </button> |
| 220 | + ))} |
| 221 | + </div> |
| 222 | + )} |
| 223 | + {categoryError && ( |
| 224 | + <p className="mt-1 text-xs text-red-500 dark:text-red-400"> |
| 225 | + {t('requiredField')} |
| 226 | + </p> |
| 227 | + )} |
| 228 | + </div> |
| 229 | + |
| 230 | + <textarea |
| 231 | + name="message" |
| 232 | + placeholder={t('messagePlaceholder')} |
| 233 | + required |
| 234 | + onInvalid={e => (e.target as HTMLTextAreaElement).setCustomValidity(t('requiredField'))} |
| 235 | + onInput={e => (e.target as HTMLTextAreaElement).setCustomValidity('')} |
| 236 | + rows={4} |
| 237 | + className={`${inputStyles} resize-none`} |
| 238 | + /> |
| 239 | + |
| 240 | + {status === 'error' && ( |
| 241 | + <div className="rounded-xl border border-red-200 bg-red-50 p-3 text-sm font-medium text-red-700 dark:border-red-500/20 dark:bg-red-500/10 dark:text-red-400"> |
| 242 | + {t('error')} |
| 243 | + </div> |
| 244 | + )} |
| 245 | + |
| 246 | + <div className="flex justify-center"> |
| 247 | + <button type="submit" disabled={loading} className={primaryBtnStyles}> |
| 248 | + <Send className="h-4 w-4" /> |
| 249 | + <span>{loading ? t('submitting') : t('submit')}</span> |
| 250 | + </button> |
| 251 | + </div> |
| 252 | + </form> |
| 253 | + )} |
| 254 | + </section> |
| 255 | + ); |
| 256 | +} |
0 commit comments