Fixed headers when exporting visits to CSV

This commit is contained in:
Alejandro Celaya 2021-03-14 18:12:10 +01:00
parent 843f646264
commit 71468379bd
5 changed files with 7 additions and 14 deletions

View file

@ -10,7 +10,7 @@ declare module 'event-source-polyfill' {
declare module 'csvjson' { declare module 'csvjson' {
export declare class CsvJson { export declare class CsvJson {
public toObject<T>(content: string): T[]; public toObject<T>(content: string): T[];
public toCSV<T>(data: T[], options: { headers: string }): string; public toCSV<T>(data: T[], options: { headers: 'full' | 'none' | 'relative' | 'key' }): string;
} }
} }

View file

@ -1,4 +1,4 @@
import { dissoc, head, keys, values } from 'ramda'; import { dissoc, 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';
@ -17,9 +17,7 @@ export default class ServersExporter {
const servers = values(this.storage.get<ServersMap>('servers') ?? {}).map(dissoc('id')); const servers = values(this.storage.get<ServersMap>('servers') ?? {}).map(dissoc('id'));
try { try {
const csv = this.csvjson.toCSV(servers, { const csv = this.csvjson.toCSV(servers, { headers: 'key' });
headers: keys(head(servers)).join(','),
});
saveCsv(this.window, csv, SERVERS_FILENAME); saveCsv(this.window, csv, SERVERS_FILENAME);
} catch (e) { } catch (e) {

View file

@ -2,7 +2,7 @@ import { isEmpty, propEq, values } from 'ramda';
import { useState, useEffect, useMemo, FC } from 'react'; import { useState, useEffect, useMemo, FC } from 'react';
import { Button, Card, Nav, NavLink, Progress, Row } from 'reactstrap'; import { Button, Card, Nav, NavLink, Progress, Row } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCalendarAlt, faMapMarkedAlt, faList, faChartPie, faFileExport } from '@fortawesome/free-solid-svg-icons'; import { faCalendarAlt, faMapMarkedAlt, faList, faChartPie, faFileDownload } from '@fortawesome/free-solid-svg-icons';
import { IconDefinition } from '@fortawesome/fontawesome-common-types'; import { IconDefinition } from '@fortawesome/fontawesome-common-types';
import { Route, Switch, NavLink as RouterNavLink, Redirect } from 'react-router-dom'; import { Route, Switch, NavLink as RouterNavLink, Redirect } from 'react-router-dom';
import { Location } from 'history'; import { Location } from 'history';
@ -274,7 +274,7 @@ const VisitsStats: FC<VisitsStatsProps> = (
className="btn-md-block" className="btn-md-block"
onClick={() => exportCsv(normalizedVisits)} onClick={() => exportCsv(normalizedVisits)}
> >
Export ({normalizedVisits.length}) <FontAwesomeIcon icon={faFileExport} /> <FontAwesomeIcon icon={faFileDownload} /> Export ({normalizedVisits.length})
</Button> </Button>
</div> </div>
</div> </div>

View file

@ -13,10 +13,7 @@ export class VisitsExporter {
return; return;
} }
const [ firstVisit ] = visits; const csv = this.csvjson.toCSV(visits, { headers: 'key' });
const csv = this.csvjson.toCSV(visits, {
headers: Object.keys(firstVisit).join(','),
});
saveCsv(this.window, csv, filename); saveCsv(this.window, csv, filename);
}; };

View file

@ -44,9 +44,7 @@ describe('VisitsExporter', () => {
exporter.exportVisits('my_visits.csv', visits); exporter.exportVisits('my_visits.csv', visits);
expect(toCSV).toHaveBeenCalledWith(visits, { expect(toCSV).toHaveBeenCalledWith(visits, { headers: 'key' });
headers: 'browser,city,country,date,latitude,longitude,os,referer',
});
}); });
it('skips execution when list of visits is empty', () => { it('skips execution when list of visits is empty', () => {