update
This commit is contained in:
18
src/features/price-type/lib/api.ts
Normal file
18
src/features/price-type/lib/api.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import httpClient from "@/shared/config/api/httpClient";
|
||||
import { API_URLS } from "@/shared/config/api/URLs";
|
||||
import type { AxiosResponse } from "axios";
|
||||
import type { PriceTypeResponse } from "./type";
|
||||
|
||||
const price_type_api = {
|
||||
async list(): Promise<AxiosResponse<PriceTypeResponse[]>> {
|
||||
const res = await httpClient.get(API_URLS.PriceTypeList);
|
||||
return res;
|
||||
},
|
||||
|
||||
async import() {
|
||||
const res = await httpClient.post(API_URLS.PriceTypeImport);
|
||||
return res;
|
||||
},
|
||||
};
|
||||
|
||||
export default price_type_api;
|
||||
10
src/features/price-type/lib/type.ts
Normal file
10
src/features/price-type/lib/type.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface PriceTypeResponse {
|
||||
id: number;
|
||||
created_at: string;
|
||||
name: string;
|
||||
code: string;
|
||||
with_card: string;
|
||||
state: string;
|
||||
price_type_kind: string;
|
||||
currency_code: string;
|
||||
}
|
||||
72
src/features/price-type/ui/TablePriceType.tsx
Normal file
72
src/features/price-type/ui/TablePriceType.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/shared/ui/table";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import type { PriceTypeResponse } from "../lib/type";
|
||||
|
||||
interface Props {
|
||||
data: PriceTypeResponse[] | [];
|
||||
isLoading: boolean;
|
||||
isError: boolean;
|
||||
}
|
||||
|
||||
const TablePriceType = ({ data, isError, isLoading }: Props) => {
|
||||
return (
|
||||
<div className="flex-1 overflow-auto">
|
||||
{isLoading && (
|
||||
<div className="h-full flex items-center justify-center bg-white/70 z-10">
|
||||
<span className="text-lg font-medium">
|
||||
<Loader2 className="animate-spin" />
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isError && (
|
||||
<div className="h-full flex items-center justify-center z-10">
|
||||
<span className="text-lg font-medium text-red-600">
|
||||
Ma'lumotlarni olishda xatolik yuz berdi.
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isError && !isLoading && (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>ID</TableHead>
|
||||
<TableHead>Nomi</TableHead>
|
||||
<TableHead>Kodi</TableHead>
|
||||
<TableHead>Valyuta kodi</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
<TableBody>
|
||||
{data.map((d, index) => (
|
||||
<TableRow key={d.id}>
|
||||
<TableCell>{index + 1}</TableCell>
|
||||
<TableCell>{d.name}</TableCell>
|
||||
<TableCell>{d.code}</TableCell>
|
||||
<TableCell>{d.currency_code}</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
|
||||
{data.length === 0 && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4} className="text-center py-6">
|
||||
Hech qanday tuman topilmadi
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default TablePriceType;
|
||||
61
src/features/price-type/ui/priceTypeList.tsx
Normal file
61
src/features/price-type/ui/priceTypeList.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import price_type_api from "../lib/api";
|
||||
import TablePriceType from "./TablePriceType";
|
||||
import { Button } from "@/shared/ui/button";
|
||||
import { toast } from "sonner";
|
||||
import type { AxiosError } from "axios";
|
||||
|
||||
const PriceTypeList = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { mutate } = useMutation({
|
||||
mutationFn: () => price_type_api.import(),
|
||||
onSuccess: () => {
|
||||
queryClient.refetchQueries({ queryKey: ["price_type"] });
|
||||
toast.success("Narx turlari import qilindi", {
|
||||
richColors: true,
|
||||
position: "top-center",
|
||||
});
|
||||
},
|
||||
onError: (err: AxiosError) => {
|
||||
const errData = (err.response?.data as { data: string }).data;
|
||||
const errMessage = (err.response?.data as { message: string }).message;
|
||||
toast.error(errData || errMessage || "Xatolik yuz berdi", {
|
||||
richColors: true,
|
||||
position: "top-center",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const { data, isLoading, isError } = useQuery({
|
||||
queryKey: ["price_type"],
|
||||
queryFn: () => price_type_api.list(),
|
||||
select(data) {
|
||||
return data.data;
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full p-10 w-full">
|
||||
<div className="flex flex-col md:flex-row justify-between items-start md:items-center mb-4 gap-4">
|
||||
<h1 className="text-2xl font-bold">Narx turlari</h1>
|
||||
|
||||
<div className="flex gap-4 justify-end">
|
||||
<Button
|
||||
className="bg-blue-500 h-12 cursor-pointer hover:bg-blue-500"
|
||||
onClick={() => mutate()}
|
||||
>
|
||||
Narx turlarini import qilish
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<TablePriceType
|
||||
data={data ? data : []}
|
||||
isError={isError}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default PriceTypeList;
|
||||
Reference in New Issue
Block a user