Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions components/reports/publish-flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1325,7 +1332,7 @@ function AgreementStep({ basics, starts: here, workspaceId: pageWorkspace, signe
<AutoHeight>
{bought > 0 ? (
<div className={cn("py-1", starts.balance !== 0 && "px-3")}>
<PromoCode fixed={starts.balance === 0} applied={voucher} onApply={setQuoted} onQuote={(code) => quoteCode({ workspaceId, code, amount: bought }).then(({ off }) => ({ code: code.trim(), off, for: workspaceId }))} typed={typedCode} onTyped={setTypedCode} />
<PromoCode fixed={starts.balance === 0} applied={voucher} onApply={(quote) => { 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} />
</div>
) : null}
</AutoHeight>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2160,7 +2169,7 @@ function Checks({ beside = null, rows, report, humanReview, terms, onActivate, o
<AutoHeight className="min-w-0 flex-1 basis-64 sm:max-w-sm">
{vouchers ? (
<div className={cn("py-1", terms.balance !== 0 && "px-3")}>
<PromoCode id={codeField} error={tried && unapplied ? t("activation.promo.applyFirst") : null} fixed={terms.balance === 0} applied={code} onApply={(quote) => onChoose?.({ voucher: quote })} onQuote={(typedCode) => onQuote(typedCode, sum.bought)} typed={typed} onTyped={setTyped} />
<PromoCode id={codeField} error={tried && unapplied ? t("activation.promo.applyFirst") : null} fixed={terms.balance === 0} applied={code} onApply={(quote) => { onChoose?.({ voucher: quote }); if (quote) void setLinkedCode(null); }} onQuote={(typedCode) => onQuote(typedCode, sum.bought)} typed={typed} onTyped={setTyped} />
</div>
) : null}
</AutoHeight>
Expand Down Expand Up @@ -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<Quote>; /** 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<string | null>(null);
Expand Down
Loading