Skip to content

Commit

Permalink
fix(editor): Add confirmation toast when changing user role (#10592)
Browse files Browse the repository at this point in the history
  • Loading branch information
r00gm authored Aug 29, 2024
1 parent c988931 commit 95da4d4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/editor-ui/src/plugins/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,9 @@
"settings.users.usersInvitedError": "Could not invite users",
"settings.users.advancedPermissions.warning": "{link} to unlock the ability to create additional admin users",
"settings.users.advancedPermissions.warning.link": "Upgrade",
"settings.users.userRoleUpdated": "Changes saved",
"settings.users.userRoleUpdated.message": "{user} has been successfully updated to a {role}",
"settings.users.userRoleUpdatedError": "Unable to updated role",
"settings.api": "API",
"settings.n8napi": "n8n API",
"settings.log-streaming": "Log Streaming",
Expand Down
37 changes: 37 additions & 0 deletions packages/editor-ui/src/views/SettingsUsersView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ import { useSettingsStore } from '@/stores/settings.store';
import { defaultSettings } from '@/__tests__/defaults';
import { ProjectTypes } from '@/types/projects.types';

const showToast = vi.fn();
const showError = vi.fn();

vi.mock('@/composables/useToast', () => ({
useToast: () => ({
showToast,
showError,
}),
}));

const wrapperComponentWithModal = {
components: { SettingsUsersView, ModalRoot, DeleteUserModal },
template: `
Expand Down Expand Up @@ -71,6 +81,9 @@ describe('SettingsUsersView', () => {
vi.spyOn(projectsStore, 'projects', 'get').mockReturnValue(projects);

usersStore.currentUserId = loggedInUser.id;

showToast.mockReset();
showError.mockReset();
});

afterEach(() => {
Expand Down Expand Up @@ -156,4 +169,28 @@ describe('SettingsUsersView', () => {
id: users[0].id,
});
});

it("should show success toast when changing a user's role", async () => {
const updateGlobalRoleSpy = vi.spyOn(usersStore, 'updateGlobalRole').mockResolvedValue();

const { getByTestId } = createComponentRenderer(SettingsUsersView)({
pinia,
});

const userListItem = getByTestId(`user-list-item-${users.at(-1)?.email}`);
expect(userListItem).toBeInTheDocument();

const roleSelect = within(userListItem).getByTestId('user-role-select');

const roleDropdownItems = await getDropdownItems(roleSelect);
await userEvent.click(roleDropdownItems[0]);

expect(updateGlobalRoleSpy).toHaveBeenCalledWith(
expect.objectContaining({ newRoleName: 'global:member' }),
);

expect(showToast).toHaveBeenCalledWith(
expect.objectContaining({ type: 'success', message: expect.stringContaining('Member') }),
);
});
});
19 changes: 18 additions & 1 deletion packages/editor-ui/src/views/SettingsUsersView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,24 @@ function goToUpgradeAdvancedPermissions() {
void uiStore.goToUpgrade('settings-users', 'upgrade-advanced-permissions');
}
async function onRoleChange(user: IUser, newRoleName: UpdateGlobalRolePayload['newRoleName']) {
await usersStore.updateGlobalRole({ id: user.id, newRoleName });
try {
await usersStore.updateGlobalRole({ id: user.id, newRoleName });
const role = userRoles.value.find(({ value }) => value === newRoleName)?.label || newRoleName;
showToast({
type: 'success',
title: i18n.baseText('settings.users.userRoleUpdated'),
message: i18n.baseText('settings.users.userRoleUpdated.message', {
interpolate: {
user: user.fullName ?? '',
role,
},
}),
});
} catch (e) {
showError(e, i18n.baseText('settings.users.userReinviteError'));
}
}
</script>

Expand Down

0 comments on commit 95da4d4

Please sign in to comment.