Skip to content

chore(site): remove template ACL XService #10332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract update user role
  • Loading branch information
BrunoQuaresma committed Oct 18, 2023
commit 82fba2fc90f20e8a4c765194f6a7f707879c0da1
2 changes: 1 addition & 1 deletion site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const templateACL = (templateId: string) => {
};
};

export const addUserToTemplateACL = (
export const setUserRole = (
queryClient: QueryClient,
): MutationOptions<
Awaited<ReturnType<typeof API.updateTemplateACL>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { useTemplateSettings } from "../TemplateSettingsLayout";
import { TemplatePermissionsPageView } from "./TemplatePermissionsPageView";
import { docs } from "utils/docs";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { addUserToTemplateACL, templateACL } from "api/queries/templates";
import { setUserRole, templateACL } from "api/queries/templates";
import { displaySuccess } from "components/GlobalSnackbar/utils";

export const TemplatePermissionsPage: FC<
React.PropsWithChildren<unknown>
Expand All @@ -25,15 +26,16 @@ export const TemplatePermissionsPage: FC<
const [state, send] = useMachine(templateACLMachine, {
context: { templateId: template.id },
});
const { userToBeUpdated, groupToBeUpdated } = state.context;
const { groupToBeUpdated } = state.context;
const templateACLQuery = useQuery({
...templateACL(template.id),
onSuccess: (data) => {
send({ type: "LOAD", data });
},
});
const queryClient = useQueryClient();
const addUserMutation = useMutation(addUserToTemplateACL(queryClient));
const addUserMutation = useMutation(setUserRole(queryClient));
const updateUserMutation = useMutation(setUserRole(queryClient));

return (
<>
Expand Down Expand Up @@ -79,10 +81,19 @@ export const TemplatePermissionsPage: FC<
reset();
}}
isAddingUser={addUserMutation.isLoading}
onUpdateUser={(user, role) => {
send("UPDATE_USER_ROLE", { user, role });
onUpdateUser={async (user, role) => {
await updateUserMutation.mutateAsync({
templateId: template.id,
userId: user.id,
role,
});
displaySuccess("User role updated successfully!");
}}
updatingUser={userToBeUpdated}
updatingUserId={
updateUserMutation.isLoading
? updateUserMutation.variables?.userId
: undefined
}
onRemoveUser={(user) => {
send("REMOVE_USER", { user });
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export interface TemplatePermissionsPageViewProps {
) => void;
isAddingUser: boolean;
onUpdateUser: (user: TemplateUser, role: TemplateRole) => void;
updatingUser: TemplateUser | undefined;
updatingUserId: TemplateUser["id"] | undefined;
onRemoveUser: (user: TemplateUser) => void;
// Group
onAddGroup: (
Expand All @@ -185,7 +185,7 @@ export const TemplatePermissionsPageView: FC<
// User
onAddUser,
isAddingUser,
updatingUser,
updatingUserId,
onUpdateUser,
onRemoveUser,
// Group
Expand Down Expand Up @@ -313,9 +313,7 @@ export const TemplatePermissionsPageView: FC<
<Cond condition={canUpdatePermissions}>
<RoleSelect
value={user.role}
disabled={
updatingUser && updatingUser.id === user.id
}
disabled={updatingUserId === user.id}
onChange={(event) => {
onUpdateUser(
user,
Expand Down
61 changes: 0 additions & 61 deletions site/src/xServices/template/templateACLXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export const templateACLMachine = createMachine(
context: {} as {
templateId: string;
templateACL?: TemplateACL;
// User
userToBeUpdated?: TemplateUser;
addUserCallback?: () => void;
// Group
groupToBeAdded?: TemplateGroup;
groupToBeUpdated?: TemplateGroup;
Expand All @@ -27,9 +24,6 @@ export const templateACLMachine = createMachine(
data: TemplateACL;
};
// User
updateUser: {
data: unknown;
};
// Group
addGroup: {
data: unknown;
Expand All @@ -43,11 +37,6 @@ export const templateACLMachine = createMachine(
type: "LOAD";
data: TemplateACL;
}
| {
type: "UPDATE_USER_ROLE";
user: TemplateUser;
role: TemplateRole;
}
| {
type: "REMOVE_USER";
user: TemplateUser;
Expand Down Expand Up @@ -84,10 +73,6 @@ export const templateACLMachine = createMachine(
idle: {
on: {
// User
UPDATE_USER_ROLE: {
target: "updatingUser",
actions: ["assignUserToBeUpdated"],
},
REMOVE_USER: {
target: "removingUser",
actions: ["removeUserFromTemplateACL"],
Expand All @@ -108,19 +93,6 @@ export const templateACLMachine = createMachine(
},
},
// User
updatingUser: {
invoke: {
src: "updateUser",
onDone: {
target: "idle",
actions: [
"updateUserOnTemplateACL",
"clearUserToBeUpdated",
"displayUpdateUserSuccessMessage",
],
},
},
},
removingUser: {
invoke: {
src: "removeUser",
Expand Down Expand Up @@ -167,12 +139,6 @@ export const templateACLMachine = createMachine(
{
services: {
// User
updateUser: ({ templateId }, { user, role }) =>
updateTemplateACL(templateId, {
user_perms: {
[user.id]: role,
},
}),
removeUser: ({ templateId }, { user }) =>
updateTemplateACL(templateId, {
user_perms: {
Expand Down Expand Up @@ -204,33 +170,6 @@ export const templateACLMachine = createMachine(
templateACL: (_, { data }) => data,
}),
// User
assignUserToBeUpdated: assign({
userToBeUpdated: (_, { user, role }) => ({ ...user, role }),
}),
updateUserOnTemplateACL: assign({
templateACL: ({ templateACL, userToBeUpdated }) => {
if (!userToBeUpdated) {
throw new Error("No user to be added");
}
if (!templateACL) {
throw new Error("Template ACL is not loaded yet");
}
return {
...templateACL,
users: templateACL.users.map((oldTemplateUser) => {
return oldTemplateUser.id === userToBeUpdated.id
? userToBeUpdated
: oldTemplateUser;
}),
};
},
}),
clearUserToBeUpdated: assign({
userToBeUpdated: (_) => undefined,
}),
displayUpdateUserSuccessMessage: () => {
displaySuccess("User role update successfully!");
},
removeUserFromTemplateACL: assign({
templateACL: ({ templateACL }, { user }) => {
if (!templateACL) {
Expand Down