tubbsfireinfo.com/app.js

129 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2017-10-10 18:45:54 +00:00
require('whatwg-fetch')
2017-10-10 18:51:50 +00:00
require('./styles.scss')
2017-10-10 18:45:54 +00:00
const React = require('react')
const ReactDOM = require('react-dom')
2017-10-10 20:12:13 +00:00
function fetchResources(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => {
return response.json()
})
.then(json => {
console.log('RESPONSE:', json)
resolve(json)
})
.catch(error => {
reject(error)
console.log('ERROR:', error)
})
})
}
class SmartTable extends React.Component {
2017-10-10 18:45:54 +00:00
constructor(props) {
super(props)
this.state = {
2017-10-10 20:12:13 +00:00
error: null,
2017-10-10 18:45:54 +00:00
loading: false,
items: [],
}
}
2017-10-10 20:12:13 +00:00
async componentWillMount() {
this.setState({ ...this.state, error: null, loading: true })
try {
const news = await fetchResources(this.props.url)
this.setState({ ...this.state, items: news, error: null, loading: false })
} catch (error) {
this.setState({ ...this.state, error: error })
console.error('ERROR:', error)
}
2017-10-10 18:45:54 +00:00
}
render() {
2017-10-10 20:12:13 +00:00
return (
<Table
error={this.state.error}
items={this.state.items}
loading={this.state.loading}
/>
)
2017-10-10 18:45:54 +00:00
}
}
2017-10-10 20:12:13 +00:00
function Table({ error, loading, items }) {
if (error) {
return (
<p className="lead text-center text-danger">
<i className="fa fa-warning fa-spin mr-3" /> Sorry, we had an issue
loading results, please try again in a few moments.
</p>
)
}
2017-10-10 18:45:54 +00:00
if (loading) {
return (
<p className="lead text-center">
2017-10-10 20:12:13 +00:00
<i className="fa fa-spinner fa-spin mr-3" /> Loading...
2017-10-10 18:45:54 +00:00
</p>
)
}
2017-10-10 20:12:13 +00:00
const columns = Object.values(items[0].column_mappings)
2017-10-10 18:45:54 +00:00
return (
2017-10-10 20:12:13 +00:00
<table className="table table-hover">
<thead>
<tr>{columns.map((col, key) => <th key={key}>{col}</th>)}</tr>
</thead>
<tbody>
{items.map((item, key) => (
<Row item={item} columns={columns} key={key} />
))}
</tbody>
</table>
2017-10-10 18:45:54 +00:00
)
}
2017-10-10 20:12:13 +00:00
function formatCell(col, value) {
// Date
2017-10-10 20:43:21 +00:00
if (col.toLowerCase() === 'last updated' && value) {
2017-10-10 20:12:13 +00:00
const date = new Date(value)
return `${date.getMonth() +
1}/${date.getDate()} at ${date.getHours()}:${date.getMinutes()}`
}
return value
}
function Row({ columns, item }) {
2017-10-10 18:45:54 +00:00
return (
2017-10-10 20:12:13 +00:00
<tr>
{columns.map((col, key) => {
const cell = formatCell(col, item.fields[col])
return <td key={key}>{cell}</td>
})}
</tr>
2017-10-10 18:45:54 +00:00
)
}
2017-10-10 20:12:13 +00:00
ReactDOM.render(
<SmartTable url="http://app.tubbsfireinfo.com/recent_news.json" />,
2017-10-10 20:59:44 +00:00
document.getElementById('resource-table')
2017-10-10 20:12:13 +00:00
)
ReactDOM.render(
<SmartTable url="http://app.tubbsfireinfo.com/gas_stations.json" />,
2017-10-10 20:59:44 +00:00
document.getElementById('gas-stations-table')
2017-10-10 20:12:13 +00:00
)
ReactDOM.render(
<SmartTable url="http://app.tubbsfireinfo.com/markets.json" />,
2017-10-10 20:59:44 +00:00
document.getElementById('markets-table')
2017-10-10 20:12:13 +00:00
)
2017-10-10 20:43:21 +00:00
ReactDOM.render(
<SmartTable url="http://app.tubbsfireinfo.com/shelters.json" />,
2017-10-10 20:59:44 +00:00
document.getElementById('shelters-table')
2017-10-10 20:43:21 +00:00
)