Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from "react";
import { Dialog } from "@headlessui/react";
import { X, Loader2, Tag, Type, FileText } from "lucide-react";
import { X, Loader2, Tag, Type, FileText, AlertCircle } from "lucide-react";
import api from "@services/common/api";
import toast from "react-hot-toast";

const CategoryFormModal = ({ isOpen, onClose }) => {
const [formData, setFormData] = useState({
Expand All @@ -10,18 +11,21 @@ const CategoryFormModal = ({ isOpen, onClose }) => {
});

const [submitting, setSubmitting] = useState(false);
const [submitError, setSubmitError] = useState("");

const updateField = (field, value) => {
setFormData(prev => ({
...prev,
[field]: value
}));
setSubmitError("");
};

const handleSubmit = async (e) => {
e.preventDefault();
try {
setSubmitting(true);
setSubmitError("");

const payload = {
name: formData.name,
Expand All @@ -31,11 +35,14 @@ const CategoryFormModal = ({ isOpen, onClose }) => {

await api.post("/secure/api/v1/categories/addCategory", payload);

toast.success("Category created successfully");
onClose();
setFormData({ name: "", description: "" });

} catch (err) {
console.error("Category create failed:", err.response?.data || err);
const message = err.response?.data?.message || err.message || "Failed to create category";
setSubmitError(message);
toast.error(message);
} finally {
setSubmitting(false);
}
Expand Down Expand Up @@ -73,6 +80,14 @@ const CategoryFormModal = ({ isOpen, onClose }) => {
{/* ========== FORM BODY ========== */}
<form onSubmit={handleSubmit} className="px-6 py-6 space-y-5">

{/* Error Banner */}
{submitError && (
<div className="flex items-start gap-3 p-4 bg-red-50 border border-red-200 rounded-xl">
<AlertCircle className="w-5 h-5 text-red-500 shrink-0 mt-0.5" />
<p className="text-sm text-red-700">{submitError}</p>
</div>
Comment on lines +85 to +88
)}

{/* Category Name */}
<div className="space-y-1.5">
<label className="flex items-center gap-2 text-sm font-semibold text-gray-700">
Expand Down