-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainView.js
More file actions
175 lines (155 loc) · 4.42 KB
/
MainView.js
File metadata and controls
175 lines (155 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React from 'react';
import Papa from 'papaparse';
import Navbar from './Navbar';
import DataTable from './DataTable';
import Search from './Search';
import Spinner from 'react-spinner';
import 'react-spinner/react-spinner.css';
import '../styles.css';
import WebpackDate from '../../webpack.date.json';
const propTypes = {
};
export default class MainView extends React.Component {
constructor(props) {
super(props);
this.state = {
data: undefined,
filteredData: undefined,
dataSource: '',
isError: false,
packdate: WebpackDate.date,
};
this.filterData = this.filterData.bind(this);
}
componentDidMount() {
this.import();
}
getQueryStringParam(name) {
let value = null;
const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
const results = regex.exec(window.location.href);
if (results) {
value = decodeURIComponent(results[2].replace(/\+/g, ' '));
}
return value;
}
getExternalFileUrl() {
return this.getQueryStringParam('externalFileUrl');
}
getLocalFileName() {
let file = this.getQueryStringParam('file');
if (!file) {
file = 'default.csv';
}
return file;
}
import() {
const localFolder = './csv/';
const localFileName = this.getLocalFileName();
let path = localFolder + localFileName;
const externalFileUrl = this.getExternalFileUrl();
if (externalFileUrl) {
path = externalFileUrl;
}
this.setState({
dataSource: path,
});
Papa.parse(path, {
download: true,
header: true,
dynamicTyping: true,
complete: results => {
this.parseCSV(results);
},
error: () => {
this.setState({
isError: true,
});
},
});
}
parseCSV(results) {
const rowData = results.data.filter((d) => Object.keys(d).length > 1);
this.setState({
data: rowData,
filteredData: rowData,
headers: Object.keys(rowData[0] || {}),
});
}
export(json) {
const filename = 'data.csv';
const csv = Papa.unparse(json);
const blob = new Blob([csv], { type: 'text/csv' });
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
}
else {
const a = window.document.createElement('a');
a.href = window.URL.createObjectURL(blob, {
type: 'text/plain',
});
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
filterData(data) {
this.setState({
filteredData: data,
});
}
render() {
const data = this.state.data;
const headers = this.state.headers;
const filteredData = this.state.filteredData;
const dataSource = encodeURI(this.state.dataSource);
const isError = this.state.isError;
const packdate = this.state.packdate;
return (
<div>
<Navbar />
<main className="container">
{(() => {
if (isError) {
return (<div className="row">
<div className="col-sm-12">
<div className="alert alert-danger alert-dismissible" role="alert">
<strong>Oh snap!</strong> We had some issues resolving the data
source: {dataSource}.
</div>
</div>
</div>);
}
if (!this.state.data) {
return <div className="row"><Spinner /></div>;
}
return (<div className="row">
<div className="col-sm-12">
<div className="row">
<div className="col-xs-8 col-sm-4">
<Search data={data} onFilteredData={this.filterData} />
</div>
<div className="col-xs-4 pull-right">
<a
className="btn btn-primary pull-right"
onClick={this.export.bind(this, filteredData)}
>Export to CSV</a>
</div>
</div>
<hr />
<div className="row">
<div className="col-xs-12">
<DataTable limit={20} values={filteredData} headers={headers} />
</div>
</div>
</div>
</div>);
})()}
<small>Packed: <span>{packdate}</span></small>
</main>
</div>
);
}
}
MainView.propTypes = propTypes;