refactor: 💡 remove route-action usage for groups (#2176)

* refactor: 💡 remove route-action usage for groups

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12605
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2024-03-14 13:28:43 -07:00
1 parent 6b0958e4d8
commit 0902e42cb0
20 files changed
+191 -111

No files matched your search

@@ -18,7 +18,7 @@
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'delete' @model)}}
{{on 'click' @delete}}
>
{{t 'resources.group.actions.delete'}}
</dropdown.button>
@@ -4,10 +4,16 @@
*/
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeGroupsGroupAddMembersController extends Controller {
// =services
@service router;
// =attributes
/**
@@ -26,4 +32,29 @@ export default class ScopesScopeGroupsGroupAddMembersController extends Controll
const sorted = [global, ...orgsAndProjects].flat();
return sorted;
}
// =actions
/**
* Adds members to the group and saves, replaces with the members index
* route, and notifies the user of success or error.
* @param {GroupModel} group
* @param {[string]} userIDs
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.add-success')
async addMembers(group, userIDs) {
await group.addMembers(userIDs);
await this.router.replaceWith('scopes.scope.groups.group.members');
}
/**
* Redirect to group members as if nothing ever happened.
*/
@action
async cancel() {
await this.router.replaceWith('scopes.scope.groups.group.members');
}
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeGroupsGroupIndexController extends Controller {
@controller('scopes/scope/groups/index') groups;
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeGroupsGroupMembersController extends Controller {
@controller('scopes/scope/groups/index') groups;
}
@@ -0,0 +1,75 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeGroupsIndexController extends Controller {
// =services
@service can;
@service router;
// =actions
/**
* Rollback changes on a group.
* @param {GroupModel} group
*/
@action
async cancel(group) {
const { isNew } = group;
group.rollbackAttributes();
if (isNew) await this.router.transitionTo('scopes.scope.groups');
}
/**
* Save a group in current scope.
* @param {GroupModel} group
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(group) {
await group.save();
if (this.can.can('read model', group)) {
await this.router.transitionTo('scopes.scope.groups.group', group);
} else {
await this.router.transitionTo('scopes.scope.groups');
}
await this.router.refresh();
}
/**
* Delete a group in current scope and redirect to index
* @param {GroupModel} group
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(group) {
await group.destroyRecord();
await this.router.replaceWith('scopes.scope.groups');
await this.router.refresh();
}
/**
* Remove a member from the current role and redirect to members index.
* @param {GroupModel} group
* @param {UserModel} member
*/
@action
@loading
@confirm('questions.remove-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.remove-success')
async removeMember(group, member) {
await group.removeMember(member.id);
await this.router.refresh();
}
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeGroupsNewController extends Controller {
@controller('scopes/scope/groups/index') groups;
}
@@ -5,16 +5,11 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeGroupsRoute extends Route {
// =services
@service store;
@service intl;
@service session;
@service can;
@service router;
@@ -39,51 +34,4 @@ export default class ScopesScopeGroupsRoute extends Route {
return this.store.query('group', { scope_id });
}
}
// =actions
/**
* Rollback changes on a group.
* @param {GroupModel} group
*/
@action
cancel(group) {
const { isNew } = group;
group.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.groups');
}
/**
* Save a group in current scope.
* @param {GroupModel} group
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(group) {
await group.save();
if (this.can.can('read model', group)) {
await this.router.transitionTo('scopes.scope.groups.group', group);
} else {
await this.router.transitionTo('scopes.scope.groups');
}
this.refresh();
}
/**
* Delete a group in current scope and redirect to index
* @param {GroupModel} group
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(group) {
await group.destroyRecord();
await this.router.replaceWith('scopes.scope.groups');
this.refresh();
}
}
@@ -7,8 +7,6 @@ import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { hash } from 'rsvp';
import { loading } from 'ember-loading';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { resourceFilter } from 'core/decorators/resource-filter';
export default class ScopesScopeGroupsGroupAddMembersRoute extends Route {
@@ -67,29 +65,6 @@ export default class ScopesScopeGroupsGroupAddMembersRoute extends Route {
// =actions
/**
* Adds members to the group and saves, replaces with the members index
* route, and notifies the user of success or error.
* @param {GroupModel} group
* @param {[string]} userIDs
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.add-success')
async addMembers(group, userIDs) {
await group.addMembers(userIDs);
await this.router.replaceWith('scopes.scope.groups.group.members');
}
/**
* Redirect to group members as if nothing ever happened.
*/
@action
cancel() {
this.router.replaceWith('scopes.scope.groups.group.members');
}
/**
* Sets the specified resource filter field to the specified value.
* @param {string} field
@@ -4,12 +4,8 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { hash } from 'rsvp';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeGroupsGroupMembersRoute extends Route {
// =services
@@ -37,21 +33,4 @@ export default class ScopesScopeGroupsGroupMembersRoute extends Route {
: [],
});
}
// =actions
/**
* Remove a member from the current role and redirect to members index.
* @param {GroupModel} group
* @param {UserModel} member
*/
@action
@loading
@confirm('questions.remove-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.remove-success')
async removeMember(group, member) {
await group.removeMember(member.id);
this.refresh();
}
}
@@ -86,8 +86,8 @@
<Form::Group::AddMembers
@model={{@model.group}}
@users={{@model.users}}
@submit={{route-action 'addMembers' @model.group}}
@cancel={{route-action 'cancel'}}
@submit={{fn this.addMembers @model.group}}
@cancel={{this.cancel}}
/>
</page.body>
@@ -18,14 +18,17 @@
</page.navigation>
<page.actions>
<Groups::Group::Actions @model={{@model}} />
<Groups::Group::Actions
@model={{@model}}
@delete={{fn this.groups.delete @model}}
/>
</page.actions>
<page.body>
<Form::Group
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@submit={{fn this.groups.save @model}}
@cancel={{fn this.groups.cancel @model}}
/>
</page.body>
@@ -24,7 +24,10 @@
</page.navigation>
<page.actions>
<Groups::Group::Actions @model={{@model.group}} />
<Groups::Group::Actions
@model={{@model.group}}
@delete={{fn this.groups.delete @model.group}}
/>
</page.actions>
<page.body>
@@ -75,7 +78,7 @@
@text={{t 'actions.remove'}}
{{on
'click'
(route-action 'removeMember' @model.group B.data)
(fn this.groups.removeMember @model.group B.data)
}}
/>
</Hds::Dropdown>
@@ -26,8 +26,8 @@
<page.body>
<Form::Group
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@submit={{fn this.groups.save @model}}
@cancel={{fn this.groups.cancel @model}}
/>
</page.body>
@@ -4,7 +4,7 @@
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { setupTest } from 'admin/tests/helpers';
module('Unit | Controller | scopes/scope/auth-methods/index', function (hooks) {
setupTest(hooks);
@@ -4,7 +4,7 @@
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { setupTest } from 'admin/tests/helpers';
module('Unit | Controller | scopes/scope/auth-methods/new', function (hooks) {
setupTest(hooks);
@@ -4,7 +4,7 @@
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { setupTest } from 'admin/tests/helpers';
module(
'Unit | Controller | scopes/scope/groups/group/add-members',
@@ -0,0 +1,13 @@
import { module, test } from 'qunit';
import { setupTest } from 'admin/tests/helpers';
module('Unit | Controller | scopes/scope/groups/group/index', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/groups/group/index',
);
assert.ok(controller);
});
});
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'admin/tests/helpers';
module(
'Unit | Controller | scopes/scope/groups/group/members',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/groups/group/members',
);
assert.ok(controller);
});
},
);
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'admin/tests/helpers';
module('Unit | Controller | scopes/scope/groups/index', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/groups/index');
assert.ok(controller);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'admin/tests/helpers';
module('Unit | Controller | scopes/scope/groups/new', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup('controller:scopes/scope/groups/new');
assert.ok(controller);
});
});