Skip to content

Commit

Permalink
fix: better handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Pomar committed Jun 26, 2024
1 parent f38f07d commit b33ad12
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/useSWRPaginated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ test("should use counter", () => {
useSWRPaginated<{ id: string }>(generateGetKey("/demo?q=124"))
);
result.current.data;
expect(result.current.data).toBe(undefined);
expect(result.current.items).toBe([]);
expect(result.current.data).toStrictEqual([undefined]);
expect(result.current.items).toStrictEqual([]);

expect(typeof result.current.setSize).toBe("function");
});
Expand All @@ -18,8 +18,8 @@ test("should use counter", () => {
useSWRPaginated(a ? generateGetKey("/demo?q=124") : null)
);

expect(result.current.data).toBe(undefined);
expect(result.current.items).toBe([]);
expect(result.current.data).toStrictEqual([undefined]);
expect(result.current.items).toStrictEqual([]);

expect(typeof result.current.setSize).toBe("function");
});
Expand All @@ -35,8 +35,8 @@ test("should use counter", () => {
)
);

expect(result.current.data).toBe(undefined);
expect(result.current.items).toBe([]);
expect(result.current.data).toStrictEqual([undefined]);
expect(result.current.items).toStrictEqual([]);

expect(typeof result.current.setSize).toBe("function");
});
37 changes: 26 additions & 11 deletions src/useSWRPaginated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BareFetcher, Middleware, SWRHook } from "swr";
import useSWRInfinite, {
SWRInfiniteConfiguration,
SWRInfiniteKeyLoader,
SWRInfiniteResponse,
} from "swr/infinite";

interface PaginationParams {
Expand Down Expand Up @@ -38,26 +39,40 @@ export const generateGetKey = <T>(
};
};

type SWRPaginatedResponse<T> = SWRInfiniteResponse<T> & {
items: T[];
isEmpty: boolean;
isReachingEnd: boolean;
};

export function useSWRPaginated<T, P extends Page<T> = Page<T>>(
getKey: SWRInfiniteKeyLoader<P>,
config?: SWRInfiniteConfiguration<P, Error, BareFetcher<P>>
) {
): SWRPaginatedResponse<P> {
const swr = useSWRInfinite<P>(getKey, config);

return Object.assign(swr, {
get isEmpty() {
return swr.data?.[0]?.results?.length === 0;
Object.defineProperty(swr, "items", {
get: function () {
return this.data?.map((p) => p?.results).flat();
},
});

Object.defineProperty(swr, "isEmpty", {
get: function () {
return this.data?.[0]?.results?.length === 0;
},
get isReachingEnd() {
const empty = swr.data?.[0]?.results?.length === 0;
});

Object.defineProperty(swr, "isReachingEnd", {
get: function () {
const empty = this.data?.[0]?.results?.length === 0;
return empty
? true
: swr.data
? swr.data[swr.data?.length - 1]?.next == null
: this.data
? this.data[this.data?.length - 1]?.next == null
: false;
},
get items() {
return swr.data?.map((p) => p?.results).flat();
},
});

return swr as any;
}

0 comments on commit b33ad12

Please sign in to comment.