User codes
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import Form from "@/islands/Form.tsx";
|
||||
import { Signal } from "@preact/signals";
|
||||
|
||||
interface AskCodeProps {
|
||||
page: Signal<string>;
|
||||
}
|
||||
|
||||
export default function AskCode({ page }: AskCodeProps) {
|
||||
const on_success = (data: { code: string }) => {
|
||||
localStorage.setItem("code", data.code);
|
||||
page.value = "keys";
|
||||
};
|
||||
|
||||
return (
|
||||
<Form action="/api/verify_code" on_success={on_success}>
|
||||
<input type="text" name="code" />
|
||||
<button type="submit">Enter</button>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { ComponentChildren } from "preact";
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
interface FormProps<T> {
|
||||
action: string;
|
||||
on_success: (data: T) => void;
|
||||
on_error?: (message: string) => void;
|
||||
class?: string;
|
||||
method?: string;
|
||||
children: ComponentChildren;
|
||||
}
|
||||
|
||||
// Posts the form's named fields as a JSON object of strings
|
||||
export default function Form<T = unknown>(props: FormProps<T>) {
|
||||
const submitting = useSignal(false);
|
||||
const error = useSignal("");
|
||||
|
||||
async function submit(event: SubmitEvent) {
|
||||
event.preventDefault();
|
||||
if (submitting.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const form = event.currentTarget as HTMLFormElement;
|
||||
const body = Object.fromEntries(new FormData(form));
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form class={props.class} onSubmit={submit}>
|
||||
<fieldset disabled={submitting.value}>{props.children}</fieldset>
|
||||
{error.value && <p role="alert">{error.value}</p>}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
import AskCode from "./AskCode.tsx";
|
||||
|
||||
export default function MainIsland() {
|
||||
const page = useSignal("askcode");
|
||||
|
||||
if (page.value === "askcode") {
|
||||
return <AskCode page={page} />;
|
||||
} else if (page.value === "keys") {
|
||||
return localStorage.getItem("code");
|
||||
} else {
|
||||
return "Error";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user