Skip to content

Commit

Permalink
adds nested object notation to need urls
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Jun 26, 2017
1 parent 22ad235 commit d5e7fa3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app/pages/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Profile extends Component {
<CanonComponent data={this.props.data} d3plus={d3plus}>
<div className="home">
<h1>{ this.props.params.id === "040AF00182" ? "Nigeria" : "Ethopia" }</h1>
<p>Top Crop ID (from "preneed"): { topCrop }</p>
<p>{ topCrop } Competitors ("need" using "preneed" in URL): { competitors ? competitors.map(c => c.geo_name).join(", ") : "Loading" }</p>
<p>Top Crop ID (from "preneed"): { topCrop.crop }</p>
<p>{ topCrop.crop } Competitors ("need" using "preneed" in URL): { competitors ? competitors.map(c => c.geo_name).join(", ") : "Loading" }</p>
<TopicTitle slug="agriculture">Agriculture</TopicTitle>
<Child />
<TopicTitle slug="climate">Climate</TopicTitle>
Expand All @@ -44,12 +44,12 @@ class Profile extends Component {

const topCropUrl = "https://api.dataafrica.io/api/join/?show=year,crop&sumlevel=latest_by_geo,lowest&required=harvested_area&order=harvested_area&sort=desc&display_names=true&geo=<id>&limit-1";
Profile.preneed = [
fetchData("topCrop", topCropUrl, res => dataFold(res)[0].crop)
fetchData("topCrop", topCropUrl, res => dataFold(res)[0])
];

Profile.need = [
Child, Child2,
fetchData("competitors", "api/join/?show=geo&sumlevel=adm0&crop=<topCrop>&required=harvested_area&order=harvested_area&sort=desc&display_names=true"),
fetchData("competitors", "api/join/?show=geo&sumlevel=adm0&crop=<topCrop.crop>&required=harvested_area&order=harvested_area&sort=desc&display_names=true"),
fetchData("value_of_production", "api/join/?geo=<id>&show=crop&required=harvested_area,value_of_production&order=value_of_production&sort=desc&display_names=true&limit=5")
];

Expand Down
10 changes: 5 additions & 5 deletions src/actions/fetchData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ function fetchData(key, url, format = dataFold, config = {}) {
let u = url.indexOf("http") === 0 ? url : `${store.API}${url}`;

(url.match(/<[^\&\=\/>]+>/g) || []).forEach(variable => {
let x = variable.slice(1, -1);
if (params[x]) x = params[x];
else if (store.data && store.data[x]) x = store.data[x];
else if (store[x]) x = store[x];
let x = variable.slice(1, -1).split(".");
if (params[x[0]]) x = params[x[0]];
else if (store.data && store.data[x[0]]) x = x.reduce((o, i) => o[i], store.data);
else if (store[x[0]]) x = x.reduce((o, i) => o[i], store);
else x = false;
if (x) u = u.replace(variable, x);
if (x && typeof x !== "object") u = u.replace(variable, x);
});

return {
Expand Down

0 comments on commit d5e7fa3

Please sign in to comment.