forked from Technigo/project-github-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (168 loc) · 5.78 KB
/
Copy pathscript.js
File metadata and controls
199 lines (168 loc) · 5.78 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const userContainer = document.getElementById('userInfo');
const reposContainer = document.getElementById('projects');
const reposSubContainer = document.getElementById('project-box');
const navSub = document.getElementById('nav-sub');
const input = document.getElementById('navInput');
const inputMobile = document.getElementById('navInputMobile');
const githubImg = document.getElementById('nav-github-img');
const turnLightMode = document.getElementById('btn-light');
const turnLightModeMobile = document.getElementById('btn-light-nav');
// for removing previous chart
let chartDrawn = false;
// shows my profile as default profile upon loading the website
showMyProfileOnLoad = () => {
let userName = 'loulunds';
getUserData(userName);
fetchRepos(userName);
};
window.onload = showMyProfileOnLoad;
// for getting user data
const getUserData = (user) => {
fetch(`https://api.github.com/users/${user}`)
.then((response) => response.json())
.then((data) => {
userContainer.innerHTML = ``;
if (data.login !== undefined) {
userContainer.innerHTML = `
<div class="user-profile-box">
<div class="user-info-box">
<h1 class="user-username"> <span id="userSpan">${
data.login
}</span></h1>
<h2 class="user-fullname"> <span id="userSpanSub">${
data.name
}</span></h2>
<h3 class="user-location"> <img class="user-pin-img" src="images/pin-map.png" alt="pin"/><span class="user-span">${
data.location
}</span></h3>
<h3 class="user-join"> Member since: ${new Date(data.created_at)
.toDateString()
.slice(4)}</h3>
</div>
<div class="user-img-box">
<a href="${data.html_url}" target="_blank">
<img class="user-img" src="${data.avatar_url}"/>
</a>
</div>
`;
} else {
userContainer.innerHTML = `
<p class="error-message"> API rate limit exceeded. Failed to Load. Come back later.</p>
`;
}
})
.catch(() => {
userContainer.innerHTML = `<h3>Sorry, we could not find the information</h3>`;
});
};
// for fetching the repos of the user
const fetchRepos = (user) => {
fetch(`https://api.github.com/users/${user}/repos`)
.then((response) => response.json())
.then((data) => {
reposSubContainer.innerHTML = ``;
// filters the repos that are only from Technigo
const forkedRepos = data.filter(
(repo) => repo.fork && repo.name.startsWith('project-')
);
// sorts the repos from oldest to newest
forkedRepos.sort((a, b) => {
return new Date(a.pushed_at) - new Date(b.pushed_at);
});
forkedRepos.forEach(
(repo) =>
(reposSubContainer.innerHTML += `
<div class="repo-box">
<a href="${
repo.html_url
}" target="blank"> <h3 class="repo-name"><img class="repo-book-img"src="images/book.png" alt="book"/>
${repo.name}</h3>
</a>
<p>Default branch <span class="repo-branch">${
repo.default_branch
}</span></p>
<p>Language: <span class="repo-language">${repo.language}</span></p>
<p>Recent push: <span class="repo-date">${new Date(
repo.pushed_at
).toDateString()}</span></p>
<p>Commits:<span id="commit-${
repo.name
}" class="repo-commit"> </span></p>
</div>
`)
);
// renders the chart
if (!chartDrawn) {
drawChart(forkedRepos.length);
chartDrawn = true;
}
getPullRequests(forkedRepos);
});
};
//Get all the PRs for each project.
const getPullRequests = (repos) => {
repos.forEach((repo) => {
fetch(
`https://api.github.com/repos/technigo/${repo.name}/pulls?per_page=100`
)
.then((res) => res.json())
.then((data) => {
// filters user's pull requests
const filteredPull = data.find(
(pull) => pull.user.login === repo.owner.login
);
if (filteredPull) {
showCommits(filteredPull.commits_url, repo.name);
} else {
document.getElementById(`commit-${repo.name}`).innerHTML =
' No pull request from me';
}
});
});
};
// shows number of commits
const showCommits = (url, myRepoName) => {
fetch(url)
.then((res) => res.json())
.then((data) => {
document.getElementById(`commit-${myRepoName}`).innerHTML += data.length;
});
};
// event listener
input.addEventListener('keypress', (event) => {
if (event.key === 'Enter' && input.value) {
let userName = '';
userName = input.value;
getUserData(userName);
fetchRepos(userName);
}
});
// for mobile input
inputMobile.addEventListener('keypress', (event) => {
if (event.key === 'Enter' && inputMobile.value) {
let userName = '';
userName = inputMobile.value;
getUserData(userName);
fetchRepos(userName);
toggleNav();
}
});
// turns the light mode on
const toLightMode = () => {
document.body.classList.toggle('light');
document.getElementById('userSpan').classList.toggle('user-span-light');
document.getElementById('userSpanSub').classList.toggle('user-span-light');
};
turnLightMode.addEventListener('click', (e) => {
toLightMode();
});
turnLightModeMobile.addEventListener('click', (e) => {
toLightMode();
});
// toggles the mobile-nav menu
const toggleNav = () => {
navSub.classList.toggle('active');
};
navSub.addEventListener('click', (e) => {
toggleNav();
});