({ categories }: CategoryManagerProps)
| 38 | } |
| 39 | |
| 40 | export function CategoryManager({ categories }: CategoryManagerProps) { |
| 41 | const [selectedCategory, setSelectedCategory] = useState<Category | null>( |
| 42 | null, |
| 43 | ); |
| 44 | const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); |
| 45 | const [isEditDialogOpen, setIsEditDialogOpen] = useState(false); |
| 46 | |
| 47 | const handleCreate = async (_: ActionState | null, formData: FormData) => { |
| 48 | const payload = { |
| 49 | name: formData.get("name") as string, |
| 50 | description: formData.get("description") as string, |
| 51 | slug: formData.get("slug") as string, |
| 52 | color: formData.get("color") as string, |
| 53 | icon: formData.get("icon") as string, |
| 54 | }; |
| 55 | return createCategory(null, payload); |
| 56 | }; |
| 57 | |
| 58 | const handleUpdate = async (_: ActionState | null, formData: FormData) => { |
| 59 | const payload = { |
| 60 | id: selectedCategory?.id as string, |
| 61 | name: formData.get("name") as string, |
| 62 | description: formData.get("description") as string, |
| 63 | slug: formData.get("slug") as string, |
| 64 | color: formData.get("color") as string, |
| 65 | icon: formData.get("icon") as string, |
| 66 | }; |
| 67 | return updateCategory(null, payload); |
| 68 | }; |
| 69 | |
| 70 | const handleDelete = async (_: ActionState | null, formData: FormData) => { |
| 71 | return deleteCategory(null, { id: formData.get("id") as string }); |
| 72 | }; |
| 73 | |
| 74 | const [createState, createAction] = useFormState(handleCreate, null); |
| 75 | const [updateState, updateAction] = useFormState(handleUpdate, null); |
| 76 | const [deleteState, deleteAction] = useFormState(handleDelete, null); |
| 77 | |
| 78 | // Handle form submission results |
| 79 | if (createState?.success || updateState?.success || deleteState?.success) { |
| 80 | toast.success("Category updated successfully!"); |
| 81 | window.location.reload(); // Refresh to show changes |
| 82 | } |
| 83 | |
| 84 | if (createState?.error || updateState?.error || deleteState?.error) { |
| 85 | toast.error(createState?.error || updateState?.error || deleteState?.error); |
| 86 | } |
| 87 | |
| 88 | return ( |
| 89 | <div className="space-y-4"> |
| 90 | <div className="flex items-center justify-between"> |
| 91 | <h2 className="text-2xl font-bold">Categories</h2> |
| 92 | <Dialog> |
| 93 | <DialogTrigger asChild> |
| 94 | <Button> |
| 95 | <Plus className="mr-2 h-4 w-4" /> |
| 96 | Add Category |
| 97 | </Button> |
nothing calls this directly
no outgoing calls
no test coverage detected