122 lines
4.9 KiB
TypeScript
122 lines
4.9 KiB
TypeScript
// pages/OnlinePaymentDetailPage.tsx
|
|
|
|
import { useParams } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Section } from "@/components/viewPageComponents";
|
|
import { useGetOnlinePaymentById } from "@/features/onlinePaymentHistory/hooks/useOnlinePaymentsHistory";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import type { PaymentHistoryStep } from "@/features/onlinePaymentHistory/api/onlinePaymentsHistoryApi";
|
|
import { GatewayStatusBadge } from "@/features/onlinePaymentHistory/components/PaymentStatusBadge";
|
|
import { rawEnumKey, type TranslateFn } from "@/constants/enumLabels";
|
|
|
|
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
function formatDateTime(iso: string): string {
|
|
const d = new Date(iso);
|
|
const hh = String(d.getHours()).padStart(2, "0");
|
|
const mm = String(d.getMinutes()).padStart(2, "0");
|
|
const dd = String(d.getDate()).padStart(2, "0");
|
|
const mo = String(d.getMonth() + 1).padStart(2, "0");
|
|
const yy = d.getFullYear();
|
|
return `${hh}:${mm}, ${dd}.${mo}.${yy}`;
|
|
}
|
|
|
|
/** Backend sends raw keys like "gateway_reference" — look them up under `onlinePayments.detailKeys`. */
|
|
function detailKeyLabel(key: string, t: TranslateFn): string {
|
|
return t(`onlinePayments.detailKeys.${key}`, rawEnumKey(key));
|
|
}
|
|
|
|
// ─── StepRow ──────────────────────────────────────────────────────────────────
|
|
|
|
function StepRow({ step }: { step: PaymentHistoryStep }) {
|
|
const { t } = useTranslation();
|
|
const detailEntries = Object.entries(step.detail ?? {});
|
|
|
|
return (
|
|
<div className="flex gap-4 relative">
|
|
{/* Timeline line */}
|
|
<div className="flex flex-col items-center">
|
|
<div className="w-3 h-3 rounded-full bg-primary mt-1 shrink-0 z-10" />
|
|
<div className="w-px flex-1 bg-border mt-1" />
|
|
</div>
|
|
|
|
{/* Content */}
|
|
<div className="pb-6 flex-1 min-w-0">
|
|
<div className="flex flex-wrap items-center gap-3 mb-1">
|
|
<GatewayStatusBadge status={step.step} />
|
|
<span className="text-xs text-muted-foreground whitespace-nowrap">{formatDateTime(step.createdAt)}</span>
|
|
<span className="text-xs text-muted-foreground">{step.createdByName}</span>
|
|
</div>
|
|
|
|
{detailEntries.length > 0 && (
|
|
<div className="mt-2 rounded-md border bg-muted/30 px-3 py-2 space-y-1">
|
|
{detailEntries.map(([key, value]) => (
|
|
<div key={key} className="flex gap-2 text-xs">
|
|
<span className="text-muted-foreground font-medium min-w-[120px]">{detailKeyLabel(key, t)}</span>
|
|
<span className="text-foreground break-all">{String(value)}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ─── OnlinePaymentDetailPage ──────────────────────────────────────────────────
|
|
|
|
export default function OnlinePaymentDetailPage() {
|
|
const { id } = useParams<{ id: string }>();
|
|
const numericId = Number(id);
|
|
const { t } = useTranslation();
|
|
|
|
const { data: steps, isLoading } = useGetOnlinePaymentById(numericId);
|
|
|
|
// ─── Loading skeleton ──────────────────────────────────────────────────────
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="p-6 space-y-4">
|
|
<Skeleton className="h-6 w-72" />
|
|
<Skeleton className="h-[400px] w-full rounded-xl" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!steps || steps.length === 0) {
|
|
return (
|
|
<div className="p-6">
|
|
<p className="text-muted-foreground">{t("onlinePayments.noHistory", "Töleg taryhy tapylmady")}</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const orderId = steps[0]?.orderId;
|
|
|
|
// ─── Render ───────────────────────────────────────────────────────────────
|
|
|
|
return (
|
|
<div>
|
|
<h1 className="text-xl font-semibold text-foreground mb-6">
|
|
{t("onlinePayments.detail.title", "Onlaýn töleg taryhy giňişleýin")}
|
|
{orderId ? ` — Order #${orderId}` : ""}
|
|
</h1>
|
|
|
|
<Section>
|
|
<div className="px-1 py-2">
|
|
<p className="text-sm font-medium text-muted-foreground mb-6">
|
|
{t("onlinePayments.detail.stepsCount", "Ädimler")}: {steps.length}
|
|
</p>
|
|
|
|
{/* Timeline */}
|
|
<div>
|
|
{steps.map((step) => (
|
|
<StepRow key={step.id} step={step} />
|
|
))}
|
|
</div>
|
|
</div>
|
|
</Section>
|
|
</div>
|
|
);
|
|
}
|