move permission query keys to folder

This commit is contained in:
Nate Kelley 2025-02-17 21:15:44 -07:00
parent 51da69bf40
commit 4b959e7f3c
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
10 changed files with 187 additions and 77 deletions

View File

@ -18,7 +18,6 @@ import {
} from './requests';
import { QueryClient, useQueryClient } from '@tanstack/react-query';
import { useMemoizedFn } from 'ahooks';
import { LIST_DATASET_GROUPS_QUERY_KEY } from '../datasets/permissions/config';
import { USER_PERMISSIONS_DATASET_GROUPS_QUERY_KEY } from '../users/permissions/config';
import type {
GetDatasetGroupDatasetsResponse,
@ -26,11 +25,12 @@ import type {
GetDatasetGroupUsersResponse
} from '../../asset_interfaces';
import { timeout } from '@/utils';
import { queryKeys } from '@/api/query_keys';
export const useListDatasetGroups = () => {
const queryFn = useMemoizedFn(() => listDatasetGroups());
return useCreateReactQuery({
queryKey: ['dataset_groups'],
...queryKeys.datasetGroupsList,
queryFn
});
};
@ -39,7 +39,11 @@ export const useDeleteDatasetGroup = () => {
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn(async (id: string) => {
const res = await deleteDatasetGroup(id);
queryClient.invalidateQueries({ queryKey: ['dataset_groups'] });
queryClient.invalidateQueries({
queryKey: queryKeys.datasetGroupsList.queryKey,
exact: true
});
return res;
});
@ -52,7 +56,10 @@ export const useUpdateDatasetGroup = () => {
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn(async (data: Parameters<typeof updateDatasetGroup>[0]) => {
const res = await updateDatasetGroup(data);
queryClient.invalidateQueries({ queryKey: ['dataset_groups'] });
queryClient.invalidateQueries({
queryKey: queryKeys.datasetGroupsList.queryKey,
exact: true
});
return res;
});
@ -64,7 +71,7 @@ export const useUpdateDatasetGroup = () => {
export const useGetDatasetGroup = (datasetId: string) => {
const queryFn = useMemoizedFn(() => getDatasetGroup(datasetId));
return useCreateReactQuery({
queryKey: ['dataset_groups', datasetId],
...queryKeys.datasetGroupsGet(datasetId),
queryFn
});
};
@ -74,8 +81,9 @@ export const prefetchDatasetGroup = async (
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
const { queryKey } = queryKeys.datasetGroupsGet(datasetGroupId);
await queryClient.prefetchQuery({
queryKey: ['dataset_group', datasetGroupId],
queryKey,
queryFn: () => getDatasetGroup_server(datasetGroupId)
});
return queryClient;
@ -101,14 +109,19 @@ export const useCreateDatasetGroup = (datasetId?: string, userId?: string) => {
if (datasetId) {
await queryClient.invalidateQueries({
queryKey: [LIST_DATASET_GROUPS_QUERY_KEY, datasetId]
queryKey: [queryKeys.datasetGroupsList.queryKey, datasetId],
exact: true
});
}
await queryClient.invalidateQueries({ queryKey: ['dataset_groups'] });
await queryClient.invalidateQueries({
queryKey: queryKeys.datasetGroupsList.queryKey,
exact: true
});
if (userId) {
await queryClient.invalidateQueries({
queryKey: USER_PERMISSIONS_DATASET_GROUPS_QUERY_KEY(userId)
queryKey: USER_PERMISSIONS_DATASET_GROUPS_QUERY_KEY(userId),
exact: true
});
}
return newDatasetGroup;
@ -123,7 +136,7 @@ export const useCreateDatasetGroup = (datasetId?: string, userId?: string) => {
export const useGetDatasetGroupUsers = (datasetGroupId: string) => {
const queryFn = useMemoizedFn(() => getDatasetGroupUsers(datasetGroupId));
return useCreateReactQuery({
queryKey: ['dataset_groups', datasetGroupId, 'users'],
...queryKeys.datasetGroupsGetUsers(datasetGroupId),
queryFn
});
};
@ -133,8 +146,9 @@ export const prefetchDatasetGroupUsers = async (
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
const { queryKey } = queryKeys.datasetGroupsGetUsers(datasetGroupId);
await queryClient.prefetchQuery({
queryKey: ['dataset_groups', datasetGroupId, 'users'],
queryKey,
queryFn: () => getDatasetGroupUsers_server(datasetGroupId)
});
return queryClient;
@ -143,7 +157,7 @@ export const prefetchDatasetGroupUsers = async (
export const useGetDatasetGroupDatasets = (datasetGroupId: string) => {
const queryFn = useMemoizedFn(() => getDatasetGroupDatasets(datasetGroupId));
return useCreateReactQuery({
queryKey: ['dataset_groups', datasetGroupId, 'datasets'],
...queryKeys.datasetGroupsGetDatasets(datasetGroupId),
queryFn
});
};
@ -153,8 +167,9 @@ export const prefetchDatasetGroupDatasets = async (
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
const { queryKey } = queryKeys.datasetGroupsGetDatasets(datasetGroupId);
await queryClient.prefetchQuery({
queryKey: ['dataset_groups', datasetGroupId, 'datasets'],
queryKey,
queryFn: () => getDatasetGroupDatasets_server(datasetGroupId)
});
return queryClient;
@ -163,7 +178,7 @@ export const prefetchDatasetGroupDatasets = async (
export const useGetDatasetGroupPermissionGroups = (datasetGroupId: string) => {
const queryFn = useMemoizedFn(() => getDatasetGroupPermissionGroups(datasetGroupId));
return useCreateReactQuery({
queryKey: ['dataset_groups', datasetGroupId, 'permission_groups'],
...queryKeys.datasetGroupsGetPermissionGroups(datasetGroupId),
queryFn
});
};
@ -173,8 +188,9 @@ export const prefetchDatasetGroupPermissionGroups = async (
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
const { queryKey } = queryKeys.datasetGroupsGetPermissionGroups(datasetGroupId);
await queryClient.prefetchQuery({
queryKey: ['dataset_groups', datasetGroupId, 'permission_groups'],
queryKey,
queryFn: () => getDatasetGroupPermissionGroups_server(datasetGroupId)
});
return queryClient;
@ -184,8 +200,9 @@ export const useUpdateDatasetGroupUsers = (datasetGroupId: string) => {
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn((data: { id: string; assigned: boolean }[]) => {
queryClient.setQueryData(
['dataset_groups', datasetGroupId, 'users'],
(oldData: GetDatasetGroupUsersResponse[]) => {
queryKeys.datasetGroupsGetUsers(datasetGroupId).queryKey,
(oldData: GetDatasetGroupUsersResponse[] | undefined) => {
if (!oldData) return [];
return oldData.map((user) => {
const userToUpdate = data.find((d) => d.id === user.id);
if (userToUpdate) {
@ -213,9 +230,10 @@ export const useUpdateDatasetGroupDatasets = () => {
groups: { id: string; assigned: boolean }[];
}) => {
queryClient.setQueryData(
['dataset_groups', datasetGroupId, 'datasets'],
(oldData: GetDatasetGroupDatasetsResponse[]) => {
return oldData?.map((dataset) => {
queryKeys.datasetGroupsGetDatasets(datasetGroupId).queryKey,
(oldData: GetDatasetGroupDatasetsResponse[] | undefined) => {
if (!oldData) return [];
return oldData.map((dataset) => {
const datasetToUpdate = groups.find((d) => d.id === dataset.id);
if (datasetToUpdate) {
return { ...dataset, assigned: datasetToUpdate.assigned };
@ -236,8 +254,9 @@ export const useUpdateDatasetGroupPermissionGroups = (datasetGroupId: string) =>
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn((data: { id: string; assigned: boolean }[]) => {
queryClient.setQueryData(
['dataset_groups', datasetGroupId, 'permission_groups'],
(oldData: GetDatasetGroupPermissionGroupsResponse[]) => {
queryKeys.datasetGroupsGetPermissionGroups(datasetGroupId).queryKey,
(oldData: GetDatasetGroupPermissionGroupsResponse[] | undefined) => {
if (!oldData) return [];
return oldData.map((permissionGroup) => {
const permissionGroupToUpdate = data.find((d) => d.id === permissionGroup.id);
if (permissionGroupToUpdate) {

View File

@ -4,18 +4,26 @@ import type {
DatasetGroup,
GetDatasetGroupPermissionGroupsResponse,
GetDatasetGroupDatasetsResponse,
GetDatasetGroupUsersResponse
GetDatasetGroupUsersResponse,
ListDatasetGroupsResponse
} from '../../asset_interfaces';
import type {
CreateDatasetGroupRequest,
UpdateDatasetGroupRequest,
UpdateDatasetGroupUsersRequest,
UpdateDatasetGroupDatasetsRequest,
UpdateDatasetGroupPermissionGroupsRequest
} from '@/api/request_interfaces/dataset_groups';
export const listDatasetGroups = async () => {
return mainApi.get<DatasetGroup[]>(`/dataset_groups`).then((res) => res.data);
return mainApi.get<ListDatasetGroupsResponse[]>(`/dataset_groups`).then((res) => res.data);
};
export const createDatasetGroup = async (data: { name: string }) => {
export const createDatasetGroup = async (data: CreateDatasetGroupRequest) => {
return mainApi.post(`/dataset_groups`, data).then((res) => res.data);
};
export const updateDatasetGroup = async (data: { id: string; name: string }[]) => {
export const updateDatasetGroup = async (data: UpdateDatasetGroupRequest[]) => {
return mainApi.put(`/dataset_groups`, data).then((res) => res.data);
};
@ -24,7 +32,7 @@ export const deleteDatasetGroup = async (id: string) => {
};
export const getDatasetGroup = async (id: string) => {
return mainApi.get(`/dataset_groups/${id}`).then((res) => res.data);
return mainApi.get<DatasetGroup>(`/dataset_groups/${id}`).then((res) => res.data);
};
export const getDatasetGroup_server = async (id: string) => {
@ -63,23 +71,20 @@ export const getDatasetGroupPermissionGroups_server = async (id: string) => {
);
};
export const updateDatasetGroupUsers = async (
id: string,
data: { id: string; assigned: boolean }[]
) => {
export const updateDatasetGroupUsers = async (id: string, data: UpdateDatasetGroupUsersRequest) => {
return mainApi.put(`/dataset_groups/${id}/users`, data).then((res) => res.data);
};
export const updateDatasetGroupDatasets = async (
id: string,
data: { id: string; assigned: boolean }[]
data: UpdateDatasetGroupDatasetsRequest
) => {
return mainApi.put(`/dataset_groups/${id}/datasets`, data).then((res) => res.data);
};
export const updateDatasetGroupPermissionGroups = async (
id: string,
data: { id: string; assigned: boolean }[]
data: UpdateDatasetGroupPermissionGroupsRequest
) => {
return mainApi.put(`/dataset_groups/${id}/permission_groups`, data).then((res) => res.data);
};

View File

@ -1,4 +0,0 @@
export const GET_PERMISSIONS_OVERVIEW = (datasetId: string) => `/datasets/${datasetId}/overview`;
//QUERY KEYS
export const LIST_DATASET_GROUPS_QUERY_KEY = 'list_dataset_groups';

View File

@ -15,9 +15,7 @@ import {
} from './requests';
import { useMemoizedFn } from 'ahooks';
import { QueryClient, useQueryClient } from '@tanstack/react-query';
import type { ListPermissionUsersResponse } from '../../../asset_interfaces';
import { PERMISSION_GROUP_QUERY_KEY } from '../../permission_groups';
import { LIST_DATASET_GROUPS_QUERY_KEY } from './config';
import { queryKeys } from '@/api/query_keys';
export const useGetDatasetPermissionsOverview = (dataset_id: string) => {
const queryFn = useMemoizedFn(() => {
@ -25,7 +23,7 @@ export const useGetDatasetPermissionsOverview = (dataset_id: string) => {
});
return useCreateReactQuery({
queryKey: ['dataset_permissions_overview', dataset_id],
...queryKeys.datasetPermissionsOverview(dataset_id),
queryFn,
staleTime: PREFETCH_STALE_TIME
});
@ -37,7 +35,7 @@ export const prefetchGetDatasetPermissionsOverview = async (
) => {
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
queryKey: ['dataset_permissions_overview', datasetId],
...queryKeys.datasetPermissionsOverview(datasetId),
queryFn: () => getDatasetPermissionsOverview_server(datasetId)
});
return queryClient;
@ -49,7 +47,7 @@ export const useDatasetListPermissionGroups = (dataset_id: string) => {
});
return useCreateReactQuery({
queryKey: [PERMISSION_GROUP_QUERY_KEY, dataset_id],
...queryKeys.datasetPermissionGroupsList(dataset_id),
queryFn,
enabled: !!dataset_id
});
@ -63,13 +61,15 @@ export const useDatasetUpdatePermissionGroups = (dataset_id: string) => {
keyedChanges[id] = { id, assigned };
});
queryClient.setQueryData(
[PERMISSION_GROUP_QUERY_KEY, dataset_id],
(oldData: ListPermissionUsersResponse[]) => {
return oldData?.map((group) => {
const updatedGroup = keyedChanges[group.id];
if (updatedGroup) return { ...group, assigned: updatedGroup.assigned };
return group;
});
queryKeys.datasetPermissionUsersList(dataset_id).queryKey,
(oldData) => {
return (
oldData?.map((group) => {
const updatedGroup = keyedChanges[group.id];
if (updatedGroup) return { ...group, assigned: updatedGroup.assigned };
return group;
}) || []
);
}
);
@ -85,7 +85,7 @@ export const useDatasetListDatasetGroups = (dataset_id: string) => {
const queryFn = useMemoizedFn(() => listDatasetDatasetGroups({ dataset_id }));
return useCreateReactQuery({
queryKey: [LIST_DATASET_GROUPS_QUERY_KEY, dataset_id],
...queryKeys.datasetGroupsList,
queryFn,
enabled: !!dataset_id
});
@ -95,7 +95,7 @@ export const useDatasetListPermissionUsers = (dataset_id: string) => {
const queryFn = useMemoizedFn(() => listDatasetPermissionUsers({ dataset_id }));
return useCreateReactQuery({
queryKey: ['list_permission_users', dataset_id],
...queryKeys.datasetPermissionUsersList(dataset_id),
queryFn,
enabled: !!dataset_id
});
@ -109,13 +109,15 @@ export const useDatasetUpdateDatasetGroups = (dataset_id: string) => {
keyedChanges[id] = { id, assigned };
});
queryClient.setQueryData(
[LIST_DATASET_GROUPS_QUERY_KEY, dataset_id],
(oldData: ListPermissionUsersResponse[]) => {
return oldData?.map((group) => {
const updatedGroup = keyedChanges[group.id];
if (updatedGroup) return { ...group, assigned: updatedGroup.assigned };
return group;
});
queryKeys.datasetPermissionGroupsList(dataset_id).queryKey,
(oldData) => {
return (
oldData?.map((group) => {
const updatedGroup = keyedChanges[group.id];
if (updatedGroup) return { ...group, assigned: updatedGroup.assigned };
return group;
}) || []
);
}
);
return updateDatasetDatasetGroups({ dataset_id, groups });
@ -134,13 +136,15 @@ export const useDatasetUpdatePermissionUsers = (dataset_id: string) => {
keyedChanges[id] = { id, assigned };
});
queryClient.setQueryData(
['list_permission_users', dataset_id],
(oldData: ListPermissionUsersResponse[]) => {
return oldData?.map((user) => {
const updatedUser = keyedChanges[user.id];
if (updatedUser) return { ...user, assigned: updatedUser.assigned };
return user;
});
queryKeys.datasetPermissionUsersList(dataset_id).queryKey,
(oldData) => {
return (
oldData?.map((user) => {
const updatedUser = keyedChanges[user.id];
if (updatedUser) return { ...user, assigned: updatedUser.assigned };
return user;
}) || []
);
}
);
return updateDatasetPermissionUsers({ dataset_id, users });

View File

@ -5,7 +5,6 @@ import type {
ListPermissionGroupsResponse,
ListPermissionUsersResponse
} from '../../../asset_interfaces';
import * as config from './config';
import { serverFetch } from '../../../createServerInstance';
export const listIndividualDatasetPermissionGroups = async ({
@ -16,12 +15,10 @@ export const listIndividualDatasetPermissionGroups = async ({
return await mainApi.get(`/datasets/${dataset_id}/permission_groups`).then((res) => res.data);
};
export const listDatasetDatasetGroups = async ({
dataset_id
}: {
dataset_id: string;
}): Promise<ListDatasetGroupsResponse[]> => {
return await mainApi.get(`/datasets/${dataset_id}/dataset_groups`).then((res) => res.data);
export const listDatasetDatasetGroups = async ({ dataset_id }: { dataset_id: string }) => {
return await mainApi
.get<ListDatasetGroupsResponse[]>(`/datasets/${dataset_id}/dataset_groups`)
.then((res) => res.data);
};
export const listDatasetPermissionUsers = async ({
@ -70,17 +67,19 @@ export const updateDatasetDatasetGroups = async ({
return await mainApi.put(`/datasets/${dataset_id}/dataset_groups`, groups);
};
const GET_PERMISSIONS_OVERVIEW = (datasetId: string) => `/datasets/${datasetId}/overview`;
export const getDatasetPermissionsOverview = async ({
dataset_id
}: {
dataset_id: string;
}): Promise<DatasetPermissionsOverviewResponse> => {
return await mainApi.get(config.GET_PERMISSIONS_OVERVIEW(dataset_id)).then((res) => res.data);
return await mainApi.get(GET_PERMISSIONS_OVERVIEW(dataset_id)).then((res) => res.data);
};
export const getDatasetPermissionsOverview_server = async (datasetId: string) => {
const response = await serverFetch<DatasetPermissionsOverviewResponse>(
config.GET_PERMISSIONS_OVERVIEW(datasetId)
GET_PERMISSIONS_OVERVIEW(datasetId)
);
return response;
};

View File

@ -1 +0,0 @@
export const PERMISSION_GROUP_QUERY_KEY = 'permission_groups';

View File

@ -0,0 +1,68 @@
import { queryOptions } from '@tanstack/react-query';
import type {
DatasetGroup,
DatasetPermissionsOverviewResponse,
GetDatasetGroupDatasetsResponse,
GetDatasetGroupPermissionGroupsResponse,
GetDatasetGroupUsersResponse,
ListDatasetGroupsResponse,
ListPermissionUsersResponse
} from '@/api/asset_interfaces';
const datasetGroupsList = queryOptions<ListDatasetGroupsResponse[]>({
queryKey: ['dataset_groups'] as const,
staleTime: 10 * 1000
});
const datasetGroupsGet = (datasetGroupId: string) =>
queryOptions<DatasetGroup>({
queryKey: ['dataset_groups', datasetGroupId] as const,
staleTime: 10 * 1000
});
const datasetGroupsGetUsers = (datasetGroupId: string) =>
queryOptions<GetDatasetGroupUsersResponse[]>({
queryKey: ['dataset_groups', datasetGroupId, 'users'] as const,
staleTime: 10 * 1000
});
const datasetGroupsGetDatasets = (datasetGroupId: string) =>
queryOptions<GetDatasetGroupDatasetsResponse[]>({
queryKey: ['dataset_groups', datasetGroupId, 'datasets'] as const,
staleTime: 10 * 1000
});
const datasetGroupsGetPermissionGroups = (datasetGroupId: string) =>
queryOptions<GetDatasetGroupPermissionGroupsResponse[]>({
queryKey: ['dataset_groups', datasetGroupId, 'permission_groups'] as const,
staleTime: 10 * 1000
});
const datasetPermissionsOverview = (datasetId: string) =>
queryOptions<DatasetPermissionsOverviewResponse>({
queryKey: ['dataset_permissions_overview', datasetId] as const,
staleTime: 10 * 1000
});
const datasetPermissionGroupsList = (datasetId: string) =>
queryOptions<ListDatasetGroupsResponse[]>({
queryKey: ['dataset_permission_groups_list', datasetId] as const,
staleTime: 10 * 1000
});
const datasetPermissionUsersList = (datasetId: string) =>
queryOptions<ListPermissionUsersResponse[]>({
queryKey: ['dataset_permission_users_list', datasetId] as const,
staleTime: 10 * 1000
});
export const datasetGroupQueryKeys = {
datasetGroupsList,
datasetGroupsGet,
datasetGroupsGetUsers,
datasetGroupsGetDatasets,
datasetGroupsGetPermissionGroups,
datasetPermissionsOverview,
datasetPermissionGroupsList,
datasetPermissionUsersList
};

View File

@ -6,6 +6,7 @@ import { metricsQueryKeys } from './metric';
import { searchQueryKeys } from './search';
import { termsQueryKeys } from './terms';
import { datasourceQueryKeys } from './datasources';
import { datasetGroupQueryKeys } from './dataset_groups';
export const queryKeys = {
...chatQueryKeys,
@ -15,5 +16,6 @@ export const queryKeys = {
...metricsQueryKeys,
...searchQueryKeys,
...termsQueryKeys,
...datasourceQueryKeys
...datasourceQueryKeys,
...datasetGroupQueryKeys
};

View File

@ -0,0 +1 @@
export * from './interfaces';

View File

@ -0,0 +1,17 @@
export interface CreateDatasetGroupRequest {
name: string;
}
export interface UpdateDatasetGroupRequest {
id: string;
name: string;
}
export interface UpdateDatasetGroupAssignmentRequest {
id: string;
assigned: boolean;
}
export type UpdateDatasetGroupUsersRequest = UpdateDatasetGroupAssignmentRequest[];
export type UpdateDatasetGroupDatasetsRequest = UpdateDatasetGroupAssignmentRequest[];
export type UpdateDatasetGroupPermissionGroupsRequest = UpdateDatasetGroupAssignmentRequest[];