Skip to content

Commit

Permalink
feat: add useGetUserPage hook for retrieving users with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
anovazzi1 committed Jul 16, 2024
1 parent b7ef9c7 commit 51346b1
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { keepPreviousData } from "@tanstack/react-query";
import { Users, useQueryFunctionType } from "../../../../types/api";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface getUsersPageQueryParams {
skip: number;
limit: number;
}

export const useGetUserPage: useQueryFunctionType<
getUsersPageQueryParams,
Users
> = ({skip,limit}) => {
const { query } = UseRequestProcessor();

async function getUsersPage(): Promise<Array<Users>> {
const res = await api.get(
`${getURL("USERS")}/?skip=${skip}&limit=${limit}`,
);
if (res.status === 200) {
return res.data;
}
return [];
}

const queryResult = query(["useGetUserPage"], getUsersPage, {
placeholderData: keepPreviousData,
});

return queryResult;
};

0 comments on commit 51346b1

Please sign in to comment.