Skip to content

Commit

Permalink
Create queryKey factory (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
robines authored Nov 6, 2024
1 parent 0033466 commit 5612017
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { deleteInformationPage, getInformationPages } from '~/api';
import { useCustomNavigate, useTitle } from '~/hooks';
import { KEY } from '~/i18n/constants';
import { reverse } from '~/named-urls';
import { infoPageKeys } from '~/queryKeys';
import { ROUTES } from '~/routes';
import { dbT, lowerCapitalize } from '~/utils';
import { AdminPageLayout } from '../AdminPageLayout/AdminPageLayout';
Expand All @@ -18,7 +19,7 @@ export function InformationAdminPage() {

// TODO: add permissions on render
const { data, isLoading } = useQuery({
queryKey: ['informationpages'],
queryKey: infoPageKeys.all,
queryFn: getInformationPages,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MultiSelect } from '~/Components/MultiSelect';
import { createRole, editRole, getPermissions } from '~/api';
import type { RoleDto } from '~/dto';
import { KEY } from '~/i18n/constants';
import { permissionKeys } from '~/queryKeys';
import { ROLE_CONTENT_TYPE, ROLE_NAME } from '~/schema/role';
import styles from './RoleForm.module.scss';

Expand All @@ -47,7 +48,7 @@ export function RoleForm({ role }: Props) {
isLoading,
isError,
} = useQuery({
queryKey: ['permissions'],
queryKey: permissionKeys.all,
queryFn: getPermissions,
});

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/PagesAdmin/RolesAdminPage/RolesAdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getRoles } from '~/api';
import { useTitle } from '~/hooks';
import { KEY } from '~/i18n/constants';
import { reverse } from '~/named-urls';
import { roleKeys } from '~/queryKeys';
import { ROUTES } from '~/routes';
import { lowerCapitalize } from '~/utils';

Expand All @@ -19,7 +20,7 @@ export function RolesAdminPage() {
useTitle(title);

const { data: roles, isLoading } = useQuery({
queryKey: ['roles'],
queryKey: roleKeys.all,
queryFn: getRoles,
});

Expand Down
23 changes: 23 additions & 0 deletions frontend/src/queryKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const roleKeys = {
all: ['roles'] as const,
lists: () => [...roleKeys.all, 'list'] as const,
list: (filters: unknown[]) => [...roleKeys.lists(), { filters }] as const,
details: () => [...roleKeys.all, 'detail'] as const,
detail: (id: number) => [...roleKeys.details(), id] as const,
};

export const infoPageKeys = {
all: ['infopages'] as const,
lists: () => [...infoPageKeys.all, 'list'] as const,
list: (filters: unknown[]) => [...infoPageKeys.lists(), { filters }] as const,
details: () => [...infoPageKeys.all, 'detail'] as const,
detail: (slug: string) => [...infoPageKeys.details(), slug] as const,
};

export const permissionKeys = {
all: ['permissions'] as const,
lists: () => [...permissionKeys.all, 'list'] as const,
list: (filters: unknown[]) => [...permissionKeys.lists(), { filters }] as const,
details: () => [...permissionKeys.all, 'detail'] as const,
detail: (id: number) => [...permissionKeys.details(), id] as const,
};

0 comments on commit 5612017

Please sign in to comment.