2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-12-09 23:21:42 +00:00
|
|
|
import Component from '@glimmer/component';
|
2022-12-15 22:57:20 +00:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import errorMessage from 'vault/utils/error-message';
|
2023-03-22 18:19:11 +00:00
|
|
|
import timestamp from 'vault/utils/timestamp';
|
2022-12-09 23:21:42 +00:00
|
|
|
/**
|
|
|
|
* @module DownloadButton
|
|
|
|
* DownloadButton components are an action button used to download data. Both the action text and icon are yielded.
|
2022-12-15 22:57:20 +00:00
|
|
|
* * NOTE: when using in an engine, remember to add the 'download' service to its dependencies (in /engine.js) and map to it in /app.js
|
|
|
|
* [ember-docs](https://ember-engines.com/docs/services)
|
2022-12-09 23:21:42 +00:00
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <DownloadButton
|
|
|
|
* class="button"
|
|
|
|
* @data={{this.data}}
|
|
|
|
* @filename={{this.filename}}
|
|
|
|
* @mime={{this.mime}}
|
|
|
|
* @extension={{this.extension}}
|
|
|
|
* @stringify={{true}}
|
|
|
|
* >
|
|
|
|
* <Icon @name="download" />
|
|
|
|
* Download
|
|
|
|
* </DownloadButton>
|
|
|
|
* ```
|
2022-12-15 22:57:20 +00:00
|
|
|
* @param {string} [filename] - name of file that prefixes the ISO timestamp generated at download
|
|
|
|
* @param {string} [data] - data to download
|
|
|
|
* @param {string} [extension='txt'] - file extension, the download service uses this to determine the mimetype
|
2022-12-09 23:21:42 +00:00
|
|
|
* @param {boolean} [stringify=false] - argument to stringify the data before passing to the File constructor
|
|
|
|
*/
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-12-09 23:21:42 +00:00
|
|
|
export default class DownloadButton extends Component {
|
2022-12-15 22:57:20 +00:00
|
|
|
@service download;
|
|
|
|
@service flashMessages;
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-12-09 23:21:42 +00:00
|
|
|
get filename() {
|
2023-03-22 18:19:11 +00:00
|
|
|
const ts = timestamp.now().toISOString();
|
|
|
|
return this.args.filename ? this.args.filename + '-' + ts : ts;
|
2022-12-09 23:21:42 +00:00
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-12-15 22:57:20 +00:00
|
|
|
get content() {
|
2022-12-09 23:21:42 +00:00
|
|
|
if (this.args.stringify) {
|
|
|
|
return JSON.stringify(this.args.data, null, 2);
|
2018-04-17 20:02:37 +00:00
|
|
|
}
|
2022-12-09 23:21:42 +00:00
|
|
|
return this.args.data;
|
|
|
|
}
|
2018-04-17 20:02:37 +00:00
|
|
|
|
2022-12-15 22:57:20 +00:00
|
|
|
get extension() {
|
|
|
|
return this.args.extension || 'txt';
|
|
|
|
}
|
|
|
|
|
2022-12-09 23:21:42 +00:00
|
|
|
@action
|
|
|
|
handleDownload() {
|
2022-12-15 22:57:20 +00:00
|
|
|
try {
|
|
|
|
this.download.miscExtension(this.filename, this.content, this.extension);
|
|
|
|
this.flashMessages.info(`Downloading ${this.filename}`);
|
|
|
|
} catch (error) {
|
|
|
|
this.flashMessages.danger(errorMessage(error, 'There was a problem downloading. Please try again.'));
|
|
|
|
}
|
2022-12-09 23:21:42 +00:00
|
|
|
}
|
|
|
|
}
|