I am currently working on a simple app to check the stats of popular javascript frameworks. For this I am using the Github API to get certain data sets and have come close to finishing my project.
const frameworks = [
{
name: "angular",
logo: "img/angular.png",
url: "https://api.github.com/repos/angular/angular.js"
}
/*
{
name: "ember",
logo: "img/ember.png",
url: "https://api.github.com/repos/emberjs/ember.js"
},
{
name: "react",
logo: "img/react.png",
url: "https://api.github.com/repos/facebook/react"
},
{
name: "vue",
logo: "img/vue.png",
url: "https://api.github.com/repos/vuejs/vue"
}
*/
];
const frameworkData = url => {
return fetch(url)
.then(resp => {
return resp.json();
})
.then(data => {
return {
data: {
name: data.name,
forks: data.forks_count,
stars: data.stargazers_count,
issues: data.open_issues_count
}
};
});
};
const setup = () => {
let now = new Date();
const row = document.getElementById("row");
frameworks.forEach(framework => {
const promises = [];
promises.push(frameworkData(framework.url));
Promise.all(promises).then(data => {
const obj = data[0].data;
console.log(obj);
const card = `
<div class="col-lg-3">
<div class="card" style="width: 18rem;">
<img src="${
framework.logo
}" alt="Card image cap" height="180" width="200">
<div class="card-body">
<h5 class="card-title">${framework.name}</h5>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Watchers: ${obj.forks}</li>
<li class="list-group-item">Stars: ${obj.stars}</li>
<li class="list-group-item">Commits: ${obj.issues}</li>
</ul>
</div>
</div>`;
row.innerHTML += card;
});
});
};
setup();
Being this is one of my first javascript 'apps' I feel it was done to the best of my ability. However, I feel there is a code smell in here that I have been trying to figure out how to fix but have not.
As you may be able to see in the frameworks for each loop, I am settings this variable const obj = data[0].data;. To me this is strange and I could not seem to find away around it. This is done because the single object for that API call is being returned in an array and I was not sure how to get that data other than calling the first index on the array since it will always be 0. Is there a better way to do this? How was my first javascript app?