104 lines
4.3 KiB
TypeScript
104 lines
4.3 KiB
TypeScript
import { Form, useActionData, useLoaderData, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { CatalogError } from "@hivegrid/foundation";
|
|
import { recordSignatureLinkOpened, signWithOtp, SignatureServiceError } from "~/modules/signature/services/signatureService.server";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
signerName: string;
|
|
signerEmail: string;
|
|
status: string;
|
|
};
|
|
|
|
type ActionData = { ok: true } | { ok: false; error: string };
|
|
|
|
function requestIp(request: Request): string | null {
|
|
return request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || request.headers.get("x-real-ip");
|
|
}
|
|
|
|
function userAgent(request: Request): string | null {
|
|
return request.headers.get("user-agent");
|
|
}
|
|
|
|
function formString(formData: FormData, name: string): string | null {
|
|
const value = formData.get(name);
|
|
return typeof value === "string" ? value : null;
|
|
}
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const token = params.token;
|
|
if (!token) throw new Response(null, { status: 404 });
|
|
try {
|
|
const data = await recordSignatureLinkOpened({ token, ipAddress: requestIp(request), userAgent: userAgent(request) });
|
|
return Response.json({ title: data.title, signerName: data.signerName, signerEmail: data.signerEmail, status: data.status } satisfies LoaderData);
|
|
} catch (error) {
|
|
if (error instanceof CatalogError) throw new Response(null, { status: 404 });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
const token = params.token;
|
|
if (!token) throw new Response(null, { status: 404 });
|
|
const formData = await request.formData();
|
|
try {
|
|
await signWithOtp({
|
|
token,
|
|
otp: formString(formData, "otp"),
|
|
signatureSvg: formString(formData, "signatureSvg"),
|
|
intentAccepted: formData.get("intentAccepted") === "on",
|
|
ipAddress: requestIp(request),
|
|
userAgent: userAgent(request),
|
|
});
|
|
return Response.json({ ok: true } satisfies ActionData);
|
|
} catch (error) {
|
|
if (error instanceof SignatureServiceError) {
|
|
return Response.json({ ok: false, error: error.messageKey } satisfies ActionData, { status: 400 });
|
|
}
|
|
if (error instanceof CatalogError) throw new Response(null, { status: 404 });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export default function SignatureLinkRoute() {
|
|
const { t } = useTranslation();
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
const isComplete = actionData?.ok === true || data.status === "signed";
|
|
|
|
return (
|
|
<main className="mx-auto flex min-h-screen w-full max-w-xl flex-col justify-center gap-6 p-6">
|
|
<header className="grid gap-2">
|
|
<p className="text-muted-foreground text-sm">{data.signerEmail}</p>
|
|
<h1 className="text-2xl font-semibold">{data.title}</h1>
|
|
<p className="text-muted-foreground text-sm">{t("signature.public.intentIntro", { name: data.signerName })}</p>
|
|
</header>
|
|
{isComplete ? (
|
|
<section className="bg-background rounded-md border p-4">
|
|
<h2 className="font-semibold">{t("signature.public.completeTitle")}</h2>
|
|
<p className="text-muted-foreground mt-2 text-sm">{t("signature.public.completeBody")}</p>
|
|
</section>
|
|
) : (
|
|
<Form className="bg-background grid gap-4 rounded-md border p-4" method="post">
|
|
{actionData?.ok === false ? <p className="border-destructive/40 bg-destructive/10 rounded-md border p-3 text-sm">{t(actionData.error)}</p> : null}
|
|
<label className="grid gap-2 text-sm">
|
|
<span>{t("signature.public.otp")}</span>
|
|
<input className="rounded-md border px-3 py-2" inputMode="numeric" maxLength={6} name="otp" />
|
|
</label>
|
|
<label className="grid gap-2 text-sm">
|
|
<span>{t("signature.public.signature")}</span>
|
|
<textarea className="min-h-28 rounded-md border px-3 py-2" name="signatureSvg" />
|
|
</label>
|
|
<label className="flex gap-2 text-sm">
|
|
<input name="intentAccepted" type="checkbox" />
|
|
<span>{t("signature.public.intentCheckbox")}</span>
|
|
</label>
|
|
<button className="bg-primary text-primary-foreground rounded-md px-3 py-2 text-sm" type="submit">
|
|
{t("signature.public.submit")}
|
|
</button>
|
|
</Form>
|
|
)}
|
|
</main>
|
|
);
|
|
}
|