Skip to content

Commit

Permalink
use promise instead of cb
Browse files Browse the repository at this point in the history
  • Loading branch information
weizman committed Mar 7, 2023
1 parent a943cc1 commit 6de68ce
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions ui/helpers/utils/export-utils.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { getRandomFileName } from './util';

function encodeB64(text, cb) {
const buf = new TextEncoder().encode(text);
// eslint-disable-next-line no-undef
const reader = new FileReader();
reader.onload = () => cb(reader.result.split(',')[1]);
// eslint-disable-next-line no-undef
reader.readAsDataURL(new Blob([buf]));
function encodeB64(text) {
return new Promise((resolve) => {
const buf = new TextEncoder().encode(text);
// eslint-disable-next-line no-undef
const reader = new FileReader();
reader.onload = () => resolve(reader.result.split(',')[1]);
// eslint-disable-next-line no-undef
reader.readAsDataURL(new Blob([buf]));
});
}

export function exportAsFile(filename, data, type = 'text/csv') {
encodeB64(data, (b64) => {
// eslint-disable-next-line no-param-reassign
filename = filename || getRandomFileName();
const elem = window.document.createElement('a');
elem.href = `data:${type};Base64,${b64}`;
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
});
export async function exportAsFile(filename, data, type = 'text/csv') {
const b64 = await encodeB64(data);
// eslint-disable-next-line no-param-reassign
filename = filename || getRandomFileName();
const elem = window.document.createElement('a');
elem.href = `data:${type};Base64,${b64}`;
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}

0 comments on commit 6de68ce

Please sign in to comment.