2022-08-23 09:01:47 +09:00
|
|
|
import { createResource } from "solid-js";
|
2022-08-24 12:49:02 +09:00
|
|
|
import type Pages from "../protocol/pages";
|
2022-08-23 09:01:47 +09:00
|
|
|
import Editor from "../components/editor";
|
2022-08-24 17:43:32 +09:00
|
|
|
import Card from "../components/card";
|
2022-08-24 12:49:02 +09:00
|
|
|
import beforeunload from "../helpers/beforeunload";
|
|
|
|
import throttle from "../helpers/throttle";
|
2022-08-23 09:01:47 +09:00
|
|
|
|
2022-08-24 12:49:02 +09:00
|
|
|
const intervalMs = 333;
|
|
|
|
const { block, unblock } = beforeunload();
|
2022-08-23 09:01:47 +09:00
|
|
|
|
2022-08-24 12:49:02 +09:00
|
|
|
async function updatePage(
|
|
|
|
id: number,
|
|
|
|
content: Pages.RequestContentPage
|
|
|
|
): Promise<boolean> {
|
|
|
|
const res = await fetch(
|
|
|
|
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL),
|
|
|
|
{
|
|
|
|
method: "PUT",
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
body: JSON.stringify(content),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return res.ok;
|
|
|
|
}
|
|
|
|
|
2022-08-24 22:07:33 +09:00
|
|
|
async function fetchPage(id: number): Promise<Pages.ResponsePage> {
|
2022-08-23 09:01:47 +09:00
|
|
|
const res = await fetch(
|
|
|
|
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL)
|
|
|
|
);
|
2022-08-24 12:49:02 +09:00
|
|
|
const data = (await res.json()) as Pages.Response;
|
|
|
|
return data[0]!;
|
2022-08-23 09:01:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export default (props: { id: number }) => {
|
2022-08-24 12:49:02 +09:00
|
|
|
const [page, { refetch }] = createResource(props.id, fetchPage);
|
|
|
|
const throttledUpdate = throttle(
|
|
|
|
async (id: number, content: Pages.RequestContentPage) => {
|
|
|
|
if (await updatePage(id, content)) {
|
|
|
|
unblock();
|
|
|
|
refetch();
|
2022-08-24 13:17:37 +09:00
|
|
|
window.history.replaceState({}, "", `/${id}`);
|
2022-08-24 12:49:02 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
intervalMs
|
|
|
|
);
|
|
|
|
|
|
|
|
function onUpdatePage(content: Pages.RequestContentPage) {
|
|
|
|
block();
|
|
|
|
throttledUpdate(props.id, content);
|
|
|
|
}
|
2022-08-23 09:01:47 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<main>
|
2022-08-24 12:49:02 +09:00
|
|
|
<Editor id={props.id} onUpdatePage={onUpdatePage} />
|
2022-08-24 17:43:32 +09:00
|
|
|
{() => <Card id={props.id} title="" text="" {...page()} />}
|
2022-08-23 09:01:47 +09:00
|
|
|
</main>
|
|
|
|
);
|
|
|
|
};
|