Files
temetro/frontend/components/wallet/wallet-sync-step.tsx
T
Khalid Abdi 76da310766 frontend: fix wallet push silently skipping in form dialogs
The "send to wallet" step in the appointment/invoice/prescription/
patient-edit/scribe dialogs was gated on sync.linked, which resolves
asynchronously via getWalletLink in an effect. A fast save (or a
transient failure that collapsed it to false) left a wallet-linked
patient looking unlinked, so the dialog took the else branch and pushed
nothing.

- Add ensureLinked() to useWalletSync: awaits getWalletLink at submit and
  returns the resolved status. Each of the 5 dialogs now awaits it before
  deciding whether to show the wallet step.
- Harden push() to drop empty/whitespace changes (the backend 400s on an
  empty change set) and add a translated summaryFallback so the step
  never sends an empty summary.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 21:10:04 +03:00

139 lines
4.8 KiB
TypeScript

"use client";
import { Check, Loader2, Send, X } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { DialogFooter, DialogPanel } from "@/components/ui/dialog";
import {
Stepper,
StepperIndicator,
StepperItem,
StepperSeparator,
StepperTitle,
} from "@/components/ui/stepper";
import type { UseWalletSync } from "./use-wallet-sync";
// Two-step header shown at the top of a dialog when the selected patient has a
// linked wallet: "Details" → "Sync to wallet". Centered so the numbered
// indicators sit inline with their labels.
export function DialogStepper({ step }: { step: "form" | "wallet" }) {
const { t } = useTranslation();
const steps = [
{ value: 1, label: t("walletSync.step1") },
{ value: 2, label: t("walletSync.step2") },
];
const activeValue = step === "form" ? 1 : 2;
return (
<Stepper className="mx-auto mt-3 max-w-sm" value={activeValue}>
{steps.map(({ value, label }, i) => (
<StepperItem className="not-last:flex-1" key={value} step={value}>
<span className="inline-flex items-center gap-2">
<StepperIndicator />
<StepperTitle className="whitespace-nowrap text-xs">
{label}
</StepperTitle>
</span>
{i < steps.length - 1 && <StepperSeparator className="mx-3" />}
</StepperItem>
))}
</Stepper>
);
}
// Step 2 body + footer: offer to push the just-saved change to the patient's
// wallet, then show the approval status. Rendered in place of the form's
// DialogPanel/DialogFooter, so it returns them as a fragment.
export function WalletSyncStep({
patientName,
summary,
sync,
onDone,
}: {
patientName: string;
summary: string;
sync: UseWalletSync;
onDone: () => void;
}) {
const { t } = useTranslation();
const { state, update, error, push } = sync;
const status = update?.status ?? "pending";
// The summary is the human-readable change the patient approves. Guard against
// an empty one (the backend 400s on empty changes) with a translated fallback.
const changeSummary = summary.trim() || t("walletSync.summaryFallback");
return (
<>
<DialogPanel className="min-h-0 flex-1 overflow-y-auto">
{state === "idle" || state === "error" ? (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-1">
<p className="font-medium text-foreground text-sm">
{t("walletSync.prompt", { name: patientName })}
</p>
<p className="text-muted-foreground text-sm">
{t("walletSync.promptBody")}
</p>
</div>
<div className="flex flex-col gap-1.5">
<span className="text-muted-foreground text-xs">
{t("walletSync.changesLabel")}
</span>
<div className="rounded-lg border bg-muted/50 px-3 py-2 text-foreground text-sm">
{changeSummary}
</div>
</div>
{error && (
<p className="text-destructive text-sm" role="alert">
{error === "generic" ? t("walletSync.errors.generic") : error}
</p>
)}
</div>
) : (
<div className="flex flex-col items-center gap-4 py-6 text-center">
{status === "approved" ? (
<div className="flex size-12 items-center justify-center rounded-full bg-success/15 text-success">
<Check className="size-6" />
</div>
) : status === "denied" ? (
<div className="flex size-12 items-center justify-center rounded-full bg-destructive/15 text-destructive">
<X className="size-6" />
</div>
) : (
<Loader2 className="size-8 animate-spin text-muted-foreground" />
)}
<div className="flex flex-col gap-1">
<p className="font-medium text-foreground text-sm">
{t(`walletPush.status.${status}.title`)}
</p>
<p className="text-muted-foreground text-sm">
{t(`walletPush.status.${status}.body`)}
</p>
</div>
</div>
)}
</DialogPanel>
<DialogFooter>
{state === "idle" || state === "error" ? (
<>
<Button onClick={onDone} type="button" variant="outline">
{t("walletSync.skip")}
</Button>
<Button onClick={() => push([changeSummary])} type="button">
<Send className="size-4" />
{t("walletSync.send")}
</Button>
</>
) : (
<Button onClick={onDone} type="button">
{t("walletSync.done")}
</Button>
)}
</DialogFooter>
</>
);
}