moved csv loading to a separate component where the state is to be maintained

master
Malar Kannan 2018-06-23 18:36:50 +05:30
parent 7960e4c9d1
commit e45fb70b8b
3 changed files with 43 additions and 28 deletions

View File

@ -1,33 +1,12 @@
import * as React from 'react'; import * as React from 'react';
import './App.css'; import './App.css';
import 'react-tabs/style/react-tabs.css'; import ServerAnalytics from './ServerAnalytics';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import { ServerTable } from './ServerTable';
import { Chart } from './Chart';
// import logo from './logo.svg';
class App extends React.Component { class App extends React.Component {
public render() { public render() {
return ( return (
<div className="App"> <div className="App">
<Tabs> <ServerAnalytics/>
<TabList>
<Tab>
Server Table
</Tab>
<Tab>Charts</Tab>
</TabList>
<TabPanel>
<ServerTable />
</TabPanel>
<TabPanel>
<Chart />
</TabPanel>
</Tabs>
</div> </div>
); );
} }

28
src/ServerAnalytics.tsx Normal file
View File

@ -0,0 +1,28 @@
import * as React from 'react';
import 'react-tabs/style/react-tabs.css';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import { ServerTable } from './ServerTable';
import { Chart } from './Chart';
export default class ServerAnalytics extends React.Component {
public render() {
return (
<div className="App">
<Tabs>
<TabList>
<Tab>
Server Table
</Tab>
<Tab>Charts</Tab>
</TabList>
<TabPanel>
<ServerTable csvPath='/data.csv'/>
</TabPanel>
<TabPanel>
<Chart />
</TabPanel>
</Tabs>
</div>
);
}
}

View File

@ -1,5 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import { Tips, Logo } from "./Utils"; import { Tips } from "./Utils";
import * as Papa from 'papaparse'; import * as Papa from 'papaparse';
const reactcsv = require('react-csv'); const reactcsv = require('react-csv');
const CSVLink = reactcsv.CSVLink; const CSVLink = reactcsv.CSVLink;
@ -15,12 +15,18 @@ export class ServerTable extends React.Component<any, any> {
headers: [], headers: [],
data: [] data: []
}; };
this.loaded = this.loaded.bind(this); const loaded = (results: any) => {
Papa.parse('/data.csv', { const localstate = this;
localstate.setState({
headers: results.meta.fields,
data: results.data
});
}
Papa.parse(this.props.csvPath, {
header: true, header: true,
download: true, download: true,
skipEmptyLines: true, skipEmptyLines: true,
complete: this.loaded complete: loaded
}); });
} }
@ -46,11 +52,13 @@ export class ServerTable extends React.Component<any, any> {
columns={headerCols} columns={headerCols}
defaultPageSize={20} defaultPageSize={20}
className="-striped -highlight" className="-striped -highlight"
style={{
height: "450px"
}}
/> />
<br /> <br />
<CSVLink data={data} filename={"data.csv"}>Click here to download CSV file</CSVLink> <CSVLink data={data} filename={"data.csv"}>Click here to download CSV file</CSVLink>
<Tips /> <Tips />
<Logo />
</div> </div>
); );
} }