From 5169c9d315df24e5ca2cde402f71c429f6745abc Mon Sep 17 00:00:00 2001 From: Phil Renaud Date: Tue, 27 Jun 2023 13:51:11 -0400 Subject: [PATCH] Let a user download file locally --- ui/app/components/job-editor.js | 38 +++++++++++++++++++ .../templates/components/job-editor/edit.hbs | 6 +++ 2 files changed, 44 insertions(+) diff --git a/ui/app/components/job-editor.js b/ui/app/components/job-editor.js index 980b9e9bba6d..c03b4666ee1d 100644 --- a/ui/app/components/job-editor.js +++ b/ui/app/components/job-editor.js @@ -21,6 +21,7 @@ import { tracked } from '@glimmer/tracking'; export default class JobEditor extends Component { @service config; @service store; + @service notifications; @tracked error = null; @tracked planOutput = null; @@ -199,6 +200,42 @@ export default class JobEditor extends Component { reader.readAsText(file); } + /** + * Download the job's definition or specification as .nomad.hcl file locally + */ + @action + async handleSaveAsFile() { + try { + const blob = new Blob([this.args.job._newDefinition], { + type: 'text/plain', + }); + const url = window.URL.createObjectURL(blob); + const downloadAnchor = document.createElement('a'); + + downloadAnchor.href = url; + downloadAnchor.target = '_blank'; + downloadAnchor.rel = 'noopener noreferrer'; + downloadAnchor.download = 'jobspec.nomad.hcl'; + + downloadAnchor.click(); + downloadAnchor.remove(); + + window.URL.revokeObjectURL(url); + this.notifications.add({ + title: 'jobspec.nomad.hcl has been downloaded', + color: 'success', + icon: 'download', + }); + } catch (err) { + this.notifications.add({ + title: 'Error downloading file', + message: err.message, + color: 'critical', + sticky: true, + }); + } + } + /** * Get the definition or specification based on the view type. * @@ -253,6 +290,7 @@ export default class JobEditor extends Component { onPlan: this.plan, onReset: this.reset, onSaveAs: this.args.handleSaveAsTemplate, + onSaveFile: this.handleSaveAsFile, onSubmit: this.submit, onSelect: this.args.onSelect, onUpdate: this.updateCode, diff --git a/ui/app/templates/components/job-editor/edit.hbs b/ui/app/templates/components/job-editor/edit.hbs index 31c90343eb6e..cdcab2af55bb 100644 --- a/ui/app/templates/components/job-editor/edit.hbs +++ b/ui/app/templates/components/job-editor/edit.hbs @@ -99,4 +99,10 @@ data-test-save-as-template /> {{/if}} +