Files
buzz/app/(ui)/rubgram/form.tsx
T
dmgnr 754379d30d
Build and Push Image / build-backend (push) Successful in 31s
Build and Push Image / build-frontend (push) Successful in 2m21s
Build and Push Image / push (push) Successful in 1m1s
fix(rg): missing actionbutton context
2026-09-30 18:14:02 +07:00

65 lines
1.8 KiB
TypeScript
Executable File

"use client";
import { useProgress } from "@bprogress/next";
import { useRouter } from "next/navigation";
import posthog from "posthog-js";
import { toast } from "sonner";
import { submitEndgame, submitEndgamePayment } from "./api";
import { useRef } from "react";
import {
ActionSubmitContext,
ActionSubmitContextData,
} from "@/components/action-submit-provider";
export function EndgameFormWrapper({
type,
...props
}: React.ComponentProps<"form"> & { type: "registration" | "payment" }) {
const router = useRouter();
const { start, stop } = useProgress();
const callback = useRef<() => void>(() => {});
const context: ActionSubmitContextData = {
listenForComplete(complete) {
callback.current = complete;
},
};
async function submit(data: FormData) {
start();
posthog.capture(
type === "registration"
? "rubgram_registration_submitted"
: "rubgram_payment_submitted",
);
({ registration: submitEndgame, payment: submitEndgamePayment })
[type](data)
.then((q) => {
if (typeof q === "string") return toast.error(q);
try {
// biome-ignore lint/suspicious/noTsIgnore: error appears on build but not visible to biome
// @ts-ignore
cookieStore.set("rsid", q.id);
} catch (e) {
console.error("Failed to set cookie", e);
}
router.refresh();
})
.catch((e) => toast.error(`${e.message || e}`))
.finally(stop);
}
return (
<ActionSubmitContext.Provider value={context}>
<form
{...props}
onSubmit={(e) => {
e.preventDefault();
submit(new FormData(e.target as HTMLFormElement)).finally(() =>
setTimeout(callback.current, 1000),
);
}}
/>
</ActionSubmitContext.Provider>
);
}