Skip to content

Commit

Permalink
Let a user download file locally
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Jun 27, 2023
1 parent 90832d9 commit 5169c9d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions ui/app/components/job-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions ui/app/templates/components/job-editor/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@
data-test-save-as-template
/>
{{/if}}
<Hds::Button
@text="Save as .nomad.hcl"
@color="secondary"
{{on "click" @fns.onSaveFile}}

/>
</Hds::ButtonSet>

0 comments on commit 5169c9d

Please sign in to comment.