refactored code, fixed checkbox

This commit is contained in:
@jcarymuhammedow committed 2026-05-22 18:51:45 +05:00
1 parent 4adc153503
commit d56620be2e
16 files changed
+640 -267

No files matched your search

@@ -1,11 +1,14 @@
import { useState } from "react";
import { useParams, useNavigate, Link } from "react-router-dom";
import { useParams, useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { Download, Trash2, Pencil, DownloadCloud } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
useCardRequisite,
useDeleteCardRequisite,
useDownloadCardRequisite,
} from "@/features/cardRequisites/hooks/useCardRequisites";
import type { CardRequisiteStatus } from "@/features/cardRequisites/api/cardRequisitesApi";
import { Spinner } from "@/components/ui/spinner";
import { StatusBadge, type StatusBadgeVariant } from "@/components/ui/statusBadge";
import { AlertCircle, CheckCircle2, XCircle } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Pencil, Trash2, Download } from "lucide-react";
import {
AlertDialog,
AlertDialogAction,
@@ -15,268 +18,262 @@ import {
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import {
useCardRequisite,
useDeleteCardRequisite,
useDownloadCardRequisite,
} from "@/features/cardRequisites/hooks/useCardRequisites";
import type { CardRequisiteStatus } from "@/features/cardRequisites/api/cardRequisitesApi";
import { InfoRow } from "@/components/viewPageComponents";
// ─── Status badge ─────────────────────────────────────────────────────────────
StatusBadge,
type StatusBadgeVariant,
} from "@/components/ui/statusBadge";
import { AlertCircle, CheckCircle2, XCircle } from "lucide-react";
import { InfoRow, PassportImage } from "@/components/viewPageComponents";
// ─── Status config ────────────────────────────────────────────────────────────
const STATUS_VARIANT: Record<CardRequisiteStatus, StatusBadgeVariant> = {
pending: "warning",
approved: "success",
rejected: "error",
};
}
const STATUS_ICON: Record<CardRequisiteStatus, React.ElementType> = {
pending: AlertCircle,
approved: CheckCircle2,
rejected: XCircle,
};
}
function CardRequisiteStatusBadge({ status }: { status: CardRequisiteStatus }) {
const { t } = useTranslation();
const variant = STATUS_VARIANT[status];
const Icon = STATUS_ICON[status];
if (!variant)
return <span className="text-xs text-muted-foreground">{status}</span>;
return (
<StatusBadge label={t(`cardRequisite.status.${status}`)} variant={variant} icon={Icon} />
);
const { t } = useTranslation()
const variant = STATUS_VARIANT[status]
const Icon = STATUS_ICON[status]
if (!variant) return <span className="text-xs text-muted-foreground">{status}</span>
return <StatusBadge label={t(`cardRequisite.status.${status}`)} variant={variant} icon={Icon} />
}
// ─── Section header ───────────────────────────────────────────────────────────
// ─── Bento primitives ─────────────────────────────────────────────────────────
function BentoGrid({ cols = 2, children }: { cols?: 1 | 2 | 3 | 4; children: React.ReactNode }) {
const colClass = {
1: "grid-cols-1",
2: "grid-cols-1 sm:grid-cols-2",
3: "grid-cols-1 sm:grid-cols-2 lg:grid-cols-3",
4: "grid-cols-1 sm:grid-cols-2 lg:grid-cols-4",
}[cols]
return <div className={`grid ${colClass} gap-4`}>{children}</div>
}
function BentoCard({
title,
span,
children,
}: {
title?: string
span?: "full" | 2 | 3
children: React.ReactNode
}) {
const spanClass =
span === "full" ? "sm:col-span-full" :
span === 2 ? "sm:col-span-2" :
span === 3 ? "sm:col-span-3" : ""
function SectionTitle({ children }: { children: React.ReactNode }) {
return (
<h2 className="text-base font-semibold text-foreground mt-6 mb-3">
<div className={`bg-card border border-border rounded-xl overflow-hidden ${spanClass}`}>
{title && (
<div className="px-4 py-2.5 border-b border-border">
<p className="text-xs font-semibold text-muted-foreground uppercase tracking-wide">
{title}
</p>
</div>
)}
{children}
</h2>
);
}
// ─── Passport image row ───────────────────────────────────────────────────────
function PassportImageRow({ label, url }: { label: string; url?: string }) {
const { t } = useTranslation();
return (
<div className="grid grid-cols-[220px_1fr] border-b border-border/50 last:border-0">
<div className="px-4 py-3 text-sm text-muted-foreground font-medium bg-muted/10">
{label}
</div>
<div className="px-4 py-4 flex flex-col gap-2">
{url ? (
<>
<img
src={url}
alt={label}
className="max-w-[200px] rounded-md border border-border object-cover"
/>
<a
href={url}
download
className="flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground transition-colors w-fit"
>
<DownloadCloud size={13} />
{t("Download", "Göçürip al")}
</a>
</>
) : (
<span className="text-muted-foreground text-sm">—</span>
)}
</div>
</div>
);
)
}
// ─── Page ─────────────────────────────────────────────────────────────────────
export default function CardRequisiteViewPage() {
const { t } = useTranslation();
const { id } = useParams<{ id: string }>();
const navigate = useNavigate();
const { id } = useParams<{ id: string }>()
const navigate = useNavigate()
const { t } = useTranslation()
const [deleteOpen, setDeleteOpen] = useState(false);
const { data, isLoading } = useCardRequisite(id ?? "");
const deleteMutation = useDeleteCardRequisite();
const downloadMutation = useDownloadCardRequisite();
const handleDelete = async () => {
await deleteMutation.mutateAsync(id ?? "");
navigate("/card-requisites");
};
const { data, isLoading, isError } = useCardRequisite(id ?? "")
const deleteMutation = useDeleteCardRequisite()
const downloadMutation = useDownloadCardRequisite()
if (isLoading) {
return (
<div className="flex items-center justify-center py-24">
<Spinner className="size-7 text-primary" />
<Spinner className="size-8 text-primary" />
</div>
);
)
}
if (!data) {
if (isError || !data) {
return (
<div className="text-center py-24 text-muted-foreground">
{t("Not found", "Tapylmady")}
<div className="flex items-center justify-center py-24 text-muted-foreground">
{t("common.notFound", "Maglumat tapylmady")}
</div>
);
)
}
return (
<div className="space-y-0">
{/* Header */}
<div className="flex items-center justify-between mb-4">
<h1 className="text-xl font-semibold text-foreground">
{t("Card requisite detail", "Kart rekwizit giňişleýin")}: {data.id}
<div className="mx-auto flex flex-col gap-6">
{/* ── Page header ──────────────────────────────────────────────────── */}
<div className="flex items-start justify-between">
<h1 className="text-xl font-bold text-foreground">
{t("cardRequisite.viewTitle", "Kart rekwizit giňişleýin")}: {data.id}
</h1>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
size="icon" variant="ghost"
className="h-8 w-8"
onClick={() => downloadMutation.mutate(data.id)}
disabled={downloadMutation.isPending}
>
<Download size={14} />
<Download size={15} />
</Button>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
size="icon"
variant="ghost"
className="h-8 w-8 text-destructive hover:text-destructive hover:bg-destructive/10"
>
<Trash2 size={15} />
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{t("cardRequisite.deleteTitle", "Pozmak isleýärsiňizmi?")}
</AlertDialogTitle>
<AlertDialogDescription>
{t("cardRequisite.deleteDesc", "Bu amal yzyna dolanyp bolmaz.")} {data.id}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={deleteMutation.isPending}>
{t("common.cancel", "Ýatyr")}
</AlertDialogCancel>
<AlertDialogAction
disabled={deleteMutation.isPending}
className="bg-destructive hover:bg-destructive/90"
onClick={() => deleteMutation.mutate(data.id, { onSuccess: () => navigate("/card-requisites") })}
>
{t("common.delete", "Poz")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<Button
variant="outline"
size="sm"
className="text-destructive border-destructive/30 hover:bg-destructive/10"
onClick={() => setDeleteOpen(true)}
size="icon" variant="ghost" className="h-8 w-8"
onClick={() => navigate(`/card-requisites/${id}/edit`)}
>
<Trash2 size={14} />
</Button>
<Button variant="outline" size="sm" asChild>
<Link to={`/card-requisites/${data.id}/edit`}>
<Pencil size={14} />
</Link>
<Pencil size={15} />
</Button>
</div>
</div>
{/* General info */}
<div className="rounded-lg border border-border bg-card overflow-hidden">
<InfoRow label={t("ID", "ID")}>{data.id}</InfoRow>
<InfoRow label={t("Created at", "Döredilen wagty")}>
{data.created_at}
</InfoRow>
<InfoRow label={t("Status", "Status")}>
<CardRequisiteStatusBadge status={data.status} />
</InfoRow>
<InfoRow label={t("Note", "Bellik")}>{data.note || "—"}</InfoRow>
<InfoRow label={t("Requested by", "Sargyt eden:")}>
{data.first_name && data.last_name ? (
<span className="text-primary font-medium">
{data.last_name}_{data.first_name} ({data.first_name})
</span>
) : (
"—"
)}
</InfoRow>
</div>
{/* ── Row 1: Meta + Kart + Lokasiýa ────────────────────────────────── */}
<BentoGrid cols={3}>
<BentoCard title={t("cardRequisite.metaSection", "Esasy maglumatlar")}>
<InfoRow label={t("common.id", "ID")}>
<span className="font-mono">{data.id}</span>
</InfoRow>
<InfoRow label={t("cardRequisite.createdAt", "Döredilen wagty")}>
{data.created_at}
</InfoRow>
<InfoRow label={t("cardRequisite.status", "Status")}>
<CardRequisiteStatusBadge status={data.status} />
</InfoRow>
<InfoRow label={t("cardRequisite.note", "Bellik")}>
{data.note || "—"}
</InfoRow>
<InfoRow label={t("cardRequisite.createdBy", "Sargyt eden")}>
{data.first_name && data.last_name ? (
<span className="text-primary font-medium">
{data.last_name}_{data.first_name}
</span>
) : "—"}
</InfoRow>
</BentoCard>
{/* Card */}
<SectionTitle>{t("Card", "Kart")}</SectionTitle>
<div className="rounded-lg border border-border bg-card overflow-hidden">
<InfoRow label={t("Card number", "Kart belgisi")}>
{data.card_number}
</InfoRow>
<InfoRow label={t("Card expiry month", "Kart Möhleti (aý)")}>
{data.card_expiry_month}
</InfoRow>
<InfoRow label={t("Card expiry year", "Kart Möhleti (ýyl)")}>
{data.card_expiry_year}
</InfoRow>
</div>
<BentoCard title={t("cardRequisite.cardSection", "Kart")}>
<InfoRow label={t("cardRequisite.cardNumber", "Kart belgisi")}>
<span className="font-mono tracking-wider">{data.card_number}</span>
</InfoRow>
<InfoRow label={t("cardRequisite.cardExpireMonth", "Kart Möhleti (aý)")}>
{data.card_expiry_month}
</InfoRow>
<InfoRow label={t("cardRequisite.cardExpireYear", "Kart Möhleti (ýyl)")}>
{data.card_expiry_year}
</InfoRow>
</BentoCard>
{/* Location */}
<SectionTitle>{t("Location", "Lokasiýa")}</SectionTitle>
<div className="rounded-lg border border-border bg-card overflow-hidden">
<InfoRow label={t("Province", "Welaýat")}>
{data.province_name ?? "—"}
</InfoRow>
<InfoRow label={t("Branch", "Şahamça")}>
<span className="text-primary font-medium">
{data.branch_name ?? "—"}
</span>
</InfoRow>
</div>
<BentoCard title={t("cardRequisite.locationSection", "Lokasiýa")}>
<InfoRow label={t("cardRequisite.province", "Welaýat")}>
{data.province_name ?? "—"}
</InfoRow>
<InfoRow label={t("cardRequisite.branch", "Şahamça")}>
<span className="text-primary font-medium">{data.branch_name ?? "—"}</span>
</InfoRow>
</BentoCard>
</BentoGrid>
{/* Personal */}
<SectionTitle>
{t("Personal information", "Şahsy maglumatlar")}
</SectionTitle>
<div className="rounded-lg border border-border bg-card overflow-hidden">
<InfoRow label={t("Full name", "Doly ady")}>
{[data.first_name, data.last_name, data.middle_name]
.filter(Boolean)
.join(" ")}
</InfoRow>
<InfoRow label={t("Birth date", "Doglan güni")}>
{data.birth_date}
</InfoRow>
<InfoRow label={t("Phone", "Telefon")}>{data.phone}</InfoRow>
</div>
{/* ── Row 2: Şahsy + Pasport maglumatlary ──────────────────────────── */}
<BentoGrid cols={2}>
<BentoCard title={t("cardRequisite.personalSection", "Şahsy maglumatlar")}>
<InfoRow label={t("cardRequisite.fullName", "Doly ady")}>
{[data.first_name, data.last_name, data.middle_name].filter(Boolean).join(" ")}
</InfoRow>
<InfoRow label={t("cardRequisite.birthDate", "Doglan güni")}>
{data.birth_date}
</InfoRow>
<InfoRow label={t("cardRequisite.phone", "Telefon")}>
{data.phone}
</InfoRow>
</BentoCard>
{/* Passport */}
<SectionTitle>{t("Passport", "Pasport")}</SectionTitle>
<div className="rounded-lg border border-border bg-card overflow-hidden">
<InfoRow label={t("Passport series", "Pasport seriýasy")}>
{data.passport_series}
</InfoRow>
<InfoRow label={t("Passport number", "Pasport belgisi")}>
{data.passport_number}
</InfoRow>
<PassportImageRow
label={t("Passport (page 1)", "Pasport (sahypa 1)")}
url={data.passport_page1_url}
/>
<PassportImageRow
label={t("Passport (pages 2-3)", "Pasport (2-3-nji sahypa)")}
url={data.passport_page2_3_url}
/>
<PassportImageRow
label={t("Passport (pages 8-9)", "Pasport (8-9 sahypa)")}
url={data.passport_page8_9_url}
/>
<PassportImageRow
label={t("Passport (page 32)", "Pasport (32-nji sahypa)")}
url={data.passport_page32_url}
/>
</div>
<BentoCard title={t("cardRequisite.passportSection", "Pasport")}>
<InfoRow label={t("cardRequisite.passportSeries", "Pasport seriýasy")}>
{data.passport_series}
</InfoRow>
<InfoRow label={t("cardRequisite.passportNumber", "Pasport belgisi")}>
{data.passport_number}
</InfoRow>
</BentoCard>
</BentoGrid>
{/* ── Row 3: Passport images ────────────────────────────────────────── */}
<BentoGrid cols={4}>
<BentoCard title={t("cardRequisite.passportFile1", "Sahypa 1")}>
<PassportImage
label={t("cardRequisite.passportFile1", "Pasport (sahypa 1)")}
src={data.passport_page1_url}
/>
</BentoCard>
<BentoCard title={t("cardRequisite.passportFile2", "Sahypa 2-3")}>
<PassportImage
label={t("cardRequisite.passportFile2", "Pasport (2-3-nji sahypa)")}
src={data.passport_page2_3_url}
/>
</BentoCard>
<BentoCard title={t("cardRequisite.passportFile3", "Sahypa 8-9")}>
<PassportImage
label={t("cardRequisite.passportFile3", "Pasport (8-9 sahypa)")}
src={data.passport_page8_9_url}
/>
</BentoCard>
<BentoCard title={t("cardRequisite.passportFile4", "Sahypa 32")}>
<PassportImage
label={t("cardRequisite.passportFile4", "Pasport (32-nji sahypa)")}
src={data.passport_page32_url}
/>
</BentoCard>
</BentoGrid>
{/* Delete dialog */}
<AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{t("Are you sure?", "Eminsiňizmi?")}
</AlertDialogTitle>
<AlertDialogDescription>
{t(
"This action cannot be undone.",
"Bu amal yzyna gaýtarylmaz. Kart rekwiziti hemişelik öçüriler.",
)}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t("Cancel", "Ýatyr")}</AlertDialogCancel>
<AlertDialogAction
onClick={handleDelete}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{deleteMutation.isPending
? t("Deleting...", "Öçürilýär...")
: t("Delete", "Öçür")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
)
}
@@ -315,6 +315,7 @@ export default function CardOrdersPage() {
onColumnVisibilityChange={setColumnVisibility}
columnOrder={columnOrder}
onColumnOrderChange={setColumnOrder}
enableRowSelection
getRowId={(row) => row.id}
currentPage={page}
totalPages={data?.totalPages ?? 1}