Skip to content

Commit 82fba2f

Browse files
committed
Extract update user role
1 parent f424a70 commit 82fba2f

File tree

4 files changed

+21
-73
lines changed

4 files changed

+21
-73
lines changed

site/src/api/queries/templates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const templateACL = (templateId: string) => {
4848
};
4949
};
5050

51-
export const addUserToTemplateACL = (
51+
export const setUserRole = (
5252
queryClient: QueryClient,
5353
): MutationOptions<
5454
Awaited<ReturnType<typeof API.updateTemplateACL>>,

site/src/pages/TemplateSettingsPage/TemplatePermissionsPage/TemplatePermissionsPage.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { useTemplateSettings } from "../TemplateSettingsLayout";
1414
import { TemplatePermissionsPageView } from "./TemplatePermissionsPageView";
1515
import { docs } from "utils/docs";
1616
import { useMutation, useQuery, useQueryClient } from "react-query";
17-
import { addUserToTemplateACL, templateACL } from "api/queries/templates";
17+
import { setUserRole, templateACL } from "api/queries/templates";
18+
import { displaySuccess } from "components/GlobalSnackbar/utils";
1819

1920
export const TemplatePermissionsPage: FC<
2021
React.PropsWithChildren<unknown>
@@ -25,15 +26,16 @@ export const TemplatePermissionsPage: FC<
2526
const [state, send] = useMachine(templateACLMachine, {
2627
context: { templateId: template.id },
2728
});
28-
const { userToBeUpdated, groupToBeUpdated } = state.context;
29+
const { groupToBeUpdated } = state.context;
2930
const templateACLQuery = useQuery({
3031
...templateACL(template.id),
3132
onSuccess: (data) => {
3233
send({ type: "LOAD", data });
3334
},
3435
});
3536
const queryClient = useQueryClient();
36-
const addUserMutation = useMutation(addUserToTemplateACL(queryClient));
37+
const addUserMutation = useMutation(setUserRole(queryClient));
38+
const updateUserMutation = useMutation(setUserRole(queryClient));
3739

3840
return (
3941
<>
@@ -79,10 +81,19 @@ export const TemplatePermissionsPage: FC<
7981
reset();
8082
}}
8183
isAddingUser={addUserMutation.isLoading}
82-
onUpdateUser={(user, role) => {
83-
send("UPDATE_USER_ROLE", { user, role });
84+
onUpdateUser={async (user, role) => {
85+
await updateUserMutation.mutateAsync({
86+
templateId: template.id,
87+
userId: user.id,
88+
role,
89+
});
90+
displaySuccess("User role updated successfully!");
8491
}}
85-
updatingUser={userToBeUpdated}
92+
updatingUserId={
93+
updateUserMutation.isLoading
94+
? updateUserMutation.variables?.userId
95+
: undefined
96+
}
8697
onRemoveUser={(user) => {
8798
send("REMOVE_USER", { user });
8899
}}

site/src/pages/TemplateSettingsPage/TemplatePermissionsPage/TemplatePermissionsPageView.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export interface TemplatePermissionsPageViewProps {
161161
) => void;
162162
isAddingUser: boolean;
163163
onUpdateUser: (user: TemplateUser, role: TemplateRole) => void;
164-
updatingUser: TemplateUser | undefined;
164+
updatingUserId: TemplateUser["id"] | undefined;
165165
onRemoveUser: (user: TemplateUser) => void;
166166
// Group
167167
onAddGroup: (
@@ -185,7 +185,7 @@ export const TemplatePermissionsPageView: FC<
185185
// User
186186
onAddUser,
187187
isAddingUser,
188-
updatingUser,
188+
updatingUserId,
189189
onUpdateUser,
190190
onRemoveUser,
191191
// Group
@@ -313,9 +313,7 @@ export const TemplatePermissionsPageView: FC<
313313
<Cond condition={canUpdatePermissions}>
314314
<RoleSelect
315315
value={user.role}
316-
disabled={
317-
updatingUser && updatingUser.id === user.id
318-
}
316+
disabled={updatingUserId === user.id}
319317
onChange={(event) => {
320318
onUpdateUser(
321319
user,

site/src/xServices/template/templateACLXService.ts

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ export const templateACLMachine = createMachine(
1414
context: {} as {
1515
templateId: string;
1616
templateACL?: TemplateACL;
17-
// User
18-
userToBeUpdated?: TemplateUser;
19-
addUserCallback?: () => void;
2017
// Group
2118
groupToBeAdded?: TemplateGroup;
2219
groupToBeUpdated?: TemplateGroup;
@@ -27,9 +24,6 @@ export const templateACLMachine = createMachine(
2724
data: TemplateACL;
2825
};
2926
// User
30-
updateUser: {
31-
data: unknown;
32-
};
3327
// Group
3428
addGroup: {
3529
data: unknown;
@@ -43,11 +37,6 @@ export const templateACLMachine = createMachine(
4337
type: "LOAD";
4438
data: TemplateACL;
4539
}
46-
| {
47-
type: "UPDATE_USER_ROLE";
48-
user: TemplateUser;
49-
role: TemplateRole;
50-
}
5140
| {
5241
type: "REMOVE_USER";
5342
user: TemplateUser;
@@ -84,10 +73,6 @@ export const templateACLMachine = createMachine(
8473
idle: {
8574
on: {
8675
// User
87-
UPDATE_USER_ROLE: {
88-
target: "updatingUser",
89-
actions: ["assignUserToBeUpdated"],
90-
},
9176
REMOVE_USER: {
9277
target: "removingUser",
9378
actions: ["removeUserFromTemplateACL"],
@@ -108,19 +93,6 @@ export const templateACLMachine = createMachine(
10893
},
10994
},
11095
// User
111-
updatingUser: {
112-
invoke: {
113-
src: "updateUser",
114-
onDone: {
115-
target: "idle",
116-
actions: [
117-
"updateUserOnTemplateACL",
118-
"clearUserToBeUpdated",
119-
"displayUpdateUserSuccessMessage",
120-
],
121-
},
122-
},
123-
},
12496
removingUser: {
12597
invoke: {
12698
src: "removeUser",
@@ -167,12 +139,6 @@ export const templateACLMachine = createMachine(
167139
{
168140
services: {
169141
// User
170-
updateUser: ({ templateId }, { user, role }) =>
171-
updateTemplateACL(templateId, {
172-
user_perms: {
173-
[user.id]: role,
174-
},
175-
}),
176142
removeUser: ({ templateId }, { user }) =>
177143
updateTemplateACL(templateId, {
178144
user_perms: {
@@ -204,33 +170,6 @@ export const templateACLMachine = createMachine(
204170
templateACL: (_, { data }) => data,
205171
}),
206172
// User
207-
assignUserToBeUpdated: assign({
208-
userToBeUpdated: (_, { user, role }) => ({ ...user, role }),
209-
}),
210-
updateUserOnTemplateACL: assign({
211-
templateACL: ({ templateACL, userToBeUpdated }) => {
212-
if (!userToBeUpdated) {
213-
throw new Error("No user to be added");
214-
}
215-
if (!templateACL) {
216-
throw new Error("Template ACL is not loaded yet");
217-
}
218-
return {
219-
...templateACL,
220-
users: templateACL.users.map((oldTemplateUser) => {
221-
return oldTemplateUser.id === userToBeUpdated.id
222-
? userToBeUpdated
223-
: oldTemplateUser;
224-
}),
225-
};
226-
},
227-
}),
228-
clearUserToBeUpdated: assign({
229-
userToBeUpdated: (_) => undefined,
230-
}),
231-
displayUpdateUserSuccessMessage: () => {
232-
displaySuccess("User role update successfully!");
233-
},
234173
removeUserFromTemplateACL: assign({
235174
templateACL: ({ templateACL }, { user }) => {
236175
if (!templateACL) {

0 commit comments

Comments
 (0)