refactord some files
This commit is contained in:
1 parent
3d1dc86cab
commit
caae941346
30 files changed
+2761
-3039
No files matched your search
@@ -1,120 +1,111 @@
|
||||
import { useState, useMemo, useCallback } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Eye, Pencil, Trash2 } from 'lucide-react'
|
||||
import type { ColumnDef, VisibilityState } from '@tanstack/react-table'
|
||||
import { useState, useMemo, useCallback } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Eye, Pencil, Trash2 } from "lucide-react";
|
||||
import type { ColumnDef, VisibilityState } from "@tanstack/react-table";
|
||||
|
||||
import { DataTable } from '@/components/dataTable'
|
||||
import { DataTableToolbar, type ActiveFilter, type FilterField } from '@/components/dataTableToolbar'
|
||||
import { useLoanRemaining, useDeleteLoanRemaining } from '@/features/loanRemaining/hooks/useLoanRemaining'
|
||||
import type { LoanRemaining } from '@/features/loanRemaining/api/loanRemainingApi'
|
||||
import { ConfirmDialog } from '@/components/confirmDialog'
|
||||
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 { useLoanRemaining, useDeleteLoanRemaining } from "@/features/loanRemaining/hooks/useLoanRemaining";
|
||||
import type { LoanRemaining } from "@/features/loanRemaining/api/loanRemainingApi";
|
||||
import { DeleteDialog } from "@/components/deleteDialog";
|
||||
|
||||
// ─── Column IDs ────────────────────────────────────────────────────────────────
|
||||
|
||||
const COLUMN_IDS = ['id', 'passportSeries', 'passportNumber', 'loanAccount'] as const
|
||||
const COLUMN_IDS = ["id", "passportSeries", "passportNumber", "loanAccount"] as const;
|
||||
|
||||
// ─── Page Component ────────────────────────────────────────────────────────────
|
||||
|
||||
export default function LoanRemainingPage() {
|
||||
const { t } = useTranslation()
|
||||
const navigate = useNavigate()
|
||||
const deleteMutation = useDeleteLoanRemaining()
|
||||
const [deleteId, setDeleteId] = useState<number | null>(null)
|
||||
const { t } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
const deleteMutation = useDeleteLoanRemaining();
|
||||
const [deleteId, setDeleteId] = useState<number | null>(null);
|
||||
|
||||
// ── State ──────────────────────────────────────────────────────────────────
|
||||
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 [activeFilters, setActiveFilters] = useState<ActiveFilter[]>([])
|
||||
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 [activeFilters, setActiveFilters] = useState<ActiveFilter[]>([]);
|
||||
|
||||
// ── Query ──────────────────────────────────────────────────────────────────
|
||||
const queryParams = useMemo(
|
||||
() => ({ search: search || undefined, page, perPage }),
|
||||
[search, page, perPage],
|
||||
)
|
||||
const queryParams = useMemo(() => ({ search: search || undefined, page, perPage }), [search, page, perPage]);
|
||||
|
||||
const { data, isLoading } = useLoanRemaining(queryParams)
|
||||
const totalPages = data ? Math.ceil(data.total / perPage) : 1
|
||||
const { data, isLoading } = useLoanRemaining(queryParams);
|
||||
const totalPages = data ? Math.ceil(data.total / perPage) : 1;
|
||||
|
||||
// ── Handlers ───────────────────────────────────────────────────────────────
|
||||
const handleFilterChange = useCallback((fieldId: string, value: string) => {
|
||||
setActiveFilters((prev) => prev.map((f) => (f.fieldId === fieldId ? { ...f, value } : f)))
|
||||
setPage(1)
|
||||
}, [])
|
||||
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)
|
||||
}, [])
|
||||
setActiveFilters((prev) => prev.map((f) => ({ ...f, value: "" })));
|
||||
setPage(1);
|
||||
}, []);
|
||||
|
||||
const handleDelete = useCallback((id: number) => {
|
||||
setDeleteId(id)
|
||||
}, [])
|
||||
setDeleteId(id);
|
||||
}, []);
|
||||
|
||||
const confirmDelete = useCallback(() => {
|
||||
if (deleteId === null) return
|
||||
deleteMutation.mutate(deleteId)
|
||||
setDeleteId(null)
|
||||
}, [deleteId, deleteMutation])
|
||||
if (deleteId === null) return;
|
||||
deleteMutation.mutate(deleteId);
|
||||
setDeleteId(null);
|
||||
}, [deleteId, deleteMutation]);
|
||||
|
||||
// ── Columns ────────────────────────────────────────────────────────────────
|
||||
const columns = useMemo<ColumnDef<LoanRemaining>[]>(
|
||||
() => [
|
||||
{
|
||||
id: 'id',
|
||||
accessorKey: 'id',
|
||||
header: t('loanRemaining.columns.id', 'ID'),
|
||||
cell: ({ row }) => (
|
||||
<span className="text-sm font-semibold text-primary">{row.original.id}</span>
|
||||
),
|
||||
id: "id",
|
||||
accessorKey: "id",
|
||||
header: t("loanRemaining.columns.id", "ID"),
|
||||
cell: ({ row }) => <span className="text-sm font-semibold text-primary">{row.original.id}</span>,
|
||||
size: 80,
|
||||
},
|
||||
{
|
||||
id: 'passportSeries',
|
||||
accessorKey: 'passportSeries',
|
||||
header: t('loanRemaining.columns.passportSeries', 'PASPORT SERIÝASY'),
|
||||
cell: ({ row }) => (
|
||||
<span className="text-sm text-foreground">{row.original.passportSeries}</span>
|
||||
),
|
||||
id: "passportSeries",
|
||||
accessorKey: "passportSeries",
|
||||
header: t("loanRemaining.columns.passportSeries", "PASPORT SERIÝASY"),
|
||||
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.passportSeries}</span>,
|
||||
size: 160,
|
||||
},
|
||||
{
|
||||
id: 'passportNumber',
|
||||
accessorKey: 'passportNumber',
|
||||
header: t('loanRemaining.columns.passportNumber', 'PASPORT BELGISI'),
|
||||
cell: ({ row }) => (
|
||||
<span className="text-sm font-mono text-foreground">{row.original.passportNumber}</span>
|
||||
),
|
||||
id: "passportNumber",
|
||||
accessorKey: "passportNumber",
|
||||
header: t("loanRemaining.columns.passportNumber", "PASPORT BELGISI"),
|
||||
cell: ({ row }) => <span className="text-sm font-mono text-foreground">{row.original.passportNumber}</span>,
|
||||
size: 140,
|
||||
},
|
||||
{
|
||||
id: 'loanAccount',
|
||||
accessorKey: 'loanAccount',
|
||||
header: t('loanRemaining.columns.loanAccount', 'KARZ HASABY'),
|
||||
cell: ({ row }) => (
|
||||
<span className="text-sm font-mono text-foreground">{row.original.loanAccount}</span>
|
||||
),
|
||||
id: "loanAccount",
|
||||
accessorKey: "loanAccount",
|
||||
header: t("loanRemaining.columns.loanAccount", "KARZ HASABY"),
|
||||
cell: ({ row }) => <span className="text-sm font-mono text-foreground">{row.original.loanAccount}</span>,
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
header: '',
|
||||
id: "actions",
|
||||
header: "",
|
||||
enableHiding: false,
|
||||
cell: ({ row }) => (
|
||||
<div className="flex items-center gap-1.5 justify-end">
|
||||
<button
|
||||
onClick={() => navigate(`/loan-remaining/${row.original.id}`)}
|
||||
className="p-1.5 cursor-pointer rounded hover:bg-muted/60 text-muted-foreground hover:text-foreground transition-colors"
|
||||
title={t('common.view', 'Görmek')}
|
||||
title={t("common.view", "Görmek")}
|
||||
>
|
||||
<Eye size={15} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => navigate(`/loan-remaining/${row.original.id}/edit`)}
|
||||
className="p-1.5 cursor-pointer rounded hover:bg-muted/60 text-muted-foreground hover:text-foreground transition-colors"
|
||||
title={t('common.edit', 'Üýtgetmek')}
|
||||
title={t("common.edit", "Üýtgetmek")}
|
||||
>
|
||||
<Pencil size={15} />
|
||||
</button>
|
||||
@@ -122,7 +113,7 @@ export default function LoanRemainingPage() {
|
||||
onClick={() => handleDelete(row.original.id)}
|
||||
disabled={deleteMutation.isPending}
|
||||
className="p-1.5 cursor-pointer rounded hover:bg-destructive/10 text-muted-foreground hover:text-destructive transition-colors disabled:opacity-50"
|
||||
title={t('common.delete', 'Pozmak')}
|
||||
title={t("common.delete", "Pozmak")}
|
||||
>
|
||||
<Trash2 size={15} />
|
||||
</button>
|
||||
@@ -132,32 +123,45 @@ export default function LoanRemainingPage() {
|
||||
},
|
||||
],
|
||||
[t, handleDelete, deleteMutation.isPending],
|
||||
)
|
||||
);
|
||||
|
||||
// ── Toggleable columns meta ────────────────────────────────────────────────
|
||||
const toggleableColumns = useMemo(
|
||||
() => COLUMN_IDS.map((id) => ({ id, label: t(`loanRemaining.columns.${id}`, id) })),
|
||||
[t],
|
||||
)
|
||||
const toggleableColumns = useMemo(() => COLUMN_IDS.map((id) => ({ id, label: t(`loanRemaining.columns.${id}`, id) })), [t]);
|
||||
|
||||
const filterFields = useMemo<FilterField[]>(() => [], [])
|
||||
const filterFields = useMemo<FilterField[]>(() => [], []);
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<div className="space-y-4">
|
||||
{/* Page Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-xl font-semibold text-foreground">
|
||||
{t('loanRemaining.title', 'Karzyň galyndysy')}
|
||||
</h1>
|
||||
<h1 className="text-xl font-semibold text-foreground">{t("loanRemaining.title", "Karzyň galyndysy")}</h1>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap items-center gap-3 justify-between">
|
||||
<TableSearchInput
|
||||
value={search}
|
||||
onChange={(v) => {
|
||||
setSearch(v);
|
||||
setPage(1);
|
||||
}}
|
||||
placeholder={t("common.search", "Gözlemek")}
|
||||
/>
|
||||
<CreateButton
|
||||
label={t("loanRemaining.createButton", "Karzyň galyndysy dörediň")}
|
||||
onClick={() => navigate("/loan-remaining/create")}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Table Card */}
|
||||
<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')}
|
||||
onSearchChange={(v) => {
|
||||
setSearch(v);
|
||||
setPage(1);
|
||||
}}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={toggleableColumns}
|
||||
columnVisibility={columnVisibility}
|
||||
onColumnVisibilityChange={setColumnVisibility}
|
||||
@@ -169,11 +173,12 @@ export default function LoanRemainingPage() {
|
||||
onFilterReset={handleFilterReset}
|
||||
perPageOptions={[10, 25, 50, 100]}
|
||||
perPage={perPage}
|
||||
onPerPageChange={(v) => { setPerPage(v); setPage(1) }}
|
||||
actionLabel={t('loanRemaining.createButton', 'Karzyň galyndysy dörediň')}
|
||||
onAction={() => {
|
||||
navigate('/loan-remaining/create')
|
||||
onPerPageChange={(v) => {
|
||||
setPerPage(v);
|
||||
setPage(1);
|
||||
}}
|
||||
hideSearch
|
||||
hideAction
|
||||
/>
|
||||
|
||||
<DataTable
|
||||
@@ -193,14 +198,15 @@ export default function LoanRemainingPage() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<ConfirmDialog
|
||||
<DeleteDialog
|
||||
open={deleteId !== null}
|
||||
onOpenChange={(o) => { if (!o) setDeleteId(null) }}
|
||||
title={t('loanRemaining.deleteConfirm', 'Bu ýazgy pozulsynmy?')}
|
||||
confirmLabel={t('common.delete', 'Poz')}
|
||||
onOpenChange={(o) => {
|
||||
if (!o) setDeleteId(null);
|
||||
}}
|
||||
title={t("loanRemaining.deleteConfirm", "Bu ýazgy pozulsynmy?")}
|
||||
onConfirm={confirmDelete}
|
||||
isLoading={deleteMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user