refactor: 💡 remove route-action usage for workers (#2302)

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12613
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2024-06-10 17:12:49 -07:00
1 parent ea460c1391
commit 12e0982d4b
11 files changed
+252 -98

No files matched your search

@@ -0,0 +1,62 @@
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 ScopesScopeWorkersIndexController extends Controller {
// =services
@service router;
@service can;
// =actions
/**
* Rollback changes on workers.
* @param {WorkerModel} worker
*/
@action
cancel(worker) {
const { isNew } = worker;
worker.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.workers');
}
/**
* Save a worker in current scope.
* @param {WorkerModel} worker
* @param {Event} e
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(worker) {
await worker.save();
if (this.can.can('read model', worker)) {
await this.router.transitionTo('scopes.scope.workers.worker', worker);
} else {
await this.router.transitionTo('scopes.scope.workers');
}
await this.router.refresh();
}
/**
* Delete worker in current scope and redirect to index.
* @param {WorkerModel} worker
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(worker) {
await worker.destroyRecord();
await this.router.replaceWith('scopes.scope.workers');
await this.router.refresh();
}
}
@@ -0,0 +1,38 @@
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 ScopesScopeWorkersNewController extends Controller {
// =services
@service router;
// =actions
/**
* Rollback changes on workers.
* @param {WorkerModel} worker
*/
@action
async cancel(worker) {
const { isNew } = worker;
if (isNew) worker.rollbackAttributes();
this.router.transitionTo('scopes.scope.workers');
await this.router.refresh();
}
/**
* Created the worker and notifies the user of success or error.
* @param {WorkerModel} worker
* @param {string} workerGeneratedAuthToken
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.add-success')
async createWorkerLed(worker, workerGeneratedAuthToken) {
await worker.createWorkerLed(workerGeneratedAuthToken);
}
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeWorkersWorkerIndexController extends Controller {
@controller('scopes/scope/workers/index') workers;
}
@@ -6,9 +6,6 @@
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 ScopesScopeWorkersRoute extends Route {
// =services
@@ -46,47 +43,4 @@ export default class ScopesScopeWorkersRoute extends Route {
refresh() {
return super.refresh(...arguments);
}
@action
cancel(worker) {
const { isNew } = worker;
if (isNew) worker.rollbackAttributes();
this.router.replaceWith('scopes.scope.workers');
this.refresh();
}
/**
* Save a worker in current scope.
* @param {WorkerModel} worker
* @param {Event} e
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(worker) {
await worker.save();
if (this.can.can('read model', worker)) {
await this.router.transitionTo('scopes.scope.workers.worker', worker);
} else {
await this.router.transitionTo('scopes.scope.workers');
}
this.refresh();
}
/**
* Delete worker in current scope and redirect to index.
* @param {UserModel} worker
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(worker) {
await worker.destroyRecord();
await this.router.replaceWith('scopes.scope.workers');
this.refresh();
}
}
@@ -4,9 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifyError, notifySuccess } from 'core/decorators/notify';
import { inject as service } from '@ember/service';
export default class ScopesScopeWorkersNewRoute extends Route {
@@ -42,17 +39,4 @@ export default class ScopesScopeWorkersNewRoute extends Route {
record.scopeModel = scopeModel;
return record;
}
/**
* Created the worker and notifies the user of success or error.
* @param {WorkerModel} worker
* @param {string} workerGeneratedAuthToken
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.add-success')
async createWorkerLed(worker, workerGeneratedAuthToken) {
await worker.createWorkerLed(workerGeneratedAuthToken);
}
}
@@ -1,15 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
export default class ScopesScopeWorkersWorkerIndexRoute extends Route {
@action
cancel(worker) {
worker.rollbackAttributes();
this.refresh();
}
}
@@ -24,8 +24,8 @@
<page.body>
<Form::Worker::CreateWorkerLed
@model={{@model}}
@cancel={{route-action 'cancel' @model}}
@submit={{route-action 'createWorkerLed' @model}}
@cancel={{fn this.cancel @model}}
@submit={{fn this.createWorkerLed @model}}
/>
</page.body>
@@ -36,7 +36,7 @@
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'delete' @model)}}
{{on 'click' (fn this.workers.delete @model)}}
>
{{t 'resources.worker.actions.delete'}}
</dropdown.button>
@@ -47,8 +47,8 @@
<page.body>
<Form::Worker
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@submit={{fn this.workers.save @model}}
@cancel={{fn this.workers.cancel @model}}
/>
</page.body>
@@ -0,0 +1,77 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { authenticateSession } from 'ember-simple-auth/test-support';
module('Unit | Controller | scopes/scope/workers/index', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
let store;
let controller;
let getWorkerCount;
const instances = {
scopes: {
global: null,
},
worker: null,
};
const urls = {
globalScope: null,
workers: null,
};
hooks.beforeEach(function () {
authenticateSession({});
store = this.owner.lookup('service:store');
controller = this.owner.lookup('controller:scopes/scope/workers/index');
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.worker = this.server.create('worker', {
scope: instances.scopes.global,
});
urls.globalScope = `/scopes/global`;
urls.workers = `${urls.globalScope}/workers`;
getWorkerCount = () => this.server.schema.workers.all().models.length;
});
test('it exists', function (assert) {
assert.ok(controller);
});
test('cancel action rolls-back changes on the specified model', async function (assert) {
await visit(urls.workers);
const worker = await store.findRecord('worker', instances.worker.id);
worker.name = 'test';
assert.strictEqual(worker.name, 'test');
await controller.cancel(worker);
assert.notEqual(worker.name, 'test');
});
test('save action saves changes on the specified model', async function (assert) {
await visit(urls.workers);
const worker = await store.findRecord('worker', instances.worker.id);
worker.name = 'test';
await controller.save(worker);
assert.strictEqual(worker.name, 'test');
});
test('delete action destroys specified model', async function (assert) {
const worker = await store.findRecord('worker', instances.worker.id);
const workerCount = getWorkerCount();
await controller.delete(worker);
assert.strictEqual(getWorkerCount(), workerCount - 1);
});
});
@@ -0,0 +1,65 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { authenticateSession } from 'ember-simple-auth/test-support';
module('Unit | Controller | scopes/scope/workers/new', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
let store;
let controller;
const instances = {
scopes: {
global: null,
},
worker: null,
};
const urls = {
globalScope: null,
workers: null,
};
hooks.beforeEach(function () {
authenticateSession({});
store = this.owner.lookup('service:store');
controller = this.owner.lookup('controller:scopes/scope/workers/new');
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.worker = this.server.create('worker', {
scope: instances.scopes.global,
});
urls.globalScope = `/scopes/global`;
urls.workers = `${urls.globalScope}/workers`;
});
test('it exists', function (assert) {
assert.ok(controller);
});
test('cancel action rolls-back changes on a new model', async function (assert) {
await visit(urls.workers);
const worker = store.createRecord('worker', instances.worker.id);
worker.name = 'test';
assert.strictEqual(worker.name, 'test');
await controller.cancel(worker);
assert.notEqual(worker.name, 'test');
});
test('createWorkerLed action saves changes on the specified model', async function (assert) {
await visit(urls.workers);
const worker = await store.findRecord('worker', instances.worker.id);
worker.name = 'test';
await controller.createWorkerLed(worker);
assert.strictEqual(worker.name, 'test');
});
});
@@ -1,16 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/workers/worker/index', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let route = this.owner.lookup('route:scopes/scope/workers/worker/index');
assert.ok(route);
});
});