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 | 'use client'; /** * ArticleManagement - Admin interface for managing support articles */ import React, { useState, useEffect, useCallback } from 'react'; import Link from 'next/link'; import { SupportArticle } from '@/types/support'; import ArticleList from './ArticleList'; import Pagination from '@/components/ui/Pagination'; import { Icon } from '@/components/ui/icons'; export default function ArticleManagement() { const [articles, setArticles] = useState<SupportArticle[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [search, setSearch] = useState(''); const [category, setCategory] = useState(''); const [categories, setCategories] = useState<string[]>([]); const [pagination, setPagination] = useState({ page: 1, limit: 20, total: 0, totalPages: 0}); const fetchArticles = useCallback(async () => { setLoading(true); setError(null); try { const params = new URLSearchParams(); params.set('page', pagination.page.toString()); params.set('limit', pagination.limit.toString()); if (search) params.set('search', search); if (category) params.set('category', category); const response = await fetch(`/api/admin/support/articles?${params}`); if (!response.ok) throw new Error('Failed to fetch articles'); const data = await response.json(); setArticles(data.articles || []); setPagination((prev) => ({ ...prev, total: data.pagination.total, totalPages: data.pagination.totalPages})); // Extract unique categories const uniqueCategories = [...new Set(data.articles.map((a: SupportArticle) => a.category))] as string[]; setCategories(uniqueCategories); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }, [pagination.page, pagination.limit, search, category]); useEffect(() => { fetchArticles(); }, [fetchArticles]); const handleDelete = async (id: string) => { if (!confirm('Are you sure you want to delete this article?')) return; try { const response = await fetch(`/api/admin/support/articles/${id}`, { method: 'DELETE'}); if (!response.ok) throw new Error('Failed to delete article'); fetchArticles(); } catch (err) { setError(err instanceof Error ? err.message : 'Delete failed'); } }; return ( <div className="space-y-6"> {/* Header */} <div className="flex items-center justify-between"> <h1 className="text-2xl font-bold text-gray-900">Support Articles</h1> <Link href="/admin/support/articles/new" className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors" > Create Article </Link> </div> {/* Filters */} <div className="bg-white rounded-lg border border-gray-200 p-4"> <div className="grid grid-cols-1 sm:grid-cols-2 gap-4"> {/* Search */} <div className="relative"> <input type="text" value={search} onChange={(e) => { setSearch(e.target.value); setPagination((prev) => ({ ...prev, page: 1 })); }} placeholder="Search articles..." className="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" /> <Icon name="search" size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" /> </div> {/* Category Filter */} <select value={category} onChange={(e) => { setCategory(e.target.value); setPagination((prev) => ({ ...prev, page: 1 })); }} className="px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" > <option value="">All Categories</option> {categories.map((cat) => ( <option key={cat} value={cat}> {cat} </option> ))} </select> </div> </div> {/* Error */} {error && ( <div className="bg-red-50 text-red-700 px-4 py-3 rounded-lg"> {error} </div> )} {/* Articles List */} <ArticleList articles={articles} loading={loading} onDelete={handleDelete} /> {/* Pagination */} {pagination.totalPages > 1 && ( <Pagination currentPage={pagination.page} totalPages={pagination.totalPages} onPageChange={(page) => setPagination((prev) => ({ ...prev, page }))} /> )} </div> ); } |