307 lines
11 KiB
TypeScript
307 lines
11 KiB
TypeScript
import { useState, useMemo, useCallback } from "react";
|
|
import { useNavigate } 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 ActiveFilter, type FilterField } from "@/components/dataTableToolbar";
|
|
// import { TableSearchInput } from "@/components/tableSearch";
|
|
import { CreateButton } from "@/components/createButton";
|
|
import { TableActions } from "@/components/tableActions";
|
|
import { useLoanOrderMobiles, useDeleteLoanOrderMobile } from "@/features/loanOrderMobiles/hooks/useLoanOrderMobiles";
|
|
import type { LoanOrderMobile, LoanOrderMobileStatus } from "@/features/loanOrderMobiles/api/loanOrderMobilesApi";
|
|
import { useInfiniteBranches } from "@/features/branches/hooks/useBranches";
|
|
import { DeleteDialog } from "@/components/deleteDialog";
|
|
import { getStatusOptions, getRegionOptions, StatusCell } from "@/constants";
|
|
import { formatFromApiDate } from "@/lib/utils";
|
|
// ─── Column meta for toggle dropdown ─────────────────────────────────────────
|
|
|
|
const COLUMN_IDS = ["id", "loanType", "createdAt", "region", "branch", "firstName", "lastName", "phone", "status"] as const;
|
|
|
|
// ─── Filter fields ────────────────────────────────────────────────────────────
|
|
|
|
// ─── Page Component ───────────────────────────────────────────────────────────
|
|
|
|
export default function LoanOrderMobilesPage() {
|
|
const { t, i18n } = useTranslation();
|
|
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as "tm" | "ru" | "en";
|
|
const navigate = useNavigate();
|
|
const deleteMutation = useDeleteLoanOrderMobile();
|
|
const [deleteId, setDeleteId] = useState<string | null>(null);
|
|
|
|
const [filterEnabled, setFilterEnabled] = useState(false);
|
|
const [branchSearch, setBranchSearch] = useState("");
|
|
const [activeFilters, setActiveFilters] = useState<ActiveFilter[]>([
|
|
{ fieldId: "region", value: "" },
|
|
{ fieldId: "status", value: "" },
|
|
{ fieldId: "branch", value: "" },
|
|
{ fieldId: "archived", value: "" },
|
|
]);
|
|
|
|
const isBranchFilterEnabled = filterEnabled || Boolean(activeFilters.find((f) => f.fieldId === "branch")?.value);
|
|
|
|
const {
|
|
data: branchesInfiniteData,
|
|
fetchNextPage: fetchNextBranches,
|
|
hasNextPage: hasNextBranches,
|
|
isFetchingNextPage: isFetchingNextBranches,
|
|
isLoading: isBranchesLoading,
|
|
} = useInfiniteBranches(branchSearch, isBranchFilterEnabled);
|
|
|
|
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 [search, setSearch] = useState("");
|
|
const [page, setPage] = useState(1);
|
|
const [perPage, setPerPage] = useState(25);
|
|
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
|
|
const [columnOrder, setColumnOrder] = useState<string[]>([...COLUMN_IDS]);
|
|
|
|
const queryParams = useMemo(() => {
|
|
const filterMap = Object.fromEntries(activeFilters.map((f) => [f.fieldId, f.value]));
|
|
return {
|
|
search: search || undefined,
|
|
region: filterMap.region || undefined,
|
|
status: (filterMap.status as LoanOrderMobileStatus) || undefined,
|
|
branch: filterMap.branch || undefined,
|
|
archived: filterMap.archived || undefined,
|
|
page,
|
|
perPage,
|
|
};
|
|
}, [search, activeFilters, page, perPage]);
|
|
|
|
const { data, isLoading } = useLoanOrderMobiles(queryParams);
|
|
const totalPages = data ? Math.ceil(data.total / perPage) : 1;
|
|
|
|
const regionOptions = useMemo(() => getRegionOptions(t), [t]);
|
|
|
|
const handleFilterChange = useCallback((fieldId: string, value: string) => {
|
|
setActiveFilters((prev) => prev.map((f) => (f.fieldId === fieldId ? { ...f, value } : f)));
|
|
setPage(1);
|
|
}, []);
|
|
|
|
const handleFilterReset = useCallback(() => {
|
|
setActiveFilters((prev) => prev.map((f) => ({ ...f, value: "" })));
|
|
setPage(1);
|
|
}, []);
|
|
|
|
const handleDelete = useCallback((id: string) => {
|
|
setDeleteId(id);
|
|
}, []);
|
|
|
|
const confirmDelete = useCallback(() => {
|
|
if (!deleteId) return;
|
|
deleteMutation.mutate(deleteId);
|
|
setDeleteId(null);
|
|
}, [deleteId, deleteMutation]);
|
|
|
|
const columns = useMemo<ColumnDef<LoanOrderMobile>[]>(
|
|
() => [
|
|
{
|
|
id: "id",
|
|
accessorKey: "id",
|
|
header: t("ID", "ID"),
|
|
cell: ({ row }) => <span className="text-xs font-mono text-muted-foreground">{row.original.id}</span>,
|
|
size: 130,
|
|
},
|
|
{
|
|
id: "loanType",
|
|
accessorKey: "loanType",
|
|
header: t("Loan type", "KARZ GÖRNÜŞI"),
|
|
cell: ({ row }) => <span className="text-sm font-medium text-primary hover:underline cursor-pointer">{row.original.loanType}</span>,
|
|
},
|
|
{
|
|
id: "createdAt",
|
|
accessorKey: "createdAt",
|
|
header: t("Created at", "DÖREDILEN WAGTY"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground whitespace-nowrap">{formatFromApiDate(row.original.createdAt)}</span>,
|
|
size: 160,
|
|
},
|
|
{
|
|
id: "region",
|
|
accessorKey: "region",
|
|
header: t("Region", "WELAÝAT"),
|
|
cell: ({ row }) => (
|
|
<span className="text-sm text-foreground">{t(`loanOrders.regions.${row.original.region}`, row.original.region)}</span>
|
|
),
|
|
size: 100,
|
|
},
|
|
{
|
|
id: "branch",
|
|
accessorKey: "branch",
|
|
header: t("Branch", "ŞAHAMÇA"),
|
|
cell: ({ row }) => <span className="text-sm text-emerald-500 font-medium">{row.original.branch}</span>,
|
|
size: 100,
|
|
},
|
|
{
|
|
id: "firstName",
|
|
accessorKey: "firstName",
|
|
header: t("Name", "ADY"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.firstName}</span>,
|
|
size: 110,
|
|
},
|
|
{
|
|
id: "lastName",
|
|
accessorKey: "lastName",
|
|
header: t("Surname", "FAMILIÝASY"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.lastName}</span>,
|
|
size: 120,
|
|
},
|
|
{
|
|
id: "phone",
|
|
accessorKey: "phone",
|
|
header: t("Phone", "TELEFON"),
|
|
cell: ({ row }) => <span className="text-sm font-mono text-foreground">{row.original.phone}</span>,
|
|
size: 110,
|
|
},
|
|
{
|
|
id: "status",
|
|
accessorKey: "status",
|
|
header: t("Status", "STATUS"),
|
|
cell: ({ row }) => <StatusCell status={row.original.status} prefix="loanOrderStatus" />,
|
|
size: 180,
|
|
},
|
|
{
|
|
id: "actions",
|
|
header: "",
|
|
enableHiding: false,
|
|
cell: ({ row }) => (
|
|
<TableActions
|
|
onView={() => navigate(`/loan-order-mobiles/${row.original.id}`)}
|
|
onEdit={() => navigate(`/loan-order-mobiles/${row.original.id}/edit`)}
|
|
onDelete={() => handleDelete(row.original.id)}
|
|
isDeleting={deleteMutation.isPending}
|
|
/>
|
|
),
|
|
size: 100,
|
|
},
|
|
],
|
|
[t, navigate, handleDelete, deleteMutation.isPending],
|
|
);
|
|
|
|
const toggleableColumns = useMemo(() => COLUMN_IDS.map((id) => ({ id, label: t(`loanOrders.columns.${id}`, id) })), [t]);
|
|
|
|
const filterFields = useMemo<FilterField[]>(
|
|
() => [
|
|
{
|
|
id: "region",
|
|
label: t("Region", "WELAÝAT"),
|
|
options: regionOptions,
|
|
},
|
|
{
|
|
id: "status",
|
|
label: t("Status", "STATUS"),
|
|
options: getStatusOptions(t, "loanOrderStatus"),
|
|
},
|
|
{
|
|
id: "branch",
|
|
label: t("Branch", "ŞAHAMÇA"),
|
|
type: "infinite-select",
|
|
options: branchOptions,
|
|
loading: isBranchesLoading,
|
|
isLoadingMore: isFetchingNextBranches,
|
|
hasNextPage: hasNextBranches,
|
|
onLoadMore: fetchNextBranches,
|
|
searchValue: branchSearch,
|
|
onSearchChange: setBranchSearch,
|
|
},
|
|
{
|
|
id: "archived",
|
|
label: t("loanOrders.filters.archived", "ARHIWLENEN"),
|
|
options: [
|
|
{ value: "true", label: t("common.yes", "Hawa") },
|
|
{ value: "false", label: t("common.no", "Ýok") },
|
|
],
|
|
},
|
|
],
|
|
[t, regionOptions, branchOptions, isBranchesLoading, isFetchingNextBranches, hasNextBranches, branchSearch],
|
|
);
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-xl font-semibold text-foreground">{t("loanOrderMobiles.title", "Karz sargytlary (Mobile)")}</h1>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3 justify-end">
|
|
{/* <TableSearchInput
|
|
value={search}
|
|
onChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
placeholder={t("loanOrderMobiles.searchPlaceholder", "Gözlemek")}
|
|
/> */}
|
|
<CreateButton
|
|
label={t("loanOrderMobiles.createButton", "Karz sargyt dörediñ")}
|
|
onClick={() => navigate("/loan-order-mobiles/create")}
|
|
/>
|
|
</div>
|
|
|
|
<DataTable
|
|
toolbar={
|
|
<DataTableToolbar
|
|
hideSearch
|
|
hideAction
|
|
searchValue={search}
|
|
onSearchChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
searchPlaceholder={t("loanOrderMobiles.searchPlaceholder", "Gözlemek")}
|
|
columns={toggleableColumns}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
filterFields={filterFields}
|
|
activeFilters={activeFilters}
|
|
onFilterChange={handleFilterChange}
|
|
onFilterReset={handleFilterReset}
|
|
perPageOptions={[10, 25, 50, 100]}
|
|
perPage={perPage}
|
|
onPerPageChange={(v) => {
|
|
setPerPage(v);
|
|
setPage(1);
|
|
}}
|
|
onFilterOpenChange={(open) => {
|
|
if (open) setFilterEnabled(true);
|
|
}}
|
|
/>
|
|
}
|
|
columns={columns}
|
|
data={data?.data ?? []}
|
|
isLoading={isLoading}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
enableRowSelection
|
|
getRowId={(row) => row.id}
|
|
currentPage={page}
|
|
totalPages={totalPages}
|
|
totalCount={data?.total}
|
|
onPageChange={setPage}
|
|
/>
|
|
|
|
<DeleteDialog
|
|
open={deleteId !== null}
|
|
onOpenChange={(o) => {
|
|
if (!o) setDeleteId(null);
|
|
}}
|
|
title={t("loanOrderMobiles.deleteConfirm", "Bu sargyt pozulsynmy?")}
|
|
onConfirm={confirmDelete}
|
|
isLoading={deleteMutation.isPending}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|