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 Form from "@/islands/Form.tsx";
|
||||||
import { Signal } from "@preact/signals";
|
import { post_json } from "@/src/client.ts";
|
||||||
|
|
||||||
interface AskCodeProps {
|
interface AskCodeProps {
|
||||||
page: Signal<string>;
|
page: Signal<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function AskCode({ page }: AskCodeProps) {
|
export default function AskCode({ page }: AskCodeProps) {
|
||||||
|
const checking = useSignal(false);
|
||||||
|
|
||||||
const on_success = (data: { code: string }) => {
|
const on_success = (data: { code: string }) => {
|
||||||
localStorage.setItem("code", data.code);
|
localStorage.setItem("code", data.code);
|
||||||
page.value = "keys";
|
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 (
|
return (
|
||||||
<Form action="/api/verify_code" on_success={on_success}>
|
<Form action="/api/verify_code" on_success={on_success}>
|
||||||
<input type="text" name="code" />
|
<input type="text" name="code" />
|
||||||
|
|||||||
+7
-22
@@ -1,6 +1,8 @@
|
|||||||
import type { ComponentChildren } from "preact";
|
import type { ComponentChildren } from "preact";
|
||||||
import { useSignal } from "@preact/signals";
|
import { useSignal } from "@preact/signals";
|
||||||
|
|
||||||
|
import { post_json } from "@/src/client.ts";
|
||||||
|
|
||||||
interface FormProps<T> {
|
interface FormProps<T> {
|
||||||
action: string;
|
action: string;
|
||||||
on_success: (data: T) => void;
|
on_success: (data: T) => void;
|
||||||
@@ -26,29 +28,12 @@ export default function Form<T = unknown>(props: FormProps<T>) {
|
|||||||
|
|
||||||
submitting.value = true;
|
submitting.value = true;
|
||||||
error.value = "";
|
error.value = "";
|
||||||
try {
|
const result = await post_json<T>(props.action, body, props.method);
|
||||||
const method = props.method ?? "POST";
|
submitting.value = false;
|
||||||
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);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (result.ok) props.on_success(result.data);
|
||||||
throw new Error(data?.error ?? `Request failed with status ${response.status}`);
|
else if (props.on_error) props.on_error(result.error);
|
||||||
}
|
else error.value = result.error;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
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