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 adding user
  • Loading branch information
BrunoQuaresma committed Oct 18, 2023
commit f424a70de78d72b0aab4dc9dfb39c981c07fefa8
2 changes: 1 addition & 1 deletion site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ export const getTemplateACL = async (
export const updateTemplateACL = async (
templateId: string,
data: TypesGen.UpdateTemplateACL,
): Promise<TypesGen.TemplateACL> => {
): Promise<{ message: string }> => {
const response = await axios.patch(
`/api/v2/templates/${templateId}/acl`,
data,
Expand Down
27 changes: 26 additions & 1 deletion site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import {
type TemplateVersion,
CreateTemplateRequest,
ProvisionerJob,
TemplateRole,
} from "api/typesGenerated";
import { type QueryClient, type QueryOptions } from "react-query";
import {
MutationOptions,
type QueryClient,
type QueryOptions,
} from "react-query";
import { delay } from "utils/delay";

export const templateByNameKey = (orgId: string, name: string) => [
Expand Down Expand Up @@ -43,6 +48,26 @@ export const templateACL = (templateId: string) => {
};
};

export const addUserToTemplateACL = (
queryClient: QueryClient,
): MutationOptions<
Awaited<ReturnType<typeof API.updateTemplateACL>>,
unknown,
{ templateId: string; userId: string; role: TemplateRole }
> => {
return {
mutationFn: ({ templateId, userId, role }) =>
API.updateTemplateACL(templateId, {
user_perms: {
[userId]: role,
},
}),
onSuccess: async (templateAcl, { templateId }) => {
await queryClient.invalidateQueries(["templateAcl", templateId]);
},
};
};

export const templateExamples = (orgId: string) => {
return {
queryKey: [...getTemplatesQueryKey(orgId), "examples"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { templateACLMachine } from "xServices/template/templateACLXService";
import { useTemplateSettings } from "../TemplateSettingsLayout";
import { TemplatePermissionsPageView } from "./TemplatePermissionsPageView";
import { docs } from "utils/docs";
import { useQuery } from "react-query";
import { templateACL } from "api/queries/templates";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { addUserToTemplateACL, templateACL } from "api/queries/templates";

export const TemplatePermissionsPage: FC<
React.PropsWithChildren<unknown>
Expand All @@ -32,6 +32,8 @@ export const TemplatePermissionsPage: FC<
send({ type: "LOAD", data });
},
});
const queryClient = useQueryClient();
const addUserMutation = useMutation(addUserToTemplateACL(queryClient));

return (
<>
Expand Down Expand Up @@ -68,10 +70,15 @@ export const TemplatePermissionsPage: FC<
templateID={template.id}
templateACL={templateACLQuery.data}
canUpdatePermissions={Boolean(permissions?.canUpdateTemplate)}
onAddUser={(user, role, reset) => {
send("ADD_USER", { user, role, onDone: reset });
onAddUser={async (user, role, reset) => {
await addUserMutation.mutateAsync({
templateId: template.id,
userId: user.id,
role,
});
reset();
}}
isAddingUser={state.matches("addingUser")}
isAddingUser={addUserMutation.isLoading}
onUpdateUser={(user, role) => {
send("UPDATE_USER_ROLE", { user, role });
}}
Expand Down
49 changes: 0 additions & 49 deletions site/src/xServices/template/templateACLXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const templateACLMachine = createMachine(
templateId: string;
templateACL?: TemplateACL;
// User
userToBeAdded?: TemplateUser;
userToBeUpdated?: TemplateUser;
addUserCallback?: () => void;
// Group
Expand All @@ -28,9 +27,6 @@ export const templateACLMachine = createMachine(
data: TemplateACL;
};
// User
addUser: {
data: unknown;
};
updateUser: {
data: unknown;
};
Expand All @@ -47,12 +43,6 @@ export const templateACLMachine = createMachine(
type: "LOAD";
data: TemplateACL;
}
| {
type: "ADD_USER";
user: TemplateUser;
role: TemplateRole;
onDone: () => void;
}
| {
type: "UPDATE_USER_ROLE";
user: TemplateUser;
Expand Down Expand Up @@ -94,7 +84,6 @@ export const templateACLMachine = createMachine(
idle: {
on: {
// User
ADD_USER: { target: "addingUser", actions: ["assignUserToBeAdded"] },
UPDATE_USER_ROLE: {
target: "updatingUser",
actions: ["assignUserToBeUpdated"],
Expand All @@ -119,15 +108,6 @@ export const templateACLMachine = createMachine(
},
},
// User
addingUser: {
invoke: {
src: "addUser",
onDone: {
target: "idle",
actions: ["addUserToTemplateACL", "runAddUserCallback"],
},
},
},
updatingUser: {
invoke: {
src: "updateUser",
Expand Down Expand Up @@ -187,12 +167,6 @@ export const templateACLMachine = createMachine(
{
services: {
// User
addUser: ({ templateId }, { user, role }) =>
updateTemplateACL(templateId, {
user_perms: {
[user.id]: role,
},
}),
updateUser: ({ templateId }, { user, role }) =>
updateTemplateACL(templateId, {
user_perms: {
Expand Down Expand Up @@ -230,29 +204,6 @@ export const templateACLMachine = createMachine(
templateACL: (_, { data }) => data,
}),
// User
assignUserToBeAdded: assign({
userToBeAdded: (_, { user, role }) => ({ ...user, role }),
addUserCallback: (_, { onDone }) => onDone,
}),
addUserToTemplateACL: assign({
templateACL: ({ templateACL, userToBeAdded }) => {
if (!userToBeAdded) {
throw new Error("No user to be added");
}
if (!templateACL) {
throw new Error("Template ACL is not loaded yet");
}
return {
...templateACL,
users: [...templateACL.users, userToBeAdded],
};
},
}),
runAddUserCallback: ({ addUserCallback }) => {
if (addUserCallback) {
addUserCallback();
}
},
assignUserToBeUpdated: assign({
userToBeUpdated: (_, { user, role }) => ({ ...user, role }),
}),
Expand Down