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 | 'use client'; /** * ArticleEditor - Form for creating/editing support articles */ import React, { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { SupportArticle } from '@/types/support'; import { Icon } from '@/components/ui/icons'; export interface ArticleEditorProps { /** Article ID for editing (undefined for create) */ articleId?: string; } interface ArticleFormData { title: string; slug: string; category: string; summary: string; content: string; keywords: string; isPublished: boolean; } const CATEGORIES = [ 'Getting Started', 'Orders & Shipping', 'Returns & Refunds', 'Account & Billing', 'Products', 'Technical Support', 'FAQ', ]; export default function ArticleEditor({ articleId }: ArticleEditorProps) { const router = useRouter(); const isEditing = Boolean(articleId); const [loading, setLoading] = useState(isEditing); const [saving, setSaving] = useState(false); const [error, setError] = useState<string | null>(null); const [formData, setFormData] = useState<ArticleFormData>({ title: '', slug: '', category: 'FAQ', summary: '', content: '', keywords: '', isPublished: false}); useEffect(() => { if (articleId) { fetchArticle(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [articleId]); const fetchArticle = async () => { try { const response = await fetch(`/api/admin/support/articles/${articleId}`); if (!response.ok) throw new Error('Failed to fetch article'); const data = await response.json(); const article: SupportArticle = data.data; setFormData({ title: article.title, slug: article.slug, category: article.category, summary: article.summary, content: article.content, keywords: article.keywords, isPublished: article.isPublished}); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; const generateSlug = (title: string) => { return title .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-|-$/g, ''); }; const handleTitleChange = (title: string) => { setFormData((prev) => ({ ...prev, title, slug: prev.slug || generateSlug(title)})); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); setError(null); try { const url = isEditing ? `/api/admin/support/articles/${articleId}` : '/api/admin/support/articles'; const method = isEditing ? 'PATCH' : 'POST'; const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData)}); if (!response.ok) { const data = await response.json(); throw new Error(data.error || 'Failed to save article'); } router.push('/admin/support/articles'); } catch (err) { setError(err instanceof Error ? err.message : 'Save failed'); } finally { setSaving(false); } }; if (loading) { return ( <div className="p-8 text-center"> <div className="animate-spin h-8 w-8 border-4 border-blue-600 border-t-transparent rounded-full mx-auto" /> </div> ); } return ( <div className="max-w-4xl mx-auto space-y-6"> {/* Header */} <div className="flex items-center gap-4"> <Link href="/admin/support/articles" className="text-gray-500 hover:text-gray-700" > <Icon name="chevron-left" size={20} /> </Link> <h1 className="text-2xl font-bold text-gray-900"> {isEditing ? 'Edit Article' : 'Create Article'} </h1> </div> {/* Error */} {error && ( <div className="bg-red-50 text-red-700 px-4 py-3 rounded-lg"> {error} </div> )} {/* Form */} <form onSubmit={handleSubmit} className="space-y-6"> <div className="bg-white rounded-lg border border-gray-200 p-6 space-y-6"> {/* Title */} <div> <label className="block text-sm font-medium text-gray-700 mb-1"> Title <span className="text-red-500">*</span> </label> <input type="text" value={formData.title} onChange={(e) => handleTitleChange(e.target.value)} required className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="How to track your order" /> </div> {/* Slug */} <div> <label className="block text-sm font-medium text-gray-700 mb-1"> URL Slug <span className="text-red-500">*</span> </label> <div className="flex items-center"> <span className="text-gray-500 text-sm mr-2">/support/articles/</span> <input type="text" value={formData.slug} onChange={(e) => setFormData((prev) => ({ ...prev, slug: e.target.value }))} required className="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="how-to-track-your-order" /> </div> </div> {/* Category */} <div> <label className="block text-sm font-medium text-gray-700 mb-1"> Category <span className="text-red-500">*</span> </label> <select value={formData.category} onChange={(e) => setFormData((prev) => ({ ...prev, category: e.target.value }))} required className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" > {CATEGORIES.map((cat) => ( <option key={cat} value={cat}> {cat} </option> ))} </select> </div> {/* Summary */} <div> <label className="block text-sm font-medium text-gray-700 mb-1"> Summary <span className="text-red-500">*</span> </label> <textarea value={formData.summary} onChange={(e) => setFormData((prev) => ({ ...prev, summary: e.target.value }))} required rows={2} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none" placeholder="A brief summary of this article..." /> </div> {/* Content */} <div> <label className="block text-sm font-medium text-gray-700 mb-1"> Content <span className="text-red-500">*</span> </label> <textarea value={formData.content} onChange={(e) => setFormData((prev) => ({ ...prev, content: e.target.value }))} required rows={15} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 resize-none font-mono text-sm" placeholder="Article content (HTML supported)..." /> <p className="text-xs text-gray-500 mt-1"> HTML formatting is supported. Use <h2>, <p>, <ul>, <ol>, etc. </p> </div> {/* Keywords */} <div> <label className="block text-sm font-medium text-gray-700 mb-1"> Keywords </label> <input type="text" value={formData.keywords} onChange={(e) => setFormData((prev) => ({ ...prev, keywords: e.target.value }))} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="order, tracking, shipping, delivery" /> <p className="text-xs text-gray-500 mt-1"> Comma-separated keywords for search optimization </p> </div> {/* Toggles */} <div className="flex flex-wrap gap-6"> <label className="flex items-center gap-2"> <input type="checkbox" checked={formData.isPublished} onChange={(e) => setFormData((prev) => ({ ...prev, isPublished: e.target.checked }))} className="h-4 w-4 text-blue-600 rounded border-gray-300" /> <span className="text-sm text-gray-700">Published</span> </label> </div> </div> {/* Actions */} <div className="flex items-center justify-end gap-4"> <Link href="/admin/support/articles" className="px-4 py-2 text-gray-700 hover:text-gray-900" > Cancel </Link> <button type="submit" disabled={saving} className="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50" > {saving ? 'Saving...' : isEditing ? 'Update Article' : 'Create Article'} </button> </div> </form> </div> ); } |