1
0
Fork 0
mirror of https://github.com/kou029w/quot.git synced 2025-01-19 00:18:09 +00:00
quot/app/views/pages/random.tsx

22 lines
699 B
TypeScript
Raw Normal View History

2022-09-07 22:54:27 +09:00
import { createEffect, createResource } from "solid-js";
import type Pages from "../../protocol/pages";
import random from "../helpers/random";
async function randomPage(): Promise<Pages.ResponsePage> {
const jwt = window.localStorage.getItem("jwt");
const res = await fetch(
`${import.meta.env.QUOT_API_ENDPOINT}/pages?order=random&limit=1`,
{ headers: jwt ? { authorization: `Bearer ${jwt}` } : {} }
);
const data = (await res.json()) as Pages.Response;
return data[0]!;
}
export default () => {
const [page] = createResource(random(), randomPage);
createEffect(() => {
if (!page.loading) window.location.replace(`/${page()!.id.toString(16)}`);
});
return null;
};