mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-18 13:33:51 +00:00
Extracted helper function to generate a Csv file
This commit is contained in:
@@ -2,30 +2,9 @@ import { dissoc, head, keys, values } from 'ramda';
|
|||||||
import { CsvJson } from 'csvjson';
|
import { CsvJson } from 'csvjson';
|
||||||
import LocalStorage from '../../utils/services/LocalStorage';
|
import LocalStorage from '../../utils/services/LocalStorage';
|
||||||
import { ServersMap } from '../data';
|
import { ServersMap } from '../data';
|
||||||
|
import { saveCsv } from '../../utils/helpers/csv';
|
||||||
|
|
||||||
const saveCsv = (window: Window, csv: string) => {
|
const SERVERS_FILENAME = 'shlink-servers.csv';
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default class ServersExporter {
|
export default class ServersExporter {
|
||||||
public constructor(
|
public constructor(
|
||||||
@@ -42,10 +21,9 @@ export default class ServersExporter {
|
|||||||
headers: keys(head(servers)).join(','),
|
headers: keys(head(servers)).join(','),
|
||||||
});
|
});
|
||||||
|
|
||||||
saveCsv(this.window, csv);
|
saveCsv(this.window, csv, SERVERS_FILENAME);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// FIXME Handle error
|
// FIXME Handle error
|
||||||
/* eslint no-console: "off" */
|
|
||||||
console.error(e);
|
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 appendChild = jest.fn();
|
||||||
const removeChild = jest.fn();
|
const removeChild = jest.fn();
|
||||||
const createWindowMock = (isIe10 = true) => Mock.of<Window>({
|
const createWindowMock = () => Mock.of<Window>({
|
||||||
navigator: {
|
|
||||||
msSaveBlob: isIe10 ? jest.fn() : undefined,
|
|
||||||
},
|
|
||||||
document: {
|
document: {
|
||||||
createElement: jest.fn(() => createLinkMock()),
|
createElement: jest.fn(() => createLinkMock()),
|
||||||
body: { appendChild, removeChild },
|
body: { appendChild, removeChild },
|
||||||
@@ -66,21 +63,9 @@ describe('ServersExporter', () => {
|
|||||||
expect(erroneousToCsv).toHaveBeenCalledTimes(1);
|
expect(erroneousToCsv).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('makes use of msSaveBlob API when available', () => {
|
it('makes use of download link API', () => {
|
||||||
const windowMock = createWindowMock();
|
const windowMock = createWindowMock();
|
||||||
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
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;
|
const { document: { createElement } } = windowMock;
|
||||||
|
|
||||||
exporter.exportServers();
|
exporter.exportServers();
|
||||||
|
|||||||
Reference in New Issue
Block a user