mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 18:27:25 +03:00
Extracted helper function to generate a Csv file
This commit is contained in:
parent
1594717f33
commit
3f3523b80f
3 changed files with 17 additions and 42 deletions
|
@ -2,30 +2,9 @@ import { dissoc, head, keys, values } from 'ramda';
|
|||
import { CsvJson } from 'csvjson';
|
||||
import LocalStorage from '../../utils/services/LocalStorage';
|
||||
import { ServersMap } from '../data';
|
||||
import { saveCsv } from '../../utils/helpers/csv';
|
||||
|
||||
const saveCsv = (window: Window, csv: string) => {
|
||||
const { navigator, document } = window;
|
||||
const filename = 'shlink-servers.csv';
|
||||
const blob = new Blob([ csv ], { type: 'text/csv;charset=utf-8;' });
|
||||
|
||||
// IE10 and IE11
|
||||
if (navigator.msSaveBlob) {
|
||||
navigator.msSaveBlob(blob, filename);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Modern browsers
|
||||
const link = document.createElement('a');
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
link.setAttribute('href', url);
|
||||
link.setAttribute('download', filename);
|
||||
link.style.visibility = 'hidden';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
};
|
||||
const SERVERS_FILENAME = 'shlink-servers.csv';
|
||||
|
||||
export default class ServersExporter {
|
||||
public constructor(
|
||||
|
@ -42,10 +21,9 @@ export default class ServersExporter {
|
|||
headers: keys(head(servers)).join(','),
|
||||
});
|
||||
|
||||
saveCsv(this.window, csv);
|
||||
saveCsv(this.window, csv, SERVERS_FILENAME);
|
||||
} catch (e) {
|
||||
// FIXME Handle error
|
||||
/* eslint no-console: "off" */
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
|
12
src/utils/helpers/csv.ts
Normal file
12
src/utils/helpers/csv.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
export const saveCsv = ({ document }: Window, csv: string, filename: string) => {
|
||||
const link = document.createElement('a');
|
||||
const blob = new Blob([ csv ], { type: 'text/csv;charset=utf-8;' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
link.setAttribute('href', url);
|
||||
link.setAttribute('download', filename);
|
||||
link.style.visibility = 'hidden';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
};
|
|
@ -11,10 +11,7 @@ describe('ServersExporter', () => {
|
|||
});
|
||||
const appendChild = jest.fn();
|
||||
const removeChild = jest.fn();
|
||||
const createWindowMock = (isIe10 = true) => Mock.of<Window>({
|
||||
navigator: {
|
||||
msSaveBlob: isIe10 ? jest.fn() : undefined,
|
||||
},
|
||||
const createWindowMock = () => Mock.of<Window>({
|
||||
document: {
|
||||
createElement: jest.fn(() => createLinkMock()),
|
||||
body: { appendChild, removeChild },
|
||||
|
@ -66,21 +63,9 @@ describe('ServersExporter', () => {
|
|||
expect(erroneousToCsv).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('makes use of msSaveBlob API when available', () => {
|
||||
it('makes use of download link API', () => {
|
||||
const windowMock = createWindowMock();
|
||||
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
||||
const { navigator: { msSaveBlob }, document: { createElement } } = windowMock;
|
||||
|
||||
exporter.exportServers();
|
||||
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
expect(msSaveBlob).toHaveBeenCalledTimes(1);
|
||||
expect(createElement).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('makes use of download link API when available', () => {
|
||||
const windowMock = createWindowMock(false);
|
||||
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
||||
const { document: { createElement } } = windowMock;
|
||||
|
||||
exporter.exportServers();
|
||||
|
|
Loading…
Reference in a new issue