Add endpoint to update MI project-level roles

This commit is contained in:
Tuan Dang
2023-11-24 19:50:01 +07:00
parent 595a26a366
commit 180274be34
5 changed files with 127 additions and 20 deletions
+2 -2
View File
@@ -20,6 +20,6 @@ export {
useRenameWorkspace,
useReorderWsEnvironment,
useToggleAutoCapitalization,
useUpdateMachineWorkspaceRole,
useUpdateUserWorkspaceRole,
useUpdateWsEnvironment,
} from "./queries";
useUpdateWsEnvironment} from "./queries";
+31 -4
View File
@@ -31,7 +31,7 @@ export const workspaceKeys = {
getAllUserWorkspace: ["workspaces"] as const,
getWorkspaceAuditLogs: (workspaceId: string) => [{ workspaceId }] as const,
getWorkspaceUsers: (workspaceId: string) => [{ workspaceId }] as const,
getWorkspaceServiceMemberships: (workspaceId: string) => [{ workspaceId }, "organization-service-memberships"] as const
getWorkspaceMachineMemberships: (workspaceId: string) => [{ workspaceId }, "workspace-machine-memberships"] as const
};
const fetchWorkspaceById = async (workspaceId: string) => {
@@ -378,7 +378,34 @@ export const useAddMachineToWorkspace = () => {
return serviceMembership;
},
onSuccess: (_, { workspaceId }) => {
queryClient.invalidateQueries(workspaceKeys.getWorkspaceServiceMemberships(workspaceId));
queryClient.invalidateQueries(workspaceKeys.getWorkspaceMachineMemberships(workspaceId));
}
});
};
export const useUpdateMachineWorkspaceRole = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({
machineId,
workspaceId,
role
}: {
machineId: string;
workspaceId: string;
role?: string;
}) => {
const {
data: { serviceMembership }
} = await apiRequest.patch(`/api/v2/workspace/${workspaceId}/machine-memberships/${machineId}`, {
role
});
return serviceMembership;
},
onSuccess: (_, { workspaceId }) => {
queryClient.invalidateQueries(workspaceKeys.getWorkspaceMachineMemberships(workspaceId));
}
});
};
@@ -401,7 +428,7 @@ export const useDeleteMachineFromWorkspace = () => {
return serviceMembership;
},
onSuccess: (_, { workspaceId }) => {
queryClient.invalidateQueries(workspaceKeys.getWorkspaceServiceMemberships(workspaceId));
queryClient.invalidateQueries(workspaceKeys.getWorkspaceMachineMemberships(workspaceId));
}
});
};
@@ -409,7 +436,7 @@ export const useDeleteMachineFromWorkspace = () => {
export const useGetWorkspaceMachineMemberships = (workspaceId: string) => {
return useQuery({
queryKey: workspaceKeys.getWorkspaceServiceMemberships(workspaceId),
queryKey: workspaceKeys.getWorkspaceMachineMemberships(workspaceId),
queryFn: async () => {
const {
@@ -27,7 +27,8 @@ import {
} from "@app/context";
import {
useGetRoles,
useGetWorkspaceMachineMemberships
useGetWorkspaceMachineMemberships,
useUpdateMachineWorkspaceRole
} from "@app/hooks/api";
import { MachineTrustedIp} from "@app/hooks/api/machineIdentities/types";
import { UsePopUpState } from "@app/hooks/usePopUp";
@@ -66,6 +67,8 @@ export const MachineIdentityTable = ({
workspaceId
});
const { mutateAsync: updateMutateAsync } = useUpdateMachineWorkspaceRole();
const handleChangeRole = async ({
machineId,
role
@@ -73,21 +76,14 @@ export const MachineIdentityTable = ({
machineId: string;
role: string;
}) => {
try {
console.log("handle project-level role change vals: ", {
await updateMutateAsync({
machineId,
workspaceId,
role
});
// TODO: change role
// await updateMutateAsync({
// serviceTokenDataId,
// role
// });
createNotification({
text: "Successfully updated machine identity role",
type: "success"