Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Week2/lecture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//promises.them and catch
//build small app that shows ransom dogs and cats
// try and catch

//event loop
109 changes: 71 additions & 38 deletions homework/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,79 @@
'use strict';

{
function fetchJSON(url, cb) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status <= 299) {
cb(null, xhr.response);
} else {
cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
}
};
xhr.onerror = () => cb(new Error('Network request failed'));
xhr.send();
}

function createAndAppend(name, parent, options = {}) {
const elem = document.createElement(name);
parent.appendChild(elem);
Object.keys(options).forEach(key => {
const value = options[key];
if (key === 'text') {
elem.textContent = value;
} else {
elem.setAttribute(key, value);
}
});
return elem;
function createAndAppend(name, parent, options = {}) {
const elem = document.createElement(name);
parent.appendChild(elem);
Object.keys(options).forEach(key => {
const value = options[key];
if (key === 'text') {
elem.textContent = value;
} else {
elem.setAttribute(key, value);
}
});
return elem;
}



async function main(url) {
try {
const response = await fetch(url);
const json = await response.json();
const root = document.getElementById('root');
const select = createAndAppend('select', root, { class: 'select' });
createAndAppend('option', select, { text: 'Choose your favorite repo' });
let sorted = json.sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()));

sorted.forEach(repo => {
const name = repo.name;
createAndAppend('option', select, { text: name });
})
const wraper = createAndAppend('div', root, { class: 'wraper' });
const repoInfo = createAndAppend('div', wraper, { class: 'repoinfo' });
const contribs = createAndAppend('div', wraper, { class: 'contribs' });

select.addEventListener('change', evt => {
const selectedRepo = evt.target.value;
const repo = json.filter(r => r.name == selectedRepo)[0];
getContributorInformation();
repoInfo.innerHTML = '';
contribs.innerHTML = '';
const addInfo = (label, value) => {
const container = createAndAppend('div', repoInfo, { class: 'container' });
createAndAppend('span', container, { text: label });
createAndAppend('span', container, { text: value });
};
addInfo('Name: ', repo.name);
addInfo('Desciption: ', repo.description);
addInfo('Number of forks: ', repo.forks);
addInfo('Updated: ', repo.updated_at)

})

}
catch (err) {
const root = document.getElementById('root');
createAndAppend('div', root, { text: err.message, class: 'alert-error' })
}

function main(url) {
fetchJSON(url, (err, repositories) => {
async function getContributorInformation(data) {
try {
const contribs = await fetch(data.contributors_url);
const jsonData = await contribs.json();


jsonData.forEach(contributor => {
createAndAppend('div', contribs, { text: contributor.login, class: 'contributor' })
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contribs here isn't what you expect. It's the promise from line 63 and not the dom-element from line 35, you need to get it from the dom again (like you do on line 35) and use it in this function.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this. but now the contrinutors_url is undefined!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related! Have a look at how you call getContributorInformation and what arguments it requires

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I get your point. I forgot to put the argument 'data' in the getContributorInformation function.
I should figure out what to put there !

createAndAppend('img', contribs, { src: contributor.avatar_url, height: 150, widtth: 150, id: 'img' })
createAndAppend('div', contribs, { text: contributor.contributions })
})
} catch (err) {
const root = document.getElementById('root');
if (err) {
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
return;
}
createAndAppend('pre', root, { text: JSON.stringify(repositories, null, 2) });
});
createAndAppend('div', contribs, { text: err.message, class: 'alert-error' })
}
}

const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => main(HYF_REPOS_URL);
}
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => main(HYF_REPOS_URL);
3 changes: 0 additions & 3 deletions homework/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
.alert-error {
color: red;
}