mirror of
https://github.com/kou029w/quot.git
synced 2025-02-25 18:15:21 +00:00
random page
This commit is contained in:
parent
e8883d1ef4
commit
f29cc8e838
5 changed files with 59 additions and 15 deletions
|
@ -31,6 +31,7 @@ header h1 > :is(a, a:hover) {
|
|||
}
|
||||
|
||||
header nav > * {
|
||||
margin: 0.75rem;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,13 @@ import "./app.css";
|
|||
import { createSignal } from "solid-js";
|
||||
import Index from "./pages/index";
|
||||
import Page from "./pages/page";
|
||||
import Random from "./pages/random";
|
||||
import random from "./helpers/random";
|
||||
import { decodeJwt } from "jose";
|
||||
|
||||
const routes = {
|
||||
"/": Index,
|
||||
"/random": Random,
|
||||
};
|
||||
|
||||
async function updateUser(jwt: string): Promise<boolean> {
|
||||
|
@ -70,6 +72,7 @@ export default () => {
|
|||
</h1>
|
||||
<nav>
|
||||
<a href={authenticated ? "/new" : "/login"}>📄</a>
|
||||
<a href="/random">🔀</a>
|
||||
</nav>
|
||||
</header>
|
||||
{routes[pathname()] ?? (
|
||||
|
|
21
app/views/pages/random.tsx
Normal file
21
app/views/pages/random.tsx
Normal file
|
@ -0,0 +1,21 @@
|
|||
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;
|
||||
};
|
7
db/migrations/20220907130637_random_pages.sql
Normal file
7
db/migrations/20220907130637_random_pages.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
-- migrate:up
|
||||
CREATE FUNCTION random(pages) RETURNS DOUBLE PRECISION LANGUAGE SQL AS $$
|
||||
SELECT random();
|
||||
$$;
|
||||
|
||||
-- migrate:down
|
||||
DROP FUNCTION random(pages);
|
|
@ -9,20 +9,6 @@ SET xmloption = content;
|
|||
SET client_min_messages = warning;
|
||||
SET row_security = off;
|
||||
|
||||
--
|
||||
-- Name: update_timestamp(); Type: FUNCTION; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE FUNCTION public.update_timestamp() RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
NEW.updated = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END
|
||||
$$;
|
||||
|
||||
|
||||
SET default_tablespace = '';
|
||||
|
||||
SET default_table_access_method = heap;
|
||||
|
@ -42,6 +28,31 @@ CREATE TABLE public.pages (
|
|||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: random(public.pages); Type: FUNCTION; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE FUNCTION public.random(public.pages) RETURNS double precision
|
||||
LANGUAGE sql
|
||||
AS $$
|
||||
SELECT random();
|
||||
$$;
|
||||
|
||||
|
||||
--
|
||||
-- Name: update_timestamp(); Type: FUNCTION; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE FUNCTION public.update_timestamp() RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
NEW.updated = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END
|
||||
$$;
|
||||
|
||||
|
||||
--
|
||||
-- Name: pages_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -186,4 +197,5 @@ CREATE POLICY users_policy ON public.users USING ((id = ((current_setting('reque
|
|||
--
|
||||
|
||||
INSERT INTO public.schema_migrations (version) VALUES
|
||||
('0');
|
||||
('0'),
|
||||
('20220907130637');
|
||||
|
|
Loading…
Add table
Reference in a new issue