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 | "use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { useConfirm } from "@/hooks/useConfirm"; import { Button } from "@/components/ui"; interface Category { id: number; title: string; slug: string; parentId: number | null; _count: { products: number }; children: Category[]; } export const CategoryList: React.FC = () => { const { confirm, ConfirmDialog } = useConfirm(); const [categories, setCategories] = useState<Category[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); useEffect(() => { fetchCategories(); }, []); const fetchCategories = async () => { setLoading(true); try { const response = await fetch("/api/admin/categories"); const result = await response.json(); if (result.success) { setCategories(result.data); } } catch (err) { const error = err instanceof Error ? err : new Error("Failed to fetch categories"); setError(error.message); } finally { setLoading(false); } }; const handleDelete = async (categoryId: number, categoryTitle: string) => { const confirmed = await confirm({ title: "Delete Category", message: `Are you sure you want to delete "${categoryTitle}"? Products will not be deleted.`, confirmText: "Delete", cancelText: "Cancel", variant: "warning"}); if (!confirmed) { return; } try { const response = await fetch(`/api/admin/categories/${categoryId}`, { method: "DELETE"}); if (response.ok) { setCategories((prev) => prev.filter((c) => c.id !== categoryId)); } else { alert("Failed to delete category"); } } catch (err) { alert("Failed to delete category"); return err; } }; return ( <div className="p-8 md:p-4 max-w-[1000px] mx-auto"> <div className="flex justify-between items-center mb-8 gap-5 md:flex-col md:items-start"> <h1 className="m-0 text-3xl text-gray-800">Categories</h1> <Link href="/admin/categories/new" className="inline-block py-3 px-6 bg-gradient-to-br from-green-500 to-teal-500 text-white no-underline rounded-md font-semibold transition-all duration-300 hover:bg-gradient-to-br hover:from-teal-500 hover:to-cyan-500 hover:-translate-y-0.5 hover:shadow-lg" > + Add Category </Link> </div> {error && ( <div className="bg-red-50 text-red-800 border border-red-200 rounded py-3 px-4 mb-5"> {error} </div> )} {loading ? ( <div className="text-center py-16 px-5 text-gray-500">Loading categories...</div> ) : categories.filter((c) => !c.parentId).length === 0 ? ( <div className="text-center py-16 px-5 text-gray-500">No categories found</div> ) : ( <div className="flex flex-col gap-5"> {categories .filter((cat) => !cat.parentId) .map((category) => ( <div key={category.id} className="bg-white rounded-lg shadow-sm overflow-hidden"> <div className="flex justify-between items-center p-5 bg-gray-50 border-b border-gray-200 md:flex-col md:items-start md:gap-3"> <div className="flex-1"> <h3 className="m-0 mb-1 text-lg text-gray-800">{category.title}</h3> <span className="inline-block bg-blue-500 text-white py-0.5 px-2 rounded text-xs font-semibold"> {category._count.products} products </span> </div> <div className="flex gap-2.5 ml-5 md:ml-0 md:w-full"> <Link href={`/admin/categories/${category.id}`} className="py-2 px-4 border-none rounded text-[13px] font-semibold cursor-pointer transition-all duration-300 no-underline inline-block bg-blue-500 text-white hover:bg-blue-700 md:flex-1 md:text-center" > Edit </Link> <Button onClick={() => handleDelete(category.id, category.title)} variant="danger" size="sm" className="md:flex-1" > Delete </Button> </div> </div> {category.children && category.children.length > 0 && ( <div className="flex flex-col gap-px bg-gray-200 p-px"> {category.children.map((child) => ( <div key={child.id} className="flex justify-between items-center py-4 px-5 pl-16 bg-white md:flex-col md:items-start md:gap-2.5" > <div className="flex-1"> <p className="m-0 mb-1 font-medium text-gray-600">{child.title}</p> <span className="inline-block bg-blue-500 text-white py-0.5 px-2 rounded text-[11px] font-semibold"> {child._count?.products || 0} products </span> </div> <div className="flex gap-2.5 md:w-full"> <Link href={`/admin/categories/${child.id}`} className="py-2 px-4 border-none rounded text-[13px] font-semibold cursor-pointer transition-all duration-300 no-underline inline-block bg-blue-500 text-white hover:bg-blue-700 md:flex-1 md:text-center" > Edit </Link> <Button onClick={() => handleDelete(child.id, child.title)} variant="danger" size="sm" className="md:flex-1" > Delete </Button> </div> </div> ))} </div> )} </div> ))} </div> )} <ConfirmDialog /> </div> ); }; |