From d7f83ebc4a41ea6c5336205f46ed19a7af80aa4f Mon Sep 17 00:00:00 2001 From: robines Date: Thu, 31 Oct 2024 13:29:58 +0100 Subject: [PATCH] Create queryKey factory --- .../InformationAdminPage.tsx | 3 ++- .../components/RoleForm/RoleForm.tsx | 3 ++- .../RolesAdminPage/RolesAdminPage.tsx | 3 ++- frontend/src/queryKeys.ts | 23 +++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 frontend/src/queryKeys.ts diff --git a/frontend/src/PagesAdmin/InformationAdminPage/InformationAdminPage.tsx b/frontend/src/PagesAdmin/InformationAdminPage/InformationAdminPage.tsx index 63da9f401..b2640fb00 100644 --- a/frontend/src/PagesAdmin/InformationAdminPage/InformationAdminPage.tsx +++ b/frontend/src/PagesAdmin/InformationAdminPage/InformationAdminPage.tsx @@ -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'; @@ -18,7 +19,7 @@ export function InformationAdminPage() { // TODO: add permissions on render const { data, isLoading } = useQuery({ - queryKey: ['informationpages'], + queryKey: infoPageKeys.all, queryFn: getInformationPages, }); diff --git a/frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/RoleForm.tsx b/frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/RoleForm.tsx index c9322116b..d1de3c3c3 100644 --- a/frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/RoleForm.tsx +++ b/frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/RoleForm.tsx @@ -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'; @@ -47,7 +48,7 @@ export function RoleForm({ role }: Props) { isLoading, isError, } = useQuery({ - queryKey: ['permissions'], + queryKey: permissionKeys.all, queryFn: getPermissions, }); diff --git a/frontend/src/PagesAdmin/RolesAdminPage/RolesAdminPage.tsx b/frontend/src/PagesAdmin/RolesAdminPage/RolesAdminPage.tsx index 5b253c7da..620089f0d 100644 --- a/frontend/src/PagesAdmin/RolesAdminPage/RolesAdminPage.tsx +++ b/frontend/src/PagesAdmin/RolesAdminPage/RolesAdminPage.tsx @@ -8,6 +8,7 @@ import { AdminPageLayout } from '~/PagesAdmin/AdminPageLayout/AdminPageLayout'; import { getRoles } from '~/api'; import { KEY } from '~/i18n/constants'; import { reverse } from '~/named-urls'; +import { roleKeys } from '~/queryKeys'; import { ROUTES } from '~/routes'; import { lowerCapitalize } from '~/utils'; @@ -17,7 +18,7 @@ export function RolesAdminPage() { const navigate = useNavigate(); const { data: roles, isLoading } = useQuery({ - queryKey: ['roles'], + queryKey: roleKeys.all, queryFn: getRoles, }); diff --git a/frontend/src/queryKeys.ts b/frontend/src/queryKeys.ts new file mode 100644 index 000000000..50e009dee --- /dev/null +++ b/frontend/src/queryKeys.ts @@ -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, +};