require('whatwg-fetch') require('./styles.scss') const React = require('react') const ReactDOM = require('react-dom') 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 { constructor(props) { super(props) this.state = { error: null, loading: false, items: [], } } 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) } } render() { return ( ) } } function Table({ error, loading, items }) { if (error) { return (

Sorry, we had an issue loading results, please try again in a few moments.

) } if (loading) { return (

Loading...

) } const columns = Object.values(items[0].column_mappings) return (
{columns.map((col, key) => )} {items.map((item, key) => ( ))}
{col}
) } function formatCell(col, value) { // Date if (col.toLowerCase() === 'last updated' && value) { const date = new Date(value) return `${date.getMonth() + 1}/${date.getDate()} at ${date.getHours()}:${date.getMinutes()}` } return value } function Row({ columns, item }) { return ( {columns.map((col, key) => { const cell = formatCell(col, item.fields[col]) return {cell} })} ) } ReactDOM.render( , document.getElementById('resource-table') ) ReactDOM.render( , document.getElementById('gas-stations-table') ) ReactDOM.render( , document.getElementById('markets-table') ) ReactDOM.render( , document.getElementById('shelters-table') )