From 7608408e590cf839d5e45beaae2f9f9bbc051ebe Mon Sep 17 00:00:00 2001 From: paulaboks Date: Thu, 29 Oct 2026 16:26:36 -0300 Subject: [PATCH] Use saved code --- islands/AskCode.tsx | 31 ++++++++++++++++++++++++++++++- islands/Form.tsx | 29 +++++++---------------------- src/client.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 src/client.ts diff --git a/islands/AskCode.tsx b/islands/AskCode.tsx index d52231e..4240f18 100644 --- a/islands/AskCode.tsx +++ b/islands/AskCode.tsx @@ -1,16 +1,45 @@ +import { Signal, useSignal, useSignalEffect } from "@preact/signals"; + import Form from "@/islands/Form.tsx"; -import { Signal } from "@preact/signals"; +import { post_json } from "@/src/client.ts"; interface AskCodeProps { page: Signal; } export default function AskCode({ page }: AskCodeProps) { + const checking = useSignal(false); + const on_success = (data: { code: string }) => { localStorage.setItem("code", data.code); page.value = "keys"; }; + useSignalEffect(() => { + const fn = async () => { + const code = localStorage.getItem("code"); + if (!code) { + return; + } + + checking.value = true; + const result = await post_json<{ code: string }>("/api/verify_code", { code }); + checking.value = false; + + if (result.ok) { + on_success(result.data); + } else if (result.status !== 0) { + // Network errors keep the code so it can be retried on the next load + localStorage.removeItem("code"); + } + }; + fn(); + }); + + if (checking.value) { + return

Verifying code...

; + } + return (
diff --git a/islands/Form.tsx b/islands/Form.tsx index 8ae9a89..fc976b0 100644 --- a/islands/Form.tsx +++ b/islands/Form.tsx @@ -1,6 +1,8 @@ import type { ComponentChildren } from "preact"; import { useSignal } from "@preact/signals"; +import { post_json } from "@/src/client.ts"; + interface FormProps { action: string; on_success: (data: T) => void; @@ -26,29 +28,12 @@ export default function Form(props: FormProps) { submitting.value = true; error.value = ""; - try { - const method = props.method ?? "POST"; - const response = await fetch( - props.action, - method.toUpperCase() === "GET" ? { method } : { - method, - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - }, - ); - const data = response.status === 204 ? undefined : await response.json().catch(() => undefined); + const result = await post_json(props.action, body, props.method); + submitting.value = false; - if (!response.ok) { - throw new Error(data?.error ?? `Request failed with status ${response.status}`); - } - props.on_success(data as T); - } catch (err) { - const message = err instanceof Error ? err.message : String(err); - if (props.on_error) props.on_error(message); - else error.value = message; - } finally { - submitting.value = false; - } + if (result.ok) props.on_success(result.data); + else if (props.on_error) props.on_error(result.error); + else error.value = result.error; } return ( diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..97e853d --- /dev/null +++ b/src/client.ts @@ -0,0 +1,27 @@ +export type PostJsonResult = + | { ok: true; data: T } + // status is 0 when the request never reached the server + | { ok: false; status: number; error: string }; + +export async function post_json(url: string, body: unknown, method = "POST"): Promise> { + let response: Response; + try { + response = await fetch(url, { + method, + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }); + } catch (err) { + return { ok: false, status: 0, error: err instanceof Error ? err.message : String(err) }; + } + + const data = response.status === 204 ? undefined : await response.json().catch(() => undefined); + if (!response.ok) { + return { + ok: false, + status: response.status, + error: data?.error ?? `Request failed with status ${response.status}`, + }; + } + return { ok: true, data: data as T }; +}