Skip to content

Commit 46974f1

Browse files
committed
refactor: create Query Keys factories
1 parent fc62f15 commit 46974f1

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/authz-module/data/hooks.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { useQuery, useSuspenseQuery } from '@tanstack/react-query';
2+
import { appId } from "@src/constants";
23
import { LibraryMetadata, TeamMember } from '@src/types';
34
import { getLibrary, getTeamMembers } from './api';
45

6+
7+
const authzQueryKeys = {
8+
all: [appId, 'authz'] as const,
9+
teamMembers: (object: string) => [...authzQueryKeys.all, 'teamMembers', object] as const,
10+
library: (libraryId: string) => [...authzQueryKeys.all, 'library', libraryId] as const,
11+
};
12+
513
/**
614
* React Query hook to fetch all team members for a specific object/scope.
715
* It retrieves the full list of members who have access to the given scope.
@@ -14,7 +22,7 @@ import { getLibrary, getTeamMembers } from './api';
1422
* ```
1523
*/
1624
export const useTeamMembers = (object: string) => useQuery<TeamMember[], Error>({
17-
queryKey: ['team-members', object],
25+
queryKey: authzQueryKeys.teamMembers(object),
1826
queryFn: () => getTeamMembers(object),
1927
staleTime: 1000 * 60 * 30, // refetch after 30 minutes
2028
});
@@ -30,7 +38,7 @@ export const useTeamMembers = (object: string) => useQuery<TeamMember[], Error>(
3038
*/
3139
export const useLibrary = (libraryId: string) => {
3240
return useSuspenseQuery<LibraryMetadata, Error>({
33-
queryKey: ['library-metadata', libraryId],
41+
queryKey: authzQueryKeys.library(libraryId),
3442
queryFn: () => getLibrary(libraryId),
3543
retry: false,
3644
});

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const appId = 'org.openedx.frontend.app.adminConsole';

src/data/hooks.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useSuspenseQuery } from '@tanstack/react-query';
22
import { PermissionValidationRequest, PermissionValidationResponse } from '@src/types';
33
import { validateUserPermissions } from './api';
4+
import { appId } from '@src/constants';
5+
6+
const adminConsoleQueryKeys = {
7+
all: [appId] as const,
8+
permissions: (permissions: PermissionValidationRequest) => [...adminConsoleQueryKeys.all, 'validatePermissions', permissions] as const,
9+
};
410

511
/**
612
* React Query hook to validate if the current user has permissions over a certain object in the instance.
@@ -11,15 +17,15 @@ import { validateUserPermissions } from './api';
1117
* @param permissions - The array of objects and actions to validate.
1218
*
1319
* @example
14-
* const { data } = userValidateUserPermissions([{
20+
* const { data } = useValidateUserPermissions([{
1521
"action": "act:read",
1622
"object": "lib:test-lib",
1723
"scope": "org:OpenedX"
1824
}]);
1925
* if (data[0].allowed) { ... }
2026
*
2127
*/
22-
export const useValidateUserPermissions = (permissions: PermissionValidationRequest[]) => {
28+
export const useValidateUserPermissions = (permissions: PermissionValidationRequest[]) => {
2329
return useSuspenseQuery<PermissionValidationResponse[], Error>({
2430
queryKey: ['validate-user-permissions', permissions],
2531
queryFn: () => validateUserPermissions(permissions),

0 commit comments

Comments
 (0)