From 3fbd22b250bcad5fae7597ccbc9cce99e8f2a659 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Mon, 10 Mar 2025 21:05:04 -0600 Subject: [PATCH] update dataset groups --- .../dataset_groups/queryRequests.ts | 122 ++++++++---------- .../buster_rest/dataset_groups/requests.ts | 42 ++++-- .../DatasetGroupDatasetSelectedPopup.tsx | 16 +-- .../DatasetGroupDatasetsListContainer.tsx | 8 +- .../users/DatasetGroupUsersListContainer.tsx | 7 +- .../users/DatasetGroupUsersSelectedPopup.tsx | 18 ++- 6 files changed, 109 insertions(+), 104 deletions(-) diff --git a/web/src/api/buster_rest/dataset_groups/queryRequests.ts b/web/src/api/buster_rest/dataset_groups/queryRequests.ts index 90a40ac61..840a4a352 100644 --- a/web/src/api/buster_rest/dataset_groups/queryRequests.ts +++ b/web/src/api/buster_rest/dataset_groups/queryRequests.ts @@ -36,34 +36,27 @@ export const useListDatasetGroups = () => { export const useDeleteDatasetGroup = () => { const queryClient = useQueryClient(); - const mutationFn = useMemoizedFn(async (id: string) => { - const res = await deleteDatasetGroup(id); - queryClient.invalidateQueries({ - queryKey: queryKeys.datasetGroupsList.queryKey, - exact: true - }); - - return res; - }); - return useMutation({ - mutationFn + mutationFn: deleteDatasetGroup, + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: queryKeys.datasetGroupsList.queryKey, + exact: true + }); + } }); }; export const useUpdateDatasetGroup = () => { const queryClient = useQueryClient(); - const mutationFn = useMemoizedFn(async (data: Parameters[0]) => { - const res = await updateDatasetGroup(data); - queryClient.invalidateQueries({ - queryKey: queryKeys.datasetGroupsList.queryKey, - exact: true - }); - return res; - }); - return useMutation({ - mutationFn + mutationFn: updateDatasetGroup, + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: queryKeys.datasetGroupsList.queryKey, + exact: true + }); + } }); }; @@ -195,39 +188,34 @@ export const prefetchDatasetGroupPermissionGroups = async ( return queryClient; }; -export const useUpdateDatasetGroupUsers = (datasetGroupId: string) => { +export const useUpdateDatasetGroupUsers = () => { const queryClient = useQueryClient(); - const mutationFn = useMemoizedFn((data: { id: string; assigned: boolean }[]) => { - queryClient.setQueryData( - 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) { - return { ...user, assigned: userToUpdate.assigned }; - } - return user; - }); - } - ); - return updateDatasetGroupUsers(datasetGroupId, data); - }); return useMutation({ - mutationFn + mutationFn: updateDatasetGroupUsers, + onMutate: ({ data, datasetGroupId }) => { + queryClient.setQueryData( + 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) { + return { ...user, assigned: userToUpdate.assigned }; + } + return user; + }); + } + ); + } }); }; export const useUpdateDatasetGroupDatasets = () => { const queryClient = useQueryClient(); - const mutationFn = useMemoizedFn( - ({ - datasetGroupId, - groups - }: { - datasetGroupId: string; - groups: { id: string; assigned: boolean }[]; - }) => { + + return useMutation({ + mutationFn: updateDatasetGroupDatasets, + onMutate: ({ groups, datasetGroupId }) => { queryClient.setQueryData( queryKeys.datasetGroupsGetDatasets(datasetGroupId).queryKey, (oldData: GetDatasetGroupDatasetsResponse[] | undefined) => { @@ -241,33 +229,29 @@ export const useUpdateDatasetGroupDatasets = () => { }); } ); - return updateDatasetGroupDatasets(datasetGroupId, groups); } - ); - return useMutation({ - mutationFn }); }; -export const useUpdateDatasetGroupPermissionGroups = (datasetGroupId: string) => { +export const useUpdateDatasetGroupPermissionGroups = () => { const queryClient = useQueryClient(); - const mutationFn = useMemoizedFn((data: { id: string; assigned: boolean }[]) => { - queryClient.setQueryData( - 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) { - return { ...permissionGroup, assigned: permissionGroupToUpdate.assigned }; - } - return permissionGroup; - }); - } - ); - return updateDatasetGroupPermissionGroups(datasetGroupId, data); - }); + return useMutation({ - mutationFn + mutationFn: updateDatasetGroupPermissionGroups, + onMutate: ({ data, datasetGroupId }) => { + queryClient.setQueryData( + 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) { + return { ...permissionGroup, assigned: permissionGroupToUpdate.assigned }; + } + return permissionGroup; + }); + } + ); + } }); }; diff --git a/web/src/api/buster_rest/dataset_groups/requests.ts b/web/src/api/buster_rest/dataset_groups/requests.ts index 45472f5a5..1c58b8ca8 100644 --- a/web/src/api/buster_rest/dataset_groups/requests.ts +++ b/web/src/api/buster_rest/dataset_groups/requests.ts @@ -27,8 +27,8 @@ export const updateDatasetGroup = async (data: UpdateDatasetGroupRequest[]) => { return mainApi.put(`/dataset_groups`, data).then((res) => res.data); }; -export const deleteDatasetGroup = async (id: string) => { - return mainApi.delete(`/dataset_groups/${id}`).then((res) => res.data); +export const deleteDatasetGroup = async (ids: string[]) => { + return mainApi.delete(`/dataset_groups`, { data: { ids } }).then((res) => res.data); }; export const getDatasetGroup = async (id: string) => { @@ -71,20 +71,34 @@ export const getDatasetGroupPermissionGroups_server = async (id: string) => { ); }; -export const updateDatasetGroupUsers = async (id: string, data: UpdateDatasetGroupUsersRequest) => { - return mainApi.put(`/dataset_groups/${id}/users`, data).then((res) => res.data); +export const updateDatasetGroupUsers = async ({ + datasetGroupId, + data +}: { + datasetGroupId: string; + data: UpdateDatasetGroupUsersRequest; +}) => { + return mainApi.put(`/dataset_groups/${datasetGroupId}/users`, data).then((res) => res.data); }; -export const updateDatasetGroupDatasets = async ( - id: string, - data: UpdateDatasetGroupDatasetsRequest -) => { - return mainApi.put(`/dataset_groups/${id}/datasets`, data).then((res) => res.data); +export const updateDatasetGroupDatasets = async ({ + datasetGroupId, + groups +}: { + datasetGroupId: string; + groups: UpdateDatasetGroupDatasetsRequest; +}) => { + return mainApi.put(`/dataset_groups/${datasetGroupId}/datasets`, groups).then((res) => res.data); }; -export const updateDatasetGroupPermissionGroups = async ( - id: string, - data: UpdateDatasetGroupPermissionGroupsRequest -) => { - return mainApi.put(`/dataset_groups/${id}/permission_groups`, data).then((res) => res.data); +export const updateDatasetGroupPermissionGroups = async ({ + datasetGroupId, + data +}: { + datasetGroupId: string; + data: UpdateDatasetGroupPermissionGroupsRequest; +}) => { + return mainApi + .put(`/dataset_groups/${datasetGroupId}/permission_groups`, data) + .then((res) => res.data); }; diff --git a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetSelectedPopup.tsx b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetSelectedPopup.tsx index 6478364da..7ec3aeb0b 100644 --- a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetSelectedPopup.tsx +++ b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetSelectedPopup.tsx @@ -1,8 +1,4 @@ -import { - updatePermissionGroupDatasetGroups, - useUpdateDatasetGroupPermissionGroups, - useUpdatePermissionGroupDatasetGroups -} from '@/api/buster_rest'; +import { useUpdateDatasetGroupPermissionGroups } from '@/api/buster_rest'; import { PermissionAssignedButton } from '@/components/features/PermissionComponents'; import { BusterListSelectedOptionPopupContainer } from '@/components/ui/list'; import { useMemoizedFn } from '@/hooks'; @@ -13,11 +9,13 @@ export const DatasetGroupDatasetGroupSelectedPopup: React.FC<{ onSelectChange: (selectedRowKeys: string[]) => void; datasetGroupId: string; }> = React.memo(({ selectedRowKeys, onSelectChange, datasetGroupId }) => { - const { mutateAsync: updateDatasetGroupDatasetGroups } = - useUpdateDatasetGroupPermissionGroups(datasetGroupId); + const { mutateAsync: updateDatasetGroupDatasetGroups } = useUpdateDatasetGroupPermissionGroups(); - const onSelectAssigned = useMemoizedFn(async (params: { id: string; assigned: boolean }[]) => { - await updateDatasetGroupDatasetGroups(params); + const onSelectAssigned = useMemoizedFn(async (data: { id: string; assigned: boolean }[]) => { + await updateDatasetGroupDatasetGroups({ + datasetGroupId, + data + }); }); return ( diff --git a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetsListContainer.tsx b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetsListContainer.tsx index ac7c36fed..5cea2ecc6 100644 --- a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetsListContainer.tsx +++ b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/permission-groups/DatasetGroupDatasetsListContainer.tsx @@ -23,11 +23,13 @@ export const DatasetGroupDatasetGroupsListContainer: React.FC<{ datasetGroupId: string; }> = React.memo(({ filteredDatasetGroups, datasetGroupId }) => { const [selectedRowKeys, setSelectedRowKeys] = useState([]); - const { mutateAsync: updateDatasetGroupDatasetGroups } = - useUpdateDatasetGroupPermissionGroups(datasetGroupId); + const { mutateAsync: updateDatasetGroupDatasetGroups } = useUpdateDatasetGroupPermissionGroups(); const onSelectAssigned = useMemoizedFn(async (params: { id: string; assigned: boolean }) => { - await updateDatasetGroupDatasetGroups([params]); + await updateDatasetGroupDatasetGroups({ + datasetGroupId, + data: [params] + }); }); const columns: BusterListColumn[] = useMemo( diff --git a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersListContainer.tsx b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersListContainer.tsx index 580e31c24..61b2f9bd7 100644 --- a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersListContainer.tsx +++ b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersListContainer.tsx @@ -24,10 +24,13 @@ export const DatasetGroupUsersListContainer: React.FC<{ datasetGroupId: string; }> = React.memo(({ filteredUsers, datasetGroupId }) => { const [selectedRowKeys, setSelectedRowKeys] = useState([]); - const { mutateAsync: updateDatasetGroupUsers } = useUpdateDatasetGroupUsers(datasetGroupId); + const { mutateAsync: updateDatasetGroupUsers } = useUpdateDatasetGroupUsers(); const onSelectAssigned = useMemoizedFn(async (params: { id: string; assigned: boolean }) => { - await updateDatasetGroupUsers([params]); + await updateDatasetGroupUsers({ + datasetGroupId, + data: [params] + }); }); const columns: BusterListColumn[] = useMemo( diff --git a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersSelectedPopup.tsx b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersSelectedPopup.tsx index 719f46601..215e479dd 100644 --- a/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersSelectedPopup.tsx +++ b/web/src/app/app/(settings_layout)/settings/(permissions)/dataset-groups/[datasetGroupId]/users/DatasetGroupUsersSelectedPopup.tsx @@ -1,10 +1,7 @@ -import { - useUpdateDatasetGroupUsers, - useUpdatePermissionGroupUsers, - useUpdateUserDatasets -} from '@/api/buster_rest'; +import { useUpdateDatasetGroupUsers } from '@/api/buster_rest'; import { PermissionAssignedButton } from '@/components/features/PermissionComponents'; import { BusterListSelectedOptionPopupContainer } from '@/components/ui/list'; +import { useMemoizedFn } from '@/hooks'; import React from 'react'; export const DatasetGroupUsersSelectedPopup: React.FC<{ @@ -12,7 +9,14 @@ export const DatasetGroupUsersSelectedPopup: React.FC<{ onSelectChange: (selectedRowKeys: string[]) => void; datasetGroupId: string; }> = React.memo(({ selectedRowKeys, onSelectChange, datasetGroupId }) => { - const { mutateAsync: updateDatasetGroupUsers } = useUpdateDatasetGroupUsers(datasetGroupId); + const { mutateAsync: updateDatasetGroupUsers } = useUpdateDatasetGroupUsers(); + + const onUpdate = useMemoizedFn(async (data: { id: string; assigned: boolean }[]) => { + await updateDatasetGroupUsers({ + datasetGroupId, + data + }); + }); return ( ]} />