Created
June 7, 2017 21:32
-
-
Save JoeGermuska/351900ad2de1b75f4d2e1dea4f01f4cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const IdyllComponent = require('idyll-component'); | |
const http = require('https'); | |
const jsonPromise = ((url) => { | |
// via https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/ | |
// return new pending promise | |
return new Promise((resolve, reject) => { | |
const request = http.get(url, (response) => { | |
// handle http errors | |
if (response.statusCode < 200 || response.statusCode > 299) { | |
reject(new Error('Failed to load page, status code: ' + response.statusCode)); | |
} | |
// temporary data holder | |
const body = []; | |
// on every content chunk, push it to the data array | |
response.on('data', (chunk) => body.push(chunk)); | |
// we are done, resolve promise with those joined chunks | |
response.on('end', () => resolve(JSON.parse(body.join('')))); | |
}); | |
// handle connection errors of the request | |
request.on('error', (err) => reject(err)) | |
}) | |
}) | |
const fetchProfileData = ((geoid) => { | |
var url = `https://embed.censusreporter.org/1.0/data/profiles/2015/${geoid}.json`; | |
return jsonPromise(url); | |
}) | |
class CensusProfile extends IdyllComponent { | |
constructor(props) { | |
super(props); | |
console.log("CensusProfile constructor") | |
} | |
render() { | |
fetchProfileData(this.props.geoid).then( | |
(json) => { | |
return <h1>{json.geography.this.full_name}</h1> | |
} | |
).err((err) => { | |
console.error("Error getting profile", err); | |
return <div>ERROR</div> | |
}); | |
} | |
} | |
module.exports = CensusProfile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment