Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds csv viewer #3

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
"webpack-dev-server": "^2.4.5"
},
"dependencies": {
"comma-separated-values": "^3.6.4",
"react": "^15.5.4",
"react-data-grid": "^2.0.42",
"react-dom": "^15.5.4"
}
}
2 changes: 1 addition & 1 deletion scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ process.on("unhandledRejection", err => {
throw err;
});

const PORT = 8080;
const PORT = 8081;

const webpack = require("webpack");
const WebpackDevServer = require("webpack-dev-server");
Expand Down
3 changes: 2 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
import FileViewer from './components/file-viewer.jsx';

ReactDOM.render(
<FileViewer />,
<FileViewer fileType="csv"
filePath="http://spatialkeydocs.s3.amazonaws.com/FL_insurance_sample.csv"/>,
document.getElementById('app')
);
49 changes: 49 additions & 0 deletions src/components/drivers/csv-viewer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { Component } from 'react';

import ReactDataGrid from "react-data-grid";
import CSV from "comma-separated-values";

const rowGetter = data => i => data.rows[i];

const parse = data => {
const rows = [];
const columns = [];

new CSV(data).forEach(array => {
if (columns.length < 1) {
array.forEach((cell, idx) => {
columns.push({
key: `key-${idx}`,
name: cell,
resizable: true,
sortable: true,
filterable: true,
});
});
} else {
const row = {};
array.forEach((cell, idx) => {
row[`key-${idx}`] = cell;
});
rows.push(row);
}
});

return { rows, columns };
};


const CsvViewer = props => {
const data = parse(props.data)

return (
<ReactDataGrid
columns={data.columns}
rowsCount={data.rows.length}
rowGetter={rowGetter(data)}
minHeight={650}
/>
);
};

export default CsvViewer;
1 change: 1 addition & 0 deletions src/components/drivers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as CsvViewer } from "./csv-viewer.jsx";
50 changes: 50 additions & 0 deletions src/components/fetch-wrapper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { Component } from 'react';

function withFetching(WrappedComponent, props) {
return class extends Component {
constructor(props) {
super(props);
this.state = { data: {} };
this.xhr = this.createRequest()
}

componentDidMount() {
this.fetch(this.props.filePath);
}

componentWillUnmount() {
this.abort()
}

render() {
if (this.state.data.length > 0 || Object.keys(this.state.data).length > 0) {
return <WrappedComponent data={this.state.data} {...this.props} />;
} else {
return <h1>Loading..</h1>
}
}

createRequest() {
const xhr = new XMLHttpRequest();

xhr.onreadystatechange = () => {
if (this.xhr.readyState == 4 && this.xhr.status == 200) {
this.setState({ data: xhr.responseText })
}
}

return xhr;
}

fetch(path) {
this.xhr.open("GET", path, true);
this.xhr.send();
}

abort() {
this.xhr && this.xhr.abort();
}
}
};

export default withFetching;
27 changes: 25 additions & 2 deletions src/components/file-viewer.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import React, { Component } from 'react';

export default class FileViewer extends Component {
import { default as withFetching } from "./fetch-wrapper.jsx";
import { CsvViewer } from "./drivers";

class FileViewer extends Component {
render() {
let Driver = this.getDriver(this.props.fileType);

return (
<div className='file-viewer'>
Hello, file viewer!
<Driver {...this.props} />
</div>
);
}

getDriver(fileTYpe) {
switch (fileTYpe) {
case "csv": {
return withFetching(CsvViewer)
}
default: {
return <h1>File type is not supported</h1>
}
}
}
};

FileViewer.propTypes = {
fileType: React.PropTypes.string.isRequired,
filePath: React.PropTypes.string.isRequired,
}

export default FileViewer;