Use saved code
This commit is contained in:
+30
-1
@@ -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<string>;
|
||||
}
|
||||
|
||||
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 <p>Verifying code...</p>;
|
||||
}
|
||||
|
||||
return (
|
||||
<Form action="/api/verify_code" on_success={on_success}>
|
||||
<input type="text" name="code" />
|
||||
|
||||
+7
-22
@@ -1,6 +1,8 @@
|
||||
import type { ComponentChildren } from "preact";
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
import { post_json } from "@/src/client.ts";
|
||||
|
||||
interface FormProps<T> {
|
||||
action: string;
|
||||
on_success: (data: T) => void;
|
||||
@@ -26,29 +28,12 @@ export default function Form<T = unknown>(props: FormProps<T>) {
|
||||
|
||||
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<T>(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 (
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
export type PostJsonResult<T> =
|
||||
| { 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<T>(url: string, body: unknown, method = "POST"): Promise<PostJsonResult<T>> {
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user