Skip to content

Commit

Permalink
feat: org-list query hook
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshift committed May 14, 2024
1 parent 70b5db6 commit d4fc0e1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/orgs/components/org-list/use-org-list-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query';

import { QUERY_STALETIME } from '@/shared/core/constants';
import { MW_URL, PAGE_SIZE } from '@/shared/core/envs';
import { createUrlWithSearchParams } from '@/shared/utils/create-url-with-search-params';
import { mwGET } from '@/shared/utils/mw-get';

import { OrgQueryKeys, orgQueryKeys } from '@/orgs/core/query-keys';
import { OrgListQueryPage, orgListQueryPageSchema } from '@/orgs/core/schemas';
import { useFiltersContext } from '@/filters/providers/filters-provider/context';

const getOrgList = async (
page: number,
searchParams: string | Record<string, string>,
) => {
const url = createUrlWithSearchParams(
`${MW_URL}/organizations/list?page=${page}&limit=${PAGE_SIZE}`,
searchParams,
);

return mwGET({
url,
label: 'getOrgList',
responseSchema: orgListQueryPageSchema,
options: { next: { revalidate: 60 * 60 } },
});
};

export const useOrgListQuery = () => {
const { filterParamsString } = useFiltersContext();

return useInfiniteQuery<
OrgListQueryPage,
Error,
InfiniteData<OrgListQueryPage, number>,
ReturnType<OrgQueryKeys['list']>,
number
>({
queryKey: orgQueryKeys.list(filterParamsString),
queryFn: async ({ pageParam }) => getOrgList(pageParam, filterParamsString),
initialPageParam: 1,
getNextPageParam: ({ page, data }) =>
page > 0 && data.length > 0 ? page + 1 : undefined,
staleTime: QUERY_STALETIME.DEFAULT,
});
};
14 changes: 14 additions & 0 deletions src/orgs/core/query-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const orgQueryKeys = {
all: ['orgs'] as const,
details: (orgId: string) => [...orgQueryKeys.all, 'details', orgId] as const,
list: (params: string | Record<string, string>) => {
const searchParams =
typeof params === 'string'
? params
: new URLSearchParams(params).toString();

return [...orgQueryKeys.all, 'list', searchParams] as const;
},
};

export type OrgQueryKeys = typeof orgQueryKeys;
21 changes: 21 additions & 0 deletions src/shared/utils/create-url-with-search-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const createUrlWithSearchParams = (
url: string,
searchParams: string | Record<string, string>,
): string => {
if (typeof searchParams === 'string') {
if (!searchParams) return url;
const separator = url.includes('?') ? '&' : '?';
return `${url}${separator}${searchParams}`;
}

const urlObject = new URL(url);
const paramsEntries = Object.entries(searchParams);

if (paramsEntries.length > 0) {
paramsEntries.forEach(([key, value]) =>
urlObject.searchParams.set(key, value),
);
}

return urlObject.toString();
};

0 comments on commit d4fc0e1

Please sign in to comment.