refactor: 💡 Move save back to route to unblock other work (#1598)

This commit is contained in:
Zhihe Li authored and GitHub committed 2023-01-23 14:32:31 -05:00
1 parent cf16bf8e33
commit da59684f25
4 files changed
+38 -44

No files matched your search

@@ -1,5 +1,5 @@
<Rose::Form <Rose::Form
@onSubmit={{this.submit}} @onSubmit={{@submit}}
@cancel={{@cancel}} @cancel={{@cancel}}
@disabled={{@model.isSaving}} @disabled={{@model.isSaving}}
@showEditToggle={{if @model.isNew false true}} @showEditToggle={{if @model.isNew false true}}
@@ -1,10 +1,8 @@
import Component from '@glimmer/component'; import Component from '@glimmer/component';
import { TYPES_TARGET } from 'api/models/target'; import { TYPES_TARGET } from 'api/models/target';
import { loading } from 'ember-loading';
import { inject as service } from '@ember/service';
import { action } from '@ember/object'; import { action } from '@ember/object';
import { notifyError } from 'core/decorators/notify';
import { tracked } from '@glimmer/tracking'; import { tracked } from '@glimmer/tracking';
// NOTE: this is all a temporary solution till we have a resource type helper. // NOTE: this is all a temporary solution till we have a resource type helper.
const types = [...TYPES_TARGET].reverse(); const types = [...TYPES_TARGET].reverse();
const icons = { const icons = {
@@ -18,10 +16,6 @@ export default class FormTargetComponent extends Component {
this.args.model.egress_worker_filter?.length; this.args.model.egress_worker_filter?.length;
@tracked migrateWorkerFilter = false; @tracked migrateWorkerFilter = false;
// =services
@service confirm;
@service intl;
// =properties // =properties
/** /**
@@ -78,39 +72,4 @@ export default class FormTargetComponent extends Component {
this.args.model.egress_worker_filter = this.args.model.worker_filter; this.args.model.egress_worker_filter = this.args.model.worker_filter;
this.args.model.worker_filter = ''; this.args.model.worker_filter = '';
} }
@action
@loading
@notifyError(({ message }) => message)
async submit() {
const target = this.args.model;
const numHostSources = target.host_sources?.length;
const address = target.address;
if (address && numHostSources) {
try {
await this.confirm.confirm(
this.intl.t(
'resources.target.questions.delete-host-sources.message',
{ numHostSources }
),
{
title: 'resources.target.questions.delete-host-sources.title',
confirm: 'actions.remove-resources',
}
);
} catch (e) {
// if the user denies, do nothing and return
return;
}
await target.removeHostSources(
target.host_sources.map((hs) => hs.host_source_id)
);
// After saving the host sources, the model gets reset to an empty address,
// so we need to update the address with the previous value before saving
target.address = address;
}
await this.args.submit();
}
} }
@@ -13,6 +13,7 @@ export default class ScopesScopeTargetsRoute extends Route {
@service session; @service session;
@service can; @service can;
@service router; @service router;
@service confirm;
// =methods // =methods
@@ -69,6 +70,40 @@ export default class ScopesScopeTargetsRoute extends Route {
this.refresh(); this.refresh();
} }
@action
@loading
@notifyError(({ message }) => message)
async saveWithAddress(target) {
const numHostSources = target.host_sources?.length;
const address = target.address;
if (address && numHostSources) {
try {
await this.confirm.confirm(
this.intl.t(
'resources.target.questions.delete-host-sources.message',
{ numHostSources }
),
{
title: 'resources.target.questions.delete-host-sources.title',
confirm: 'actions.remove-resources',
}
);
} catch (e) {
// if the user denies, do nothing and return
return;
}
await target.removeHostSources(
target.host_sources.map((hs) => hs.host_source_id)
);
// After saving the host sources, the model gets reset to an empty address,
// so we need to update the address with the previous value before saving
target.address = address;
}
await this.save(target);
}
/** /**
* Deletes a target and redirects to targets index. * Deletes a target and redirects to targets index.
* @param {TargetModel} target * @param {TargetModel} target
@@ -19,7 +19,7 @@
<page.body> <page.body>
<Form::Target <Form::Target
@model={{@model}} @model={{@model}}
@submit={{route-action 'save' @model}} @submit={{route-action 'saveWithAddress' @model}}
@cancel={{route-action 'cancel' @model}} @cancel={{route-action 'cancel' @model}}
/> />
</page.body> </page.body>