mirror of
https://github.com/temetro/temetro.git
synced 2026-08-08 09:43:13 +00:00
76da310766
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>
133 lines
3.9 KiB
TypeScript
133 lines
3.9 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import { ApiError } from "@/lib/api-client";
|
|
import {
|
|
getWalletLink,
|
|
getWalletUpdate,
|
|
pushWalletUpdate,
|
|
type WalletUpdate,
|
|
} from "@/lib/wallet-updates";
|
|
|
|
export type WalletSyncState =
|
|
| "idle"
|
|
| "pending"
|
|
| "approved"
|
|
| "denied"
|
|
| "error";
|
|
|
|
// Shared wallet-sync logic for create/edit dialogs. It resolves whether the
|
|
// selected patient has a linked wallet, then (after the primary save) can push
|
|
// the change to their phone and poll until they approve/deny it — the same
|
|
// mechanism as the standalone WalletPushDialog, lifted out for reuse.
|
|
export function useWalletSync(fileNumber: string | null | undefined) {
|
|
const [linked, setLinked] = useState(false);
|
|
const [checking, setChecking] = useState(false);
|
|
const [state, setState] = useState<WalletSyncState>("idle");
|
|
const [update, setUpdate] = useState<WalletUpdate | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
// Resolve link status whenever the chosen patient changes. A 404 simply means
|
|
// "not wallet-backed", so failures collapse to `linked = false`.
|
|
useEffect(() => {
|
|
if (!fileNumber) {
|
|
setLinked(false);
|
|
return;
|
|
}
|
|
let active = true;
|
|
setChecking(true);
|
|
getWalletLink(fileNumber)
|
|
.then(() => {
|
|
if (active) setLinked(true);
|
|
})
|
|
.catch(() => {
|
|
if (active) setLinked(false);
|
|
})
|
|
.finally(() => {
|
|
if (active) setChecking(false);
|
|
});
|
|
return () => {
|
|
active = false;
|
|
};
|
|
}, [fileNumber]);
|
|
|
|
// Poll the pushed update until the patient approves/denies it.
|
|
useEffect(() => {
|
|
if (state !== "pending" || !update || update.resolvedAt) return;
|
|
let active = true;
|
|
const timer = setInterval(async () => {
|
|
try {
|
|
const fresh = await getWalletUpdate(update.id);
|
|
if (!active) return;
|
|
setUpdate(fresh);
|
|
if (fresh.status === "approved") setState("approved");
|
|
else if (fresh.status === "denied") setState("denied");
|
|
if (fresh.resolvedAt) clearInterval(timer);
|
|
} catch {
|
|
/* keep polling */
|
|
}
|
|
}, 3000);
|
|
return () => {
|
|
active = false;
|
|
clearInterval(timer);
|
|
};
|
|
}, [state, update]);
|
|
|
|
// Authoritatively resolve link status at submit time. The `linked` state above
|
|
// is populated asynchronously by the effect, so a fast save (or a transient
|
|
// failure that collapsed it to false) can leave a wallet-backed patient looking
|
|
// unlinked. Callers await this before deciding whether to show the wallet step,
|
|
// so the decision is never made on an unresolved check.
|
|
const ensureLinked = useCallback(async (): Promise<boolean> => {
|
|
if (!fileNumber) {
|
|
setLinked(false);
|
|
return false;
|
|
}
|
|
setChecking(true);
|
|
try {
|
|
await getWalletLink(fileNumber);
|
|
setLinked(true);
|
|
return true;
|
|
} catch {
|
|
setLinked(false);
|
|
return false;
|
|
} finally {
|
|
setChecking(false);
|
|
}
|
|
}, [fileNumber]);
|
|
|
|
const push = useCallback(
|
|
async (changes: string[]) => {
|
|
if (!fileNumber) return;
|
|
// Never push an empty/whitespace change set — the backend rejects it (400).
|
|
const clean = changes.map((c) => c.trim()).filter(Boolean);
|
|
if (clean.length === 0) {
|
|
setError("generic");
|
|
setState("error");
|
|
return;
|
|
}
|
|
setError(null);
|
|
setState("pending");
|
|
try {
|
|
const created = await pushWalletUpdate({ fileNumber, changes: clean });
|
|
setUpdate(created);
|
|
} catch (err) {
|
|
setError(err instanceof ApiError ? err.message : "generic");
|
|
setState("error");
|
|
}
|
|
},
|
|
[fileNumber],
|
|
);
|
|
|
|
const reset = useCallback(() => {
|
|
setState("idle");
|
|
setUpdate(null);
|
|
setError(null);
|
|
}, []);
|
|
|
|
return { linked, checking, state, update, error, ensureLinked, push, reset };
|
|
}
|
|
|
|
export type UseWalletSync = ReturnType<typeof useWalletSync>;
|