176 lines
8.5 KiB
TypeScript
176 lines
8.5 KiB
TypeScript
import { useState } from "react";
|
|
import { useNavigate, useParams } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Trash2, Pencil, FileText, ExternalLink } from "lucide-react";
|
|
import { useLoanPaidOffLetterById, useDeleteLoanPaidOffLetter } from "@/features/loanPaidOffLetters/hooks/useLoanPaidOffLetters";
|
|
import { StatusBadge } from "@/components/ui/statusBadge";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import { BentoGrid, BentoCard, InfoRow } from "@/components/viewPageComponents";
|
|
import { ConfirmDialog } from "@/components/confirmDialog";
|
|
import { STATUS_BADGE_VARIANTS } from "@/constants";
|
|
import { buildFileUrl } from "@/features/shared/api/sharedApi";
|
|
|
|
import type { LocalizedText } from "@/features/branches/api/branchesApi";
|
|
|
|
function formatDate(value: string | null | undefined): string {
|
|
if (!value) return "—";
|
|
return value.slice(0, 10);
|
|
}
|
|
|
|
function resolveLocalizedName(name: LocalizedText | string | undefined | null, lang = "en"): string {
|
|
if (!name) return "";
|
|
if (typeof name === "string") return name;
|
|
return name[lang as keyof LocalizedText] ?? name.tm ?? name.en ?? name.ru ?? "";
|
|
}
|
|
|
|
function LoanPaidOffLetterViewSkeleton() {
|
|
return (
|
|
<div className="flex flex-col gap-4">
|
|
<Skeleton className="h-7 w-64" />
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
{[...Array(4)].map((_, i) => (
|
|
<div key={i} className="bg-card border border-border rounded-xl p-4 flex flex-col gap-3">
|
|
<Skeleton className="h-3 w-24 mb-1" />
|
|
{[...Array(4)].map((_, j) => (
|
|
<Skeleton key={j} className="h-4 w-full" />
|
|
))}
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function LoanPaidOffLettersViewPage() {
|
|
const { t, i18n } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const { id } = useParams<{ id: string }>();
|
|
const deleteMutation = useDeleteLoanPaidOffLetter();
|
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
|
|
|
const { data: record, isLoading } = useLoanPaidOffLetterById(id!);
|
|
|
|
const confirmDelete = () => {
|
|
deleteMutation.mutate(Number(id!), {
|
|
onSuccess: () => navigate("/loan-paid-off-letters"),
|
|
});
|
|
};
|
|
|
|
if (isLoading) return <LoanPaidOffLetterViewSkeleton />;
|
|
|
|
if (!record) {
|
|
return <div className="flex items-center justify-center h-64 text-muted-foreground text-sm">{t("common.notFound", "Tapylmady")}</div>;
|
|
}
|
|
|
|
const fullName = [record.customerSurname, record.customerName, record.customerPatronicName].filter(Boolean).join(" ");
|
|
|
|
return (
|
|
<div className="flex flex-col gap-6">
|
|
{/* ── Page header ──────────────────────────────────────────────────── */}
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-xl font-semibold text-foreground">
|
|
{t("loanPaidOffLetters.view.title", "Güwanama maglumaty")}: {record.uniqueId}
|
|
</h1>
|
|
<div className="flex items-center gap-1.5">
|
|
<button
|
|
onClick={() => setShowDeleteDialog(true)}
|
|
disabled={deleteMutation.isPending}
|
|
className="p-2 rounded-md cursor-pointer hover:bg-destructive/10 text-muted-foreground hover:text-destructive transition-colors disabled:opacity-50"
|
|
title={t("common.delete", "Poz")}
|
|
>
|
|
<Trash2 size={16} />
|
|
</button>
|
|
<button
|
|
onClick={() => navigate(`/loan-paid-off-letters/${record.id}/edit`)}
|
|
className="p-2 rounded-md cursor-pointer hover:bg-muted/60 text-muted-foreground hover:text-foreground transition-colors"
|
|
title={t("common.edit", "Redaktirle")}
|
|
>
|
|
<Pencil size={16} />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* ── Row 1: Esasy maglumatlar + Müşderi maglumatlary ───────────────── */}
|
|
<BentoGrid cols={2}>
|
|
<BentoCard title={t("loanPaidOffLetters.sections.info", "Esasy maglumatlar")}>
|
|
<InfoRow label={t("common.id", "ID")} value={String(record.id)} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.uniqueId", "Unikal ID")} value={record.uniqueId || "—"} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.status", "Status")}>
|
|
<StatusBadge
|
|
label={t(`loanPaidOffLetters.status.${record.status}`, record.status)}
|
|
variant={STATUS_BADGE_VARIANTS[record.status] ?? "default"}
|
|
/>
|
|
</InfoRow>
|
|
<InfoRow label={t("loanPaidOffLetters.fields.createdAt", "Döredilen wagty")} value={formatDate(record.createdAt)} />
|
|
</BentoCard>
|
|
|
|
<BentoCard title={t("loanPaidOffLetters.sections.customer", "Müşderi maglumatlary")}>
|
|
<InfoRow label={t("loanPaidOffLetters.fields.customerName", "Ady")} value={fullName || "—"} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.bornAt", "Doglan senesi")} value={formatDate(record.bornAt)} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.phone", "Telefon")} value={record.phone || "—"} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.passportSerie", "Pasport seriýasy")} value={record.passportSerie || "—"} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.passportId", "Pasport belgisi")} value={record.passportId || "—"} />
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
{/* ── Row 2: Karz maglumatlary + Goşmaça ───────────────────────────── */}
|
|
<BentoGrid cols={2}>
|
|
<BentoCard title={t("loanPaidOffLetters.sections.loan", "Karz maglumatlary")}>
|
|
<InfoRow
|
|
label={t("loanPaidOffLetters.fields.loanContractNumber", "Karz şertnamasy belgisi")}
|
|
value={record.loanContractNumber || "—"}
|
|
/>
|
|
<InfoRow
|
|
label={t("loanPaidOffLetters.fields.loanContractDate", "Karz şertnamasy senesi")}
|
|
value={formatDate(record.loanContractDate)}
|
|
/>
|
|
<InfoRow label={t("loanPaidOffLetters.fields.loanAmount", "Karz möçberi")} value={record.loanAmount || "—"} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.loanReason", "Karz sebäbi")} value={record.loanReason || "—"} />
|
|
<InfoRow label={t("loanPaidOffLetters.fields.loanFile", "Faýl")}>
|
|
{record.loanFile ? (
|
|
<a
|
|
href={record.loanFile.startsWith("http") ? record.loanFile : buildFileUrl(record.loanFile)}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md bg-primary/10 text-primary text-xs font-medium hover:bg-primary/20 transition-colors"
|
|
>
|
|
<FileText size={14} />
|
|
<span className="truncate max-w-[220px]">{decodeURIComponent(record.loanFile.split("/").pop() || record.loanFile)}</span>
|
|
<ExternalLink size={12} className="ml-1 opacity-70 shrink-0" />
|
|
</a>
|
|
) : (
|
|
<span className="text-muted-foreground">—</span>
|
|
)}
|
|
</InfoRow>
|
|
</BentoCard>
|
|
|
|
<BentoCard title={t("loanPaidOffLetters.sections.additional", "Goşmaça")}>
|
|
<InfoRow label={t("loanPaidOffLetters.fields.region", "Sebit")} value={record.region || "—"} />
|
|
<InfoRow
|
|
label={t("loanPaidOffLetters.fields.branchName", "Şahamça")}
|
|
value={resolveLocalizedName(record.branchName, i18n.language) || (record.branchId != null ? String(record.branchId) : "—")}
|
|
isLink={Boolean(record.branchId)}
|
|
href={record.branchId ? `/settings/location/branches/${record.branchId}` : undefined}
|
|
/>
|
|
<InfoRow
|
|
label={t("loanPaidOffLetters.fields.userName", "Ulanyjy ady")}
|
|
value={record.userName || (record.userId != null ? String(record.userId) : "—")}
|
|
isLink={Boolean(record.userId)}
|
|
href={record.userId ? `/all-users/${record.userId}` : undefined}
|
|
/>
|
|
<InfoRow label={t("loanPaidOffLetters.fields.notes", "Bellikler")} value={record.notes || "—"} />
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
<ConfirmDialog
|
|
open={showDeleteDialog}
|
|
onOpenChange={setShowDeleteDialog}
|
|
title={t("loanPaidOffLetters.deleteConfirm", "Bu ýazgy pozulsynmy?")}
|
|
confirmLabel={t("common.delete", "Poz")}
|
|
onConfirm={confirmDelete}
|
|
isLoading={deleteMutation.isPending}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|