server_table/src/ServerTable.tsx

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-06-23 07:57:40 +00:00
import * as React from 'react';
import { Tips } from "./Utils";
import * as _ from 'lodash';
2018-07-01 14:12:00 +00:00
import { Button } from 'bloomer';
const randomColor = require('randomcolor');
2018-06-23 08:20:08 +00:00
const reactcsv = require('react-csv');
const CSVLink = reactcsv.CSVLink;
2018-06-23 17:32:35 +00:00
import * as Papa from 'papaparse';
2018-06-23 07:57:40 +00:00
// Import React Table
import ReactTable from 'react-table';
import "react-table/react-table.css";
2018-06-23 17:32:35 +00:00
export default class ServerTable extends React.Component<any, any> {
2018-06-23 07:57:40 +00:00
constructor(props: any) {
super(props);
2018-06-23 17:32:35 +00:00
this.state = {
headers: [], data: [], colColorMap: {}
2018-06-23 17:32:35 +00:00
}
const loaded = (results: any) => {
const local = this;
const colsUnique = _.keys(_.groupBy(results.data, (o: any) => _.get<any>(o, this.props.colorColumn)));
const colColorMap = _.zipObject(colsUnique, _.map(colsUnique, () => randomColor()));
local.setState({
2018-06-23 17:32:35 +00:00
headers: results.meta.fields,
data: results.data,
colColorMap
2018-06-23 17:32:35 +00:00
});
}
2018-06-23 18:15:35 +00:00
Papa.parse(this.props.csvFile, {
2018-06-23 17:32:35 +00:00
header: true,
download: true,
skipEmptyLines: true,
dynamicTyping: true,
complete: loaded
});
2018-06-23 07:57:40 +00:00
}
2018-06-23 07:57:40 +00:00
public render() {
2018-06-23 17:32:35 +00:00
const data = this.state.data;
const headers = this.state.headers;
2018-06-23 07:57:40 +00:00
const filterPart = (filter: any, row: any) =>
String(row[filter.id]).startsWith(filter.value) ||
String(row[filter.id]).endsWith(filter.value)
2018-06-23 08:20:08 +00:00
const headerGen = (headerName: string) => {
// const title = headerName
// .replace(/([A-Z])/g, ' $1')
// .replace(/^./, (str: string) => str.toUpperCase());
2018-06-23 08:20:08 +00:00
return {
Header: headerName,
accessor: headerName,
Cell: (row: any) => {
return row.value;
}
2018-06-23 08:20:08 +00:00
}
}
const colorCol = this.props.colorColumn;
const colColorMap = this.state.colColorMap;
2018-07-01 14:12:00 +00:00
const rowStyle = (_0: any, rowInfo: any, _1: any) => {
if (_.isUndefined(rowInfo) || _.isUndefined(colorCol)) {
return {};
}
const colValue = _.get<any>(rowInfo.row, colorCol, '');
const colColor = _.get<any>(colColorMap, colValue, 'green');
return {
style: {
background: colColor
}
};
};
2018-06-23 08:20:08 +00:00
const headerCols = headers.map(headerGen);
2018-06-23 07:57:40 +00:00
return (
<ReactTable
data={data}
filterable={true}
defaultFilterMethod={filterPart}
columns={headerCols}
2018-06-23 18:15:35 +00:00
showPagination={false}
defaultPageSize={100}
className="-striped -highlight"
getTrProps={rowStyle}
style={{
2018-07-01 14:12:00 +00:00
height: "75vh"
}}
>
2018-06-23 17:32:35 +00:00
{(state, makeTable, ) => {
const lastFiltered = state.sortedData.map((o: any) => _.pick(o, headers));
return (
<div>
{makeTable()}
2018-06-23 18:15:35 +00:00
{/* <Chart data={lastFiltered} /> */}
2018-07-01 14:12:00 +00:00
<Button>
<CSVLink data={lastFiltered} filename={"data.csv"}>
Download CSV
</CSVLink>
</Button>
<br />
<Tips />
</div>
)
}}
</ReactTable>
2018-06-23 07:57:40 +00:00
);
}
public loaded(results: any) {
this.setState({
2018-06-23 08:20:08 +00:00
headers: results.meta.fields,
data: results.data
});
}
2018-06-23 07:57:40 +00:00
}