From da59684f254aafeba5cdd80575fdc69644bd4bf3 Mon Sep 17 00:00:00 2001 From: Zhihe Li Date: Mon, 23 Jan 2023 14:32:31 -0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20Move=20save=20back?= =?UTF-8?q?=20to=20route=20to=20unblock=20other=20work=20(#1598)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/form/target/details/index.hbs | 2 +- .../components/form/target/details/index.js | 43 +------------------ ui/admin/app/routes/scopes/scope/targets.js | 35 +++++++++++++++ .../scopes/scope/targets/target/index.hbs | 2 +- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/ui/admin/app/components/form/target/details/index.hbs b/ui/admin/app/components/form/target/details/index.hbs index 56cf42ff7..2fa1f77bd 100644 --- a/ui/admin/app/components/form/target/details/index.hbs +++ b/ui/admin/app/components/form/target/details/index.hbs @@ -1,5 +1,5 @@ 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(); - } } diff --git a/ui/admin/app/routes/scopes/scope/targets.js b/ui/admin/app/routes/scopes/scope/targets.js index da6a73893..1adab4283 100644 --- a/ui/admin/app/routes/scopes/scope/targets.js +++ b/ui/admin/app/routes/scopes/scope/targets.js @@ -13,6 +13,7 @@ export default class ScopesScopeTargetsRoute extends Route { @service session; @service can; @service router; + @service confirm; // =methods @@ -69,6 +70,40 @@ export default class ScopesScopeTargetsRoute extends Route { 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. * @param {TargetModel} target diff --git a/ui/admin/app/templates/scopes/scope/targets/target/index.hbs b/ui/admin/app/templates/scopes/scope/targets/target/index.hbs index 164acd745..626c46a18 100644 --- a/ui/admin/app/templates/scopes/scope/targets/target/index.hbs +++ b/ui/admin/app/templates/scopes/scope/targets/target/index.hbs @@ -19,7 +19,7 @@