product page update
This commit is contained in:
@@ -1,17 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { plans_api } from "@/features/plans/lib/api";
|
||||
import type { Product } from "@/features/plans/lib/data";
|
||||
import { API_URLS } from "@/shared/config/api/URLs";
|
||||
import formatPrice from "@/shared/lib/formatPrice";
|
||||
import { Button } from "@/shared/ui/button";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/shared/ui/select";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
@@ -20,12 +11,8 @@ import {
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/shared/ui/table";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { AxiosError } from "axios";
|
||||
import clsx from "clsx";
|
||||
import { Edit, Eye, Loader2, Trash } from "lucide-react";
|
||||
import { Eye, Loader2 } from "lucide-react";
|
||||
import type { Dispatch, SetStateAction } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface Props {
|
||||
products: Product[] | [];
|
||||
@@ -45,59 +32,7 @@ const ProductTable = ({
|
||||
isError,
|
||||
setEditingProduct,
|
||||
setDetailOpen,
|
||||
setDialogOpen,
|
||||
handleDelete,
|
||||
}: Props) => {
|
||||
const { data } = useQuery({
|
||||
queryKey: ["categories"],
|
||||
queryFn: () => {
|
||||
return plans_api.categories({ page: 1, page_size: 1000 });
|
||||
},
|
||||
select(data) {
|
||||
return data.data.results;
|
||||
},
|
||||
});
|
||||
|
||||
const { data: unityData } = useQuery({
|
||||
queryKey: ["unity"],
|
||||
queryFn: () => {
|
||||
return plans_api.unity({ page: 1, page_size: 1000 });
|
||||
},
|
||||
select(data) {
|
||||
return data.data.results;
|
||||
},
|
||||
});
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { mutate: updated } = useMutation({
|
||||
mutationFn: ({ body, id }: { id: string; body: FormData }) =>
|
||||
plans_api.update({ body, id }),
|
||||
onSuccess() {
|
||||
toast.success("Mahsulot statusi o'zgardi", {
|
||||
richColors: true,
|
||||
position: "top-center",
|
||||
});
|
||||
queryClient.refetchQueries({ queryKey: ["product_list"] });
|
||||
setDialogOpen(false);
|
||||
},
|
||||
onError: (err: AxiosError) => {
|
||||
toast.error((err.response?.data as string) || "Xatolik yuz berdi", {
|
||||
richColors: true,
|
||||
position: "top-center",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const handleStatusChange = async (
|
||||
product: string,
|
||||
status: "true" | "false",
|
||||
) => {
|
||||
const formData = new FormData();
|
||||
formData.append("is_active", status);
|
||||
updated({ body: formData, id: product });
|
||||
};
|
||||
|
||||
if (isLoading || isFetching) {
|
||||
return (
|
||||
<div className="flex h-full items-center justify-center">
|
||||
@@ -121,43 +56,39 @@ const ProductTable = ({
|
||||
<TableRow>
|
||||
<TableHead>ID</TableHead>
|
||||
<TableHead>Rasmi</TableHead>
|
||||
<TableHead>Nomi (UZ)</TableHead>
|
||||
<TableHead>Nomi (RU)</TableHead>
|
||||
<TableHead>Tavsif (UZ)</TableHead>
|
||||
<TableHead>Tavsif (RU)</TableHead>
|
||||
<TableHead>Kategoriya</TableHead>
|
||||
<TableHead>Birligi</TableHead>
|
||||
<TableHead>Brendi</TableHead>
|
||||
<TableHead>Narx</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead className="text-right">Harakatlar</TableHead>
|
||||
<TableHead>Nomi</TableHead>
|
||||
<TableHead>Tavsif</TableHead>
|
||||
<TableHead className="text-end">Harakatlar</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
<TableBody>
|
||||
{products.map((product, index) => {
|
||||
const cat = data?.find((c) => c.id === product.category);
|
||||
const unity = unityData?.find((u) => u.id === product.unity);
|
||||
|
||||
return (
|
||||
<TableRow key={product.id}>
|
||||
<TableCell>{index + 1}</TableCell>
|
||||
{product.images.length > 0 ? (
|
||||
<TableCell>
|
||||
<img
|
||||
src={API_URLS.BASE_URL + product.images[0].image}
|
||||
alt={product.name}
|
||||
className="w-16 h-16 object-cover rounded"
|
||||
/>
|
||||
</TableCell>
|
||||
) : (
|
||||
<TableCell>
|
||||
<img
|
||||
src={"/logo.png"}
|
||||
alt={product.name}
|
||||
className="w-10 h-10 object-cover rounded"
|
||||
/>
|
||||
</TableCell>
|
||||
)}
|
||||
<TableCell>{product.name}</TableCell>
|
||||
<TableCell>
|
||||
<img
|
||||
src={API_URLS.BASE_URL + product.image}
|
||||
alt={product.name_uz}
|
||||
className="w-16 h-16 object-cover rounded"
|
||||
/>
|
||||
{product.short_name && product.short_name.slice(0, 15)}...
|
||||
</TableCell>
|
||||
<TableCell>{product.name_uz}</TableCell>
|
||||
<TableCell>{product.name_ru}</TableCell>
|
||||
<TableCell>{product.description_uz.slice(0, 15)}...</TableCell>
|
||||
<TableCell>{product.description_ru.slice(0, 15)}...</TableCell>
|
||||
<TableCell>{cat?.name_uz}</TableCell>
|
||||
<TableCell>{unity?.name_uz}</TableCell>
|
||||
<TableCell>{product.brand}</TableCell>
|
||||
<TableCell>{formatPrice(product.price, true)}</TableCell>
|
||||
<TableCell
|
||||
{/* <TableCell
|
||||
className={clsx(
|
||||
product.is_active ? "text-green-600" : "text-red-600",
|
||||
)}
|
||||
@@ -176,7 +107,7 @@ const ProductTable = ({
|
||||
<SelectItem value="false">Nofaol</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</TableCell>
|
||||
</TableCell> */}
|
||||
|
||||
<TableCell className="space-x-2 text-right">
|
||||
<Button
|
||||
@@ -190,7 +121,7 @@ const ProductTable = ({
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
{/*<Button
|
||||
size="sm"
|
||||
className="bg-blue-500 text-white hover:bg-blue-600"
|
||||
onClick={() => {
|
||||
@@ -207,7 +138,7 @@ const ProductTable = ({
|
||||
onClick={() => handleDelete(product)}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
</Button>*/}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user