diff --git a/components/reports/publish-flow.tsx b/components/reports/publish-flow.tsx index eed673e4d..a70360080 100644 --- a/components/reports/publish-flow.tsx +++ b/components/reports/publish-flow.tsx @@ -305,6 +305,12 @@ const PLACE = { byline: parseAsStringLiteral(BYLINE_BY), }; +/** + * A promotion code a link carries (`/publish?code=…`, handed out at an event): it starts typed in the code's field, + * ready to apply, wherever the person goes on the page, and leaves the address once it is applied. + */ +const useLinkedCode = () => useQueryState("code", parseAsString); + export function PublishFlow() { const { isLoaded, isSignedIn } = useAuth(); const workspaceId = useWorkspace(); @@ -1166,7 +1172,8 @@ function AgreementStep({ basics, starts: here, workspaceId: pageWorkspace, signe // A code is quoted for one workspace's customer, so it counts only for the workspace it was asked for: with another // picked it is asked again, and an answer that comes back after the pick is not the new workspace's either. const voucher: Quote | null = quoted && quoted.for === workspaceId ? { code: quoted.code, off: quoted.off } : null; - const [typedCode, setTypedCode] = useState(""); + const [linkedCode, setLinkedCode] = useLinkedCode(); + const [typedCode, setTypedCode] = useState(() => linkedCode ?? ""); const quoteCode = useAction(api.reports.activation.quoteCode); // How the report is to be paid: from the balance while it holds something, unless the author picks the card. Kept // with the report when it is signed, and open to change until it is activated. @@ -1325,7 +1332,7 @@ function AgreementStep({ basics, starts: here, workspaceId: pageWorkspace, signe {bought > 0 ? (
- quoteCode({ workspaceId, code, amount: bought }).then(({ off }) => ({ code: code.trim(), off, for: workspaceId }))} typed={typedCode} onTyped={setTypedCode} /> + { setQuoted(quote); if (quote) void setLinkedCode(null); }} onQuote={(code) => quoteCode({ workspaceId, code, amount: bought }).then(({ off }) => ({ code: code.trim(), off, for: workspaceId }))} typed={typedCode} onTyped={setTypedCode} />
) : null}
@@ -2045,10 +2052,12 @@ function Checks({ beside = null, rows, report, humanReview, terms, onActivate, o const sum = payment({ price, balance: terms ? terms.balance : 0, payWith, off: code?.off ?? 0, credit: terms?.credit ?? 0, cardSaved: terms?.cardSaved ?? true }); /** Where a code can be used: while a card pays something and no payment is waiting. It lowers what the card pays. */ const vouchers = !!terms && sum.bought > 0 && !pending && !!onQuote; - // What is typed in the code's field, kept here so paying sees it. - const [typed, setTyped] = useState(""); - // A code typed and not applied: nothing is charged past it, or the price without the code would be. - const unapplied = vouchers && typed.trim() !== ""; + // What is typed in the code's field, kept here so paying sees it. A code a link carried starts there. + const [linkedCode, setLinkedCode] = useLinkedCode(); + const [typed, setTyped] = useState(() => linkedCode ?? ""); + // A code typed and not applied: nothing is charged past it, or the price without the code would be. With a code + // applied the field isn't there, so what a link put in it waits for nothing. + const unapplied = vouchers && !code && typed.trim() !== ""; // The charge's dialog closes when the person stops being an admin while it's open: they can't pay for it any more. // It never stays open over a code still to be applied either. if (paying && (!admin || unapplied)) setPaying(false); @@ -2160,7 +2169,7 @@ function Checks({ beside = null, rows, report, humanReview, terms, onActivate, o {vouchers ? (
- onChoose?.({ voucher: quote })} onQuote={(typedCode) => onQuote(typedCode, sum.bought)} typed={typed} onTyped={setTyped} /> + { onChoose?.({ voucher: quote }); if (quote) void setLinkedCode(null); }} onQuote={(typedCode) => onQuote(typedCode, sum.bought)} typed={typed} onTyped={setTyped} />
) : null}
@@ -2368,7 +2377,7 @@ function useDollars() { * Autumn quotes the activation with the code, and says when it doesn't know the code or it has been used up. */ function PromoCode({ id, error = null, applied, onApply, onQuote, typed, onTyped, fixed = false }: { /** The code's box, for the step's way on to take the person to. */ id?: string; /** What the step says is wrong with the field, once its way on was pressed. */ error?: string | null; /** The field is all the section holds (a card is the only way to pay): it stays open, with no way to shut it. */ fixed?: boolean; applied: Quote | null; onApply: (quote: Quote | null) => void; /** Quotes a code typed: the share it takes off what a card would pay. */ onQuote?: (code: string) => Promise; /** What is typed in the field, which its owner holds: nothing is charged while a code waits there. */ typed: string; onTyped: (typed: string) => void }) { - // A code already typed (it came back from Stripe's page in the address) opens the field with it. + // A code already typed (a link carried it in the address) opens the field with it. const [opened, setOpen] = useState(typed !== ""); const open = fixed || opened; const [wrong, setWrong] = useState(null);