first commit
This commit is contained in:
232
src/pages/faq/ui/FaqCategory.tsx
Normal file
232
src/pages/faq/ui/FaqCategory.tsx
Normal file
@@ -0,0 +1,232 @@
|
||||
"use client";
|
||||
|
||||
import { Button } from "@/shared/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/shared/ui/dialog";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormMessage,
|
||||
} from "@/shared/ui/form";
|
||||
import { Input } from "@/shared/ui/input";
|
||||
import { Label } from "@/shared/ui/label";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/shared/ui/table";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Pencil, PlusCircle, Trash2 } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import z from "zod";
|
||||
|
||||
type FaqCategoryType = {
|
||||
id: number;
|
||||
name: string;
|
||||
faqCount: number;
|
||||
};
|
||||
|
||||
// fakeData: kategoriya + savol soni
|
||||
const initialCategories: FaqCategoryType[] = [
|
||||
{ id: 1, name: "umumiy", faqCount: 8 },
|
||||
{ id: 2, name: "to‘lov", faqCount: 5 },
|
||||
{ id: 3, name: "hujjatlar", faqCount: 6 },
|
||||
{ id: 4, name: "sug‘urta", faqCount: 3 },
|
||||
];
|
||||
|
||||
const categoryFormSchema = z.object({
|
||||
name: z.string().min(1, { message: "Kategoriya nomi majburiy" }),
|
||||
});
|
||||
|
||||
const FaqCategory = () => {
|
||||
const [categories, setCategories] =
|
||||
useState<FaqCategoryType[]>(initialCategories);
|
||||
const [openModal, setOpenModal] = useState(false);
|
||||
const [editCategory, setEditCategory] = useState<FaqCategoryType | null>(
|
||||
null,
|
||||
);
|
||||
const [deleteId, setDeleteId] = useState<number | null>(null);
|
||||
|
||||
const form = useForm<z.infer<typeof categoryFormSchema>>({
|
||||
resolver: zodResolver(categoryFormSchema),
|
||||
defaultValues: { name: "" },
|
||||
});
|
||||
|
||||
const onSubmit = (values: z.infer<typeof categoryFormSchema>) => {
|
||||
if (editCategory) {
|
||||
setCategories((prev) =>
|
||||
prev.map((cat) =>
|
||||
cat.id === editCategory.id ? { ...cat, name: values.name } : cat,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
const newCategory = {
|
||||
id: Date.now(),
|
||||
name: values.name,
|
||||
faqCount: 0, // yangi kategoriya bo‘lsa 0 ta savol
|
||||
};
|
||||
setCategories((prev) => [...prev, newCategory]);
|
||||
}
|
||||
|
||||
setOpenModal(false);
|
||||
};
|
||||
|
||||
const handleEdit = (cat: FaqCategoryType) => {
|
||||
setEditCategory(cat);
|
||||
form.setValue("name", cat.name);
|
||||
setOpenModal(true);
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
if (deleteId) {
|
||||
setCategories((prev) => prev.filter((cat) => cat.id !== deleteId));
|
||||
setDeleteId(null);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!openModal) {
|
||||
form.reset();
|
||||
setEditCategory(null);
|
||||
}
|
||||
}, [openModal, form]);
|
||||
|
||||
return (
|
||||
<div className="p-6 space-y-6 w-full">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-semibold">FAQ Kategoriyalar</h1>
|
||||
<Button
|
||||
className="gap-2"
|
||||
onClick={() => {
|
||||
setEditCategory(null);
|
||||
setOpenModal(true);
|
||||
}}
|
||||
>
|
||||
<PlusCircle className="w-4 h-4" /> Yangi kategoriya
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Jadval */}
|
||||
<div className="border rounded-md overflow-hidden shadow-sm">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[50px] text-center">#</TableHead>
|
||||
<TableHead>Kategoriya nomi</TableHead>
|
||||
<TableHead className="text-center">Savollar soni</TableHead>
|
||||
<TableHead className="w-[120px] text-center">Amallar</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{categories.map((cat, index) => (
|
||||
<TableRow key={cat.id}>
|
||||
<TableCell className="text-center font-medium">
|
||||
{index + 1}
|
||||
</TableCell>
|
||||
<TableCell className="capitalize font-medium">
|
||||
{cat.name}
|
||||
</TableCell>
|
||||
<TableCell className="text-center">{cat.faqCount}</TableCell>
|
||||
<TableCell className="flex justify-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => handleEdit(cat)}
|
||||
>
|
||||
<Pencil className="w-4 h-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="icon"
|
||||
onClick={() => setDeleteId(cat.id)}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
|
||||
{/* Modal */}
|
||||
<Dialog open={openModal} onOpenChange={setOpenModal}>
|
||||
<DialogContent className="sm:max-w-[400px] bg-gray-900">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{editCategory ? "Kategoriyani tahrirlash" : "Yangi kategoriya"}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<Label className="text-md">Kategoriya nomi</Label>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="Masalan: umumiy"
|
||||
{...field}
|
||||
className="h-12 bg-gray-800 border-gray-700 text-white"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<DialogFooter className="flex justify-end gap-3">
|
||||
<Button
|
||||
type="button"
|
||||
onClick={() => setOpenModal(false)}
|
||||
className="bg-gray-600 hover:bg-gray-700 text-white"
|
||||
>
|
||||
Bekor qilish
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
className="bg-blue-600 hover:bg-blue-700 text-white"
|
||||
>
|
||||
{editCategory ? "Saqlash" : "Qo‘shish"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* O‘chirish tasdig‘i */}
|
||||
<Dialog open={!!deleteId} onOpenChange={() => setDeleteId(null)}>
|
||||
<DialogContent className="sm:max-w-[400px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Haqiqatan ham o‘chirmoqchimisiz?</DialogTitle>
|
||||
</DialogHeader>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setDeleteId(null)}>
|
||||
Bekor qilish
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={handleDelete}>
|
||||
O‘chirish
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default FaqCategory;
|
||||
Reference in New Issue
Block a user