382 lines
13 KiB
TypeScript
382 lines
13 KiB
TypeScript
import { useState, useCallback, useMemo } from "react";
|
|
import { useNavigate, Link } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import type { ColumnDef, VisibilityState } from "@tanstack/react-table";
|
|
|
|
import { DataTable } from "@/components/dataTable";
|
|
import { DataTableToolbar, type ColumnMeta, type ActiveFilter, type FilterField } from "@/components/dataTableToolbar";
|
|
import { TableSearchInput } from "@/components/tableSearch";
|
|
import { CreateButton } from "@/components/createButton";
|
|
import { TableActions } from "@/components/tableActions";
|
|
import { DeleteDialog } from "@/components/deleteDialog";
|
|
import { useCards, useDeleteCard } from "@/features/orderNewCard/hooks/useOrderNewCard";
|
|
import type { CardOrder } from "@/features/orderNewCard/api/orderNewCardApi";
|
|
import { useInfiniteBranches } from "@/features/branches/hooks/useBranches";
|
|
import { useInfiniteDistricts } from "@/features/districts/hooks/useDistricts";
|
|
import { useInfiniteCardTypes } from "@/features/cardTypes/hooks/useCardTypes";
|
|
import { getStatusOptions, StatusCell, ActiveStatusCell } from "@/constants";
|
|
import { formatFromApiDate } from "@/lib/utils/dateUtils";
|
|
|
|
const PER_PAGE = 25;
|
|
|
|
// ─── Filter field IDs ──────────────────────────────────────────────────────────
|
|
|
|
const FILTER_IDS = ["status", "branch_id", "user_id", "card_state_id", "card_type_id", "region"] as const;
|
|
type FilterId = (typeof FILTER_IDS)[number];
|
|
|
|
const INITIAL_FILTERS: ActiveFilter[] = FILTER_IDS.map((fieldId) => ({ fieldId, value: "" }));
|
|
|
|
export default function CardOrdersPage() {
|
|
const { t, i18n } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const deleteMutation = useDeleteCard();
|
|
|
|
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as "tm" | "ru" | "en";
|
|
|
|
const [page, setPage] = useState(1);
|
|
const [perPage, setPerPage] = useState(PER_PAGE);
|
|
const [search, setSearch] = useState("");
|
|
const [deleteId, setDeleteId] = useState<number | null>(null);
|
|
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
|
|
const [columnOrder, setColumnOrder] = useState<string[]>([]);
|
|
const [activeFilters, setActiveFilters] = useState<ActiveFilter[]>(INITIAL_FILTERS);
|
|
|
|
// ── Dropdown data ──────────────────────────────────────────────────────────
|
|
const [branchSearch, setBranchSearch] = useState("");
|
|
const [districtSearch, setDistrictSearch] = useState("");
|
|
const [cardTypeSearch, setCardTypeSearch] = useState("");
|
|
|
|
const {
|
|
data: branchesInfiniteData,
|
|
fetchNextPage: fetchNextBranches,
|
|
hasNextPage: hasNextBranches,
|
|
isFetchingNextPage: isFetchingNextBranches,
|
|
isLoading: isBranchesLoading,
|
|
} = useInfiniteBranches(branchSearch);
|
|
|
|
const {
|
|
data: districtsInfiniteData,
|
|
fetchNextPage: fetchNextDistricts,
|
|
hasNextPage: hasNextDistricts,
|
|
isFetchingNextPage: isFetchingNextDistricts,
|
|
isLoading: isDistrictsLoading,
|
|
} = useInfiniteDistricts(districtSearch);
|
|
|
|
const {
|
|
data: cardTypesInfiniteData,
|
|
fetchNextPage: fetchNextCardTypes,
|
|
hasNextPage: hasNextCardTypes,
|
|
isFetchingNextPage: isFetchingNextCardTypes,
|
|
isLoading: isCardTypesLoading,
|
|
} = useInfiniteCardTypes(cardTypeSearch);
|
|
|
|
// ── Query ──────────────────────────────────────────────────────────────────
|
|
const getFilter = useCallback((id: FilterId) => activeFilters.find((f) => f.fieldId === id)?.value ?? "", [activeFilters]);
|
|
|
|
const queryParams = useMemo(
|
|
() => ({
|
|
search: search || undefined,
|
|
page,
|
|
limit: perPage,
|
|
status: getFilter("status") || undefined,
|
|
branch_id: getFilter("branch_id") || undefined,
|
|
user_id: getFilter("user_id") || undefined,
|
|
card_state_id: getFilter("card_state_id") || undefined,
|
|
card_type_id: getFilter("card_type_id") || undefined,
|
|
region: getFilter("region") || undefined,
|
|
}),
|
|
[search, page, perPage, getFilter],
|
|
);
|
|
|
|
const { data, isLoading } = useCards(queryParams);
|
|
|
|
const columns: ColumnDef<CardOrder>[] = [
|
|
{
|
|
id: "id",
|
|
accessorKey: "id",
|
|
header: t("cardOrder.col.id", "ID"),
|
|
cell: ({ row }) => (
|
|
<Link to={`/order-new-card/${row.original.id}`} className="font-mono text-xs text-primary hover:underline">
|
|
{row.original.id}
|
|
</Link>
|
|
),
|
|
size: 80,
|
|
},
|
|
{
|
|
id: "uniqueId",
|
|
accessorKey: "uniqueId",
|
|
header: t("cardOrder.col.uniqueId", "Sargyt belgisi"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground font-mono">{row.original.uniqueId}</span>,
|
|
},
|
|
{
|
|
id: "customerName",
|
|
accessorKey: "customerName",
|
|
header: t("cardOrder.col.customer", "Müşderi"),
|
|
cell: ({ row }) => (
|
|
<span className="text-sm text-foreground">
|
|
{row.original.customerName} {row.original.customerSurname}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
id: "phone",
|
|
accessorKey: "phone",
|
|
header: t("cardOrder.col.phone", "Telefon"),
|
|
cell: ({ row }) => <span className="font-mono text-sm">{row.original.phone}</span>,
|
|
},
|
|
{
|
|
id: "region",
|
|
accessorKey: "region",
|
|
header: t("cardOrder.col.region", "Sebit"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.region}</span>,
|
|
},
|
|
{
|
|
id: "status",
|
|
accessorKey: "status",
|
|
header: t("cardOrder.col.status", "Statusy"),
|
|
cell: ({ row }) => <StatusCell status={row.original.status} prefix="cardOrder.status" />,
|
|
},
|
|
{
|
|
id: "paid",
|
|
accessorKey: "paid",
|
|
header: t("cardOrder.col.paid", "Tölegli"),
|
|
cell: ({ row }) => <ActiveStatusCell active={row.original.paid} />,
|
|
size: 90,
|
|
},
|
|
{
|
|
id: "createdAt",
|
|
accessorKey: "createdAt",
|
|
header: t("cardOrder.col.createdAt", "Döredilen wagty"),
|
|
cell: ({ row }) => (
|
|
<span className="text-sm text-muted-foreground whitespace-nowrap">
|
|
{row.original.createdAt ? formatFromApiDate(row.original.createdAt) : "—"}
|
|
</span>
|
|
),
|
|
size: 180,
|
|
},
|
|
{
|
|
id: "actions",
|
|
header: "",
|
|
enableHiding: false,
|
|
cell: ({ row }) => (
|
|
<TableActions
|
|
onView={() => navigate(`/order-new-card/${row.original.id}`)}
|
|
onEdit={() => navigate(`/order-new-card/${row.original.id}/edit`)}
|
|
onDelete={() => setDeleteId(row.original.id)}
|
|
isDeleting={deleteMutation.isPending}
|
|
/>
|
|
),
|
|
size: 100,
|
|
},
|
|
];
|
|
|
|
const columnMeta: ColumnMeta[] = columns
|
|
.filter((c) => c.id !== "actions")
|
|
.map((c) => ({
|
|
id: c.id!,
|
|
label: typeof c.header === "string" ? c.header : c.id!,
|
|
}));
|
|
|
|
const total = data?.count ?? 0;
|
|
const totalPages = Math.ceil(total / perPage);
|
|
|
|
const branchOptions = useMemo(
|
|
() =>
|
|
(branchesInfiniteData?.pages ?? []).flatMap((page) =>
|
|
(page?.branches ?? []).map((b) => ({
|
|
value: String(b.id),
|
|
label: b.name[lang] || b.name.tm,
|
|
})),
|
|
),
|
|
[branchesInfiniteData, lang],
|
|
);
|
|
|
|
const districtOptions = useMemo(
|
|
() =>
|
|
(districtsInfiniteData?.pages ?? []).flatMap((page) =>
|
|
(page?.provinces ?? []).map((d) => ({
|
|
value: d.region?.tm || d.name.tm,
|
|
label: d.name[lang] || d.name.tm,
|
|
})),
|
|
),
|
|
[districtsInfiniteData, lang],
|
|
);
|
|
|
|
const cardTypeOptions = useMemo(
|
|
() =>
|
|
(cardTypesInfiniteData?.pages ?? []).flatMap((page) =>
|
|
(page?.paymentCards ?? []).map((ct) => ({
|
|
value: String(ct.id),
|
|
label: ct.title[lang] || ct.title.tm,
|
|
})),
|
|
),
|
|
[cardTypesInfiniteData, lang],
|
|
);
|
|
|
|
// ── Filter fields ──────────────────────────────────────────────────────────
|
|
const filterFields = useMemo<FilterField[]>(
|
|
() => [
|
|
{
|
|
id: "status",
|
|
label: t("cardOrder.filter.status", "Status"),
|
|
options: getStatusOptions(t, "cardOrder.status"),
|
|
},
|
|
{
|
|
id: "branch_id",
|
|
label: t("cardOrder.filter.branch", "Şahamça"),
|
|
type: "infinite-select",
|
|
options: branchOptions,
|
|
loading: isBranchesLoading,
|
|
isLoadingMore: isFetchingNextBranches,
|
|
hasNextPage: hasNextBranches,
|
|
onLoadMore: fetchNextBranches,
|
|
searchValue: branchSearch,
|
|
onSearchChange: setBranchSearch,
|
|
},
|
|
{
|
|
id: "region",
|
|
label: t("cardOrder.filter.region", "Sebit"),
|
|
type: "infinite-select",
|
|
options: districtOptions,
|
|
loading: isDistrictsLoading,
|
|
isLoadingMore: isFetchingNextDistricts,
|
|
hasNextPage: hasNextDistricts,
|
|
onLoadMore: fetchNextDistricts,
|
|
searchValue: districtSearch,
|
|
onSearchChange: setDistrictSearch,
|
|
},
|
|
{
|
|
id: "user_id",
|
|
label: t("cardOrder.filter.userId", "Ulanyjy ID"),
|
|
type: "text",
|
|
},
|
|
{
|
|
id: "card_state_id",
|
|
label: t("cardOrder.filter.cardStateId", "Kart ýagdaýy ID"),
|
|
type: "text",
|
|
},
|
|
{
|
|
id: "card_type_id",
|
|
label: t("cardOrder.filter.cardTypeId", "Kart görnüşi"),
|
|
type: "infinite-select",
|
|
options: cardTypeOptions,
|
|
loading: isCardTypesLoading,
|
|
isLoadingMore: isFetchingNextCardTypes,
|
|
hasNextPage: hasNextCardTypes,
|
|
onLoadMore: fetchNextCardTypes,
|
|
searchValue: cardTypeSearch,
|
|
onSearchChange: setCardTypeSearch,
|
|
},
|
|
],
|
|
[
|
|
t,
|
|
branchOptions,
|
|
isBranchesLoading,
|
|
isFetchingNextBranches,
|
|
hasNextBranches,
|
|
branchSearch,
|
|
districtOptions,
|
|
isDistrictsLoading,
|
|
isFetchingNextDistricts,
|
|
hasNextDistricts,
|
|
districtSearch,
|
|
cardTypeOptions,
|
|
isCardTypesLoading,
|
|
isFetchingNextCardTypes,
|
|
hasNextCardTypes,
|
|
cardTypeSearch,
|
|
],
|
|
);
|
|
|
|
const handleFilterChange = useCallback((fieldId: string, value: string) => {
|
|
setActiveFilters((prev) => prev.map((f) => (f.fieldId === fieldId ? { ...f, value } : f)));
|
|
setPage(1);
|
|
}, []);
|
|
|
|
const handleFilterReset = useCallback(() => {
|
|
setActiveFilters(INITIAL_FILTERS);
|
|
setPage(1);
|
|
}, []);
|
|
|
|
const handleDelete = useCallback(() => {
|
|
if (deleteId === null) return;
|
|
deleteMutation.mutate(deleteId);
|
|
setDeleteId(null);
|
|
}, [deleteId, deleteMutation]);
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-xl font-semibold text-foreground tracking-tight">{t("cardOrder.title", "Kart sargytlary")}</h1>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3 justify-between">
|
|
<TableSearchInput
|
|
value={search}
|
|
onChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
placeholder={t("common.search", "Gözlemek")}
|
|
/>
|
|
<CreateButton label={t("cardOrder.create", "Kart dörediň")} onClick={() => navigate("/order-new-card/create")} />
|
|
</div>
|
|
|
|
<div className="bg-card border border-border rounded-xl p-4">
|
|
<DataTableToolbar
|
|
searchValue={search}
|
|
onSearchChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
searchPlaceholder={t("common.search", "Gözlemek")}
|
|
columns={columnMeta}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
hideSearch
|
|
hideAction
|
|
perPageOptions={[10, 25, 50, 100]}
|
|
perPage={perPage}
|
|
onPerPageChange={(v) => {
|
|
setPerPage(v);
|
|
setPage(1);
|
|
}}
|
|
filterFields={filterFields}
|
|
activeFilters={activeFilters}
|
|
onFilterChange={handleFilterChange}
|
|
onFilterReset={handleFilterReset}
|
|
/>
|
|
|
|
<DataTable
|
|
columns={columns}
|
|
data={data?.cardOrders ?? []}
|
|
isLoading={isLoading}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
enableRowSelection
|
|
getRowId={(row) => String(row.id)}
|
|
currentPage={page}
|
|
totalPages={totalPages}
|
|
totalCount={total}
|
|
onPageChange={setPage}
|
|
/>
|
|
</div>
|
|
|
|
<DeleteDialog
|
|
open={deleteId !== null}
|
|
onOpenChange={(o) => {
|
|
if (!o) setDeleteId(null);
|
|
}}
|
|
title={t("cardOrder.deleteConfirm", "Bu kart pozulsynmy?")}
|
|
description={t("cardOrder.deleteDescription", "Bu kart hemişelik pozular. Bu işi yzyna gaýtaryp bolmaz.")}
|
|
onConfirm={handleDelete}
|
|
isLoading={deleteMutation.isPending}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|