-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchDetails.js
49 lines (45 loc) · 1.46 KB
/
fetchDetails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import FetchWrapper from "./fetchWrapper.js";
import {renderRepo, renderUserData, resetYear, renderSelectYear, yearSelect} from "./renderUI.js";
import {renderChart, repoYearList} from './chartRender.js';
const fetchAPI = new FetchWrapper("https://api.github.com");
export const getButton = document.querySelector("#getApiBtn");
export const userInput = document.querySelector("#userName");
export let gitDataList = [];
export const fetchData=(username)=>{
fetchAPI.get(`/users/${username}/repos`)
.then(data=>{
if(data.length === 0){
resetYear();
throw 'User yet not created any repository.'
}else if(data.message === 'Not Found'){
resetYear();
throw 'User Not Found';
}else{
gitDataList = data;
renderRepo(data);
renderChart(data);
renderSelectYear(repoYearList);
}
}).catch(err=>{
if(err === 'User Not Found'){
userList.innerHTML = `<div class="alert alert-danger">${err}. Please enter valid User Name.</div>`;
userInput.focus();
}else{
userList.innerHTML = `<div class="alert alert-warning">${err}</div>`;
}
}).finally(()=>{
getButton.innerHTML = "Get Repo Data";
getButton.removeAttribute("disabled");
yearSelect.removeAttribute("disabled");
});
// fetch user details.
fetchAPI.get(`/users/${username}`)
.then(userData=>{
if(userData.message === 'Not Found'){
userInfo.innerHTML = "";
throw 'User Not Found';
}else{
renderUserData(userData);
}
})
}