mirror of
https://github.com/kou029w/quot.git
synced 2025-01-19 00:18:09 +00:00
use refetch
This commit is contained in:
parent
851b942276
commit
86afc69f56
4 changed files with 68 additions and 48 deletions
|
@ -1,29 +1,10 @@
|
||||||
|
import type Pages from "../protocol/pages";
|
||||||
import "./editor.css";
|
import "./editor.css";
|
||||||
import beforeunload from "../helpers/beforeunload";
|
|
||||||
import throttle from "../helpers/throttle";
|
|
||||||
|
|
||||||
const intervalMs = 333;
|
export default (props: {
|
||||||
|
|
||||||
const { block, unblock } = beforeunload();
|
|
||||||
|
|
||||||
const updatePage = throttle(async function (params: {
|
|
||||||
id: number;
|
id: number;
|
||||||
title: string;
|
onUpdatePage: (content: Pages.RequestContentPage) => void;
|
||||||
text: string;
|
}) => {
|
||||||
}) {
|
|
||||||
const res = await fetch(
|
|
||||||
new URL(`/pages?id=eq.${params.id}`, import.meta.env.QUOT_API_URL),
|
|
||||||
{
|
|
||||||
method: "PUT",
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
body: JSON.stringify(params),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (res.ok) unblock();
|
|
||||||
},
|
|
||||||
intervalMs);
|
|
||||||
|
|
||||||
export default (props: { id: number }) => {
|
|
||||||
return (
|
return (
|
||||||
<textarea
|
<textarea
|
||||||
id={String(props.id)}
|
id={String(props.id)}
|
||||||
|
@ -32,8 +13,7 @@ export default (props: { id: number }) => {
|
||||||
onInput={(e) => {
|
onInput={(e) => {
|
||||||
const text = e.currentTarget.value;
|
const text = e.currentTarget.value;
|
||||||
const lines = text.split("\n");
|
const lines = text.split("\n");
|
||||||
block();
|
props.onUpdatePage({ id: props.id, title: lines[0] ?? "", text });
|
||||||
updatePage({ id: props.id, title: lines[0] ?? "", text });
|
|
||||||
e.currentTarget.setAttribute("rows", String(Math.max(2, lines.length)));
|
e.currentTarget.setAttribute("rows", String(Math.max(2, lines.length)));
|
||||||
}}
|
}}
|
||||||
></textarea>
|
></textarea>
|
||||||
|
|
|
@ -1,19 +1,12 @@
|
||||||
import { createResource } from "solid-js";
|
import { createResource } from "solid-js";
|
||||||
|
import type Pages from "../protocol/pages";
|
||||||
|
|
||||||
type PagesResponse = Array<{
|
async function fetchPages(): Promise<Pages.Response> {
|
||||||
id: number;
|
|
||||||
title: string;
|
|
||||||
text: string;
|
|
||||||
created: string;
|
|
||||||
updated: string;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
async function fetchPages(): Promise<PagesResponse> {
|
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
new URL("/pages?order=updated.desc", import.meta.env.QUOT_API_URL)
|
new URL("/pages?order=updated.desc", import.meta.env.QUOT_API_URL)
|
||||||
);
|
);
|
||||||
const data = await res.json();
|
const data = (await res.json()) as Pages.Response;
|
||||||
return data as PagesResponse;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
|
|
@ -1,28 +1,55 @@
|
||||||
import { createResource } from "solid-js";
|
import { createResource } from "solid-js";
|
||||||
|
import type Pages from "../protocol/pages";
|
||||||
import Editor from "../components/editor";
|
import Editor from "../components/editor";
|
||||||
|
import beforeunload from "../helpers/beforeunload";
|
||||||
|
import throttle from "../helpers/throttle";
|
||||||
|
|
||||||
type PageResponse = {
|
const intervalMs = 333;
|
||||||
id: number;
|
const { block, unblock } = beforeunload();
|
||||||
title: string;
|
|
||||||
text: string;
|
|
||||||
created: string;
|
|
||||||
updated: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function fetchPage(id: number): Promise<PageResponse> {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchPage(id: number): Promise<Pages.RequestContentPage> {
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL)
|
new URL(`/pages?id=eq.${id}`, import.meta.env.QUOT_API_URL)
|
||||||
);
|
);
|
||||||
const [data] = await res.json();
|
const data = (await res.json()) as Pages.Response;
|
||||||
return data as PageResponse;
|
return data[0]!;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (props: { id: number }) => {
|
export default (props: { id: number }) => {
|
||||||
const [page] = createResource(props.id, fetchPage);
|
const [page, { refetch }] = createResource(props.id, fetchPage);
|
||||||
|
const throttledUpdate = throttle(
|
||||||
|
async (id: number, content: Pages.RequestContentPage) => {
|
||||||
|
if (await updatePage(id, content)) {
|
||||||
|
unblock();
|
||||||
|
refetch();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
intervalMs
|
||||||
|
);
|
||||||
|
|
||||||
|
function onUpdatePage(content: Pages.RequestContentPage) {
|
||||||
|
block();
|
||||||
|
throttledUpdate(props.id, content);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<Editor id={props.id} />
|
<Editor id={props.id} onUpdatePage={onUpdatePage} />
|
||||||
<pre>{() => JSON.stringify(page(), null, " ")}</pre>
|
<pre>{() => JSON.stringify(page(), null, " ")}</pre>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
|
|
20
app/src/protocol/pages.ts
Normal file
20
app/src/protocol/pages.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
namespace Pages {
|
||||||
|
export type RequestContentPage = {
|
||||||
|
id?: number;
|
||||||
|
title: string;
|
||||||
|
text: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResponsePage = {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
text: string;
|
||||||
|
created: string;
|
||||||
|
updated: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RequestContent = Array<RequestContentPage>;
|
||||||
|
export type Response = Array<ResponsePage>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Pages;
|
Loading…
Add table
Reference in a new issue