Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | "use client"; import { useState, useEffect, useCallback } from "react"; import { Button, Input } from "@/components/ui"; import { Icon } from "@/components/ui/icons"; import toast from "react-hot-toast"; import Image from "next/image"; interface Testimonial { id: number; review: string; authorName: string; authorRole: string; authorImage: string; isActive: boolean; createdAt: string; } interface TestimonialFormData { review: string; authorName: string; authorRole: string; authorImage: string; isActive: boolean; } const emptyForm: TestimonialFormData = { review: "", authorName: "", authorRole: "", authorImage: "/images/users/user-01.jpg", isActive: true}; export default function AdminTestimonialsPage() { const [testimonials, setTestimonials] = useState<Testimonial[]>([]); const [loading, setLoading] = useState(true); const [showForm, setShowForm] = useState(false); const [editingId, setEditingId] = useState<number | null>(null); const [formData, setFormData] = useState<TestimonialFormData>(emptyForm); const [submitting, setSubmitting] = useState(false); const fetchTestimonials = useCallback(async () => { try { setLoading(true); const response = await fetch("/api/admin/testimonials"); const result = await response.json(); if (result.success && Array.isArray(result.data?.testimonials)) { setTestimonials(result.data.testimonials); } else if (result.success && Array.isArray(result.data)) { // Fallback for direct array response setTestimonials(result.data); } else { toast.error("Failed to load testimonials"); } } catch { toast.error("Failed to load testimonials"); } finally { setLoading(false); } }, []); useEffect(() => { fetchTestimonials(); }, [fetchTestimonials]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSubmitting(true); try { const url = editingId ? `/api/admin/testimonials/${editingId}` : "/api/admin/testimonials"; const method = editingId ? "PUT" : "POST"; const response = await fetch(url, { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(formData)}); const result = await response.json(); if (result.success) { toast.success(editingId ? "Testimonial updated" : "Testimonial created"); setShowForm(false); setEditingId(null); setFormData(emptyForm); fetchTestimonials(); } else { toast.error(result.error || "Failed to save testimonial"); } } catch { toast.error("Failed to save testimonial"); } finally { setSubmitting(false); } }; const handleEdit = (testimonial: Testimonial) => { setEditingId(testimonial.id); setFormData({ review: testimonial.review, authorName: testimonial.authorName, authorRole: testimonial.authorRole, authorImage: testimonial.authorImage, isActive: testimonial.isActive}); setShowForm(true); }; const handleDelete = async (id: number) => { if (!confirm("Are you sure you want to delete this testimonial?")) return; try { const response = await fetch(`/api/admin/testimonials/${id}`, { method: "DELETE"}); const result = await response.json(); if (result.success) { toast.success("Testimonial deleted"); fetchTestimonials(); } else { toast.error(result.error || "Failed to delete testimonial"); } } catch { toast.error("Failed to delete testimonial"); } }; const handleToggleActive = async (id: number) => { try { const response = await fetch(`/api/admin/testimonials/${id}`, { method: "PATCH"}); const result = await response.json(); if (result.success) { toast.success(`Testimonial ${result.data.isActive ? "activated" : "deactivated"}`); fetchTestimonials(); } else { toast.error(result.error || "Failed to toggle status"); } } catch { toast.error("Failed to toggle status"); } }; const handleCancel = () => { setShowForm(false); setEditingId(null); setFormData(emptyForm); }; return ( <div className="p-6"> <div className="flex justify-between items-center mb-6"> <h1 className="text-2xl font-bold text-dark dark:text-gray-100">Testimonials Management</h1> <Button variant="primary" onClick={() => { setShowForm(true); setEditingId(null); setFormData(emptyForm); }} > <Icon name="plus" size={16} className="mr-2" /> Add Testimonial </Button> </div> {/* Form Modal */} {showForm && ( <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"> <div className="bg-white dark:bg-gray-800 rounded-lg p-6 w-full max-w-lg mx-4"> <h2 className="text-xl font-bold mb-4 dark:text-gray-100"> {editingId ? "Edit Testimonial" : "Add Testimonial"} </h2> <form onSubmit={handleSubmit} className="space-y-4"> <div> <label className="block text-sm font-medium mb-1 dark:text-gray-300">Review</label> <textarea value={formData.review} onChange={(e) => setFormData({ ...formData, review: e.target.value })} className="w-full border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 dark:text-gray-100 rounded-md p-2 h-24" required minLength={10} placeholder="Customer review text..." /> </div> <div> <label className="block text-sm font-medium mb-1 dark:text-gray-300">Author Name</label> <Input value={formData.authorName} onChange={(e) => setFormData({ ...formData, authorName: e.target.value })} required minLength={2} placeholder="John Doe" /> </div> <div> <label className="block text-sm font-medium mb-1 dark:text-gray-300">Author Role</label> <Input value={formData.authorRole} onChange={(e) => setFormData({ ...formData, authorRole: e.target.value })} required minLength={2} placeholder="CEO, Customer, etc." /> </div> <div> <label className="block text-sm font-medium mb-1 dark:text-gray-300">Author Image URL</label> <Input value={formData.authorImage} onChange={(e) => setFormData({ ...formData, authorImage: e.target.value })} required placeholder="/images/users/user-01.jpg" /> </div> <div className="flex items-center gap-2"> <input type="checkbox" id="isActive" checked={formData.isActive} onChange={(e) => setFormData({ ...formData, isActive: e.target.checked })} className="w-4 h-4" /> <label htmlFor="isActive" className="text-sm dark:text-gray-300">Active (visible on site)</label> </div> <div className="flex gap-3 pt-4"> <Button type="submit" variant="primary" disabled={submitting}> {submitting ? "Saving..." : editingId ? "Update" : "Create"} </Button> <Button type="button" variant="ghost" onClick={handleCancel}> Cancel </Button> </div> </form> </div> </div> )} {/* Testimonials List */} {loading ? ( <div className="text-center py-8 dark:text-gray-300">Loading testimonials...</div> ) : testimonials.length === 0 ? ( <div className="text-center py-8 text-gray-500 dark:text-gray-400"> No testimonials yet. Add your first testimonial! </div> ) : ( <div className="grid gap-4"> {testimonials.map((testimonial) => ( <div key={testimonial.id} className={`bg-white dark:bg-gray-800 rounded-lg shadow p-4 border-l-4 ${ testimonial.isActive ? "border-green-500" : "border-gray-300 dark:border-gray-600" }`} > <div className="flex justify-between items-start"> <div className="flex gap-4 flex-1"> <div className="w-12 h-12 rounded-full overflow-hidden flex-shrink-0"> <Image src={testimonial.authorImage} alt={testimonial.authorName} width={48} height={48} className="object-cover" /> </div> <div className="flex-1"> <div className="flex items-center gap-2 mb-1"> <h3 className="font-medium dark:text-gray-100">{testimonial.authorName}</h3> <span className="text-sm text-gray-500 dark:text-gray-400">•</span> <span className="text-sm text-gray-500 dark:text-gray-400">{testimonial.authorRole}</span> {!testimonial.isActive && ( <span className="text-xs bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-400 px-2 py-0.5 rounded"> Inactive </span> )} </div> <p className="text-gray-600 dark:text-gray-300 text-sm">{testimonial.review}</p> <p className="text-xs text-gray-400 mt-2"> Created: {new Date(testimonial.createdAt).toLocaleDateString()} </p> </div> </div> <div className="flex gap-2 ml-4"> <button onClick={() => handleToggleActive(testimonial.id)} className={`p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-700 ${ testimonial.isActive ? "text-green-600" : "text-gray-400" }`} title={testimonial.isActive ? "Deactivate" : "Activate"} > <Icon name={testimonial.isActive ? "check-circle" : "x-circle"} size={18} /> </button> <button onClick={() => handleEdit(testimonial)} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-700 text-blue" title="Edit" > <Icon name="edit" size={18} /> </button> <button onClick={() => handleDelete(testimonial.id)} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-700 text-red-500" title="Delete" > <Icon name="trash" size={18} /> </button> </div> </div> </div> ))} </div> )} </div> ); } |