diff --git a/app.js b/app.js new file mode 100644 index 0000000..0994687 --- /dev/null +++ b/app.js @@ -0,0 +1,31 @@ +// Init Github +const github = new Github; +// Init UI +const ui = new UI; + +// Search input +const searchUser = document.getElementById('searchUser'); + +// Search input event listener +searchUser.addEventListener('keyup', (e) => { + // Get input text + const userText = e.target.value; + + if(userText != '') { + // Make HTTP call + github.getUser(userText) + .then(data => { + if(data.profile.message === 'Not Found') { + // Show alert + ui.showAlert('User not found', 'alert alert-danger'); + } else { + // Show profile + ui.showProfile(data.profile); + ui.showRepos(data.repos); + } + }) + } else { + // Clear profile + ui.clearProfile(); + } +}); \ No newline at end of file diff --git a/github.js b/github.js new file mode 100644 index 0000000..e6aadf3 --- /dev/null +++ b/github.js @@ -0,0 +1,22 @@ +class Github { + constructor() { + this.client_id = '8e6fe0bcd1150b14274d'; + this.client_secret = '670646345f9a4f437a323f1b77479670c9f6f050'; + this.repos_count = 5; + this.repos_sort = 'created: asc'; + } + + async getUser(user) { + const profileResponse = await fetch(`https://api.github.com/users/${user}?client_id=${this.client_id}&client_secret=${this.client_secret}`); + + const repoResponse = await fetch(`https://api.github.com/users/${user}/repos?per_page=${this.repos_count}&sort=${this.repos_sort}&client_id=${this.client_id}&client_secret=${this.client_secret}`); + + const profile = await profileResponse.json(); + const repos = await repoResponse.json(); + + return { + profile, + repos + } + } +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..1874bc3 --- /dev/null +++ b/index.html @@ -0,0 +1,38 @@ + + +
+ + + + +Enter a username to fetch a user profile and repos
+ +