From ec120e7d2619d9d28751b49bbc9bd1999a22bdf9 Mon Sep 17 00:00:00 2001 From: monoid Date: Sat, 13 Apr 2024 12:55:46 +0900 Subject: [PATCH] fix: fetcher arguments --- packages/client/src/hook/fetcher.ts | 13 +++- packages/client/src/hook/useDifference.ts | 8 +-- packages/client/src/page/differencePage.tsx | 5 +- packages/client/src/page/galleryPage.tsx | 76 ++++++++++----------- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/packages/client/src/hook/fetcher.ts b/packages/client/src/hook/fetcher.ts index 5d02720..d35738f 100644 --- a/packages/client/src/hook/fetcher.ts +++ b/packages/client/src/hook/fetcher.ts @@ -4,8 +4,17 @@ export function makeApiUrl(pathnameAndQueryparam: string) { return new URL(pathnameAndQueryparam, BASE_API_URL).toString(); } -export async function fetcher(url: string) { +export class ApiError extends Error { + constructor(public readonly status: number, message: string) { + super(message); + } +} + +export async function fetcher(url: string, init?: RequestInit) { const u = makeApiUrl(url); - const res = await fetch(u); + const res = await fetch(u, init); + if (!res.ok) { + throw new ApiError(res.status, await res.text()); + } return res.json(); } diff --git a/packages/client/src/hook/useDifference.ts b/packages/client/src/hook/useDifference.ts index 37976a5..627f628 100644 --- a/packages/client/src/hook/useDifference.ts +++ b/packages/client/src/hook/useDifference.ts @@ -14,7 +14,7 @@ export function useDifferenceDoc() { } export async function commit(path: string, type: string) { - const res = await fetch("/api/diff/commit", { + const data = await fetcher("/api/diff/commit", { method: "POST", body: JSON.stringify([{ path, type }]), headers: { @@ -22,11 +22,11 @@ export async function commit(path: string, type: string) { }, }); mutate("/api/diff/list"); - return res.ok; + return data; } export async function commitAll(type: string) { - const res = await fetch("/api/diff/commitall", { + const data = await fetcher("/api/diff/commitall", { method: "POST", body: JSON.stringify({ type }), headers: { @@ -34,5 +34,5 @@ export async function commitAll(type: string) { }, }); mutate("/api/diff/list"); - return res.ok; + return data; } \ No newline at end of file diff --git a/packages/client/src/page/differencePage.tsx b/packages/client/src/page/differencePage.tsx index efe7ffc..0d49d9b 100644 --- a/packages/client/src/page/differencePage.tsx +++ b/packages/client/src/page/differencePage.tsx @@ -26,7 +26,7 @@ export function DifferencePage() { Difference Scanned Files List @@ -45,8 +45,7 @@ export function DifferencePage() { diff --git a/packages/client/src/page/galleryPage.tsx b/packages/client/src/page/galleryPage.tsx index 326dd00..3d89e40 100644 --- a/packages/client/src/page/galleryPage.tsx +++ b/packages/client/src/page/galleryPage.tsx @@ -46,7 +46,7 @@ export default function Gallery() { } } }, [virtualItems, setSize, size, data]); - + if (isLoading) { return
Loading...
} @@ -66,47 +66,45 @@ export default function Gallery() { {(word || tags) &&
{word && Search: {word}} - {tags && Tags:
    {tags.split(",").map(x => )}
} + {tags && Tags:
    { + tags.split(",").map(x => )} +
}
} - { - data?.length === 0 &&
No results
- } + {data?.length === 0 &&
No results
}
- { - // TODO: date based grouping - virtualItems.map((item) => { - const isLoaderRow = item.index === size - 1 && isLoadingMore; - if (isLoaderRow) { - return
- -
; - } - const docs = data[item.index]; - if (!docs) return null; - return
- {docs.startCursor &&
-

Start with {docs.startCursor}

- -
} - {docs?.data?.map((x) => { - return ( - - ); - })} -
- }) - } + style={{ height: virtualizer.getTotalSize() }}> + {// TODO: date based grouping + virtualItems.map((item) => { + const isLoaderRow = item.index === size - 1 && isLoadingMore; + if (isLoaderRow) { + return
+ +
; + } + const docs = data[item.index]; + if (!docs) return null; + return
+ {docs.startCursor &&
+

Start with {docs.startCursor}

+ +
} + {docs?.data?.map((x) => { + return ( + + ); + })} +
+ }) + }
);