-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
133 lines (120 loc) · 4.25 KB
/
app.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
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
'use strict';
// load more content
function loadMore() {
const paragraphs = document.querySelectorAll('#projects_items .load_more');
for (let i = 0; i < paragraphs.length; i++) {
paragraphs[i].classList.add('show');
}
const hr = document.querySelectorAll('.line_hr');
for (let i = 0; i < hr.length; i++) {
hr[i].classList.add('sh');
}
document.getElementById('load-more-button').style.display = 'none';
document.getElementById('hide-button').style.display = 'block';
}
function hide() {
const paragraphs = document.querySelectorAll('#projects_items .load_more');
for (let i = 0; i < paragraphs.length; i++) {
paragraphs[i].classList.remove('show');
}
const hr = document.querySelectorAll('.line_hr');
for (let i = 0; i < hr.length; i++) {
hr[i].classList.remove('sh');
}
document.getElementById('load-more-button').style.display = 'block';
document.getElementById('hide-button').style.display = 'none';
}
const members_url = "https://api.github.com/orgs/move-fast-and-break-things/members";
// fetch authors data
async function getAuthors(apiURL) {
const response = await fetch(apiURL);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return Promise.all(data.map(async (element) => {
const userResponse = await fetch(element.url);
if (!userResponse.ok) {
throw new Error(`HTTP error! status: ${userResponse.status}`);
}
const userData = await userResponse.json();
return {
name: userData.name || userData.login,
bio: userData.bio,
avatar: userData.avatar_url,
html_url: userData.html_url,
};
}));
}
// set authors in the DOM
async function setAuthors(apiURL) {
const authors = await getAuthors(apiURL);
const container = document.querySelector("#creators > div > div");
if (!container) {
throw new Error("Container element not found");
}
authors.forEach(author => {
const personDiv = document.createElement('div');
personDiv.className = 'person';
personDiv.innerHTML = `
<div class="name_and_img">
<img src="${author.avatar}" alt="${author.name}'s avatar">
<p class="person_name">${author.name}</p>
</div>
<p class="about_person">${author.bio || 'No biography available'}</p>
`;
container.appendChild(personDiv);
});
}
// set contributors in the DOM
async function setContributors(apiURL, element) {
try {
const contributors = await getAuthors(apiURL);
element.innerHTML = contributors.length === 0 ? "No developers found" :
contributors.map(contributor => `
<span class="developer-name">
<a href="${contributor.html_url}" class="tooltip" target="_blank">
<img src="${contributor.avatar}" alt="${contributor.name}'s avatar" class="shining-image">
<span class="tooltip-text">${contributor.name}</span>
</a>
</span>
`).join('');
} catch (error) {
console.error("Error setting contributors:", error);
element.innerHTML = "Can't load the developer list. Check out the repository page to see all of the contributors 😅";
}
}
// load all contributors
async function loadAllContributors() {
const developerLists = document.querySelectorAll(".developers-list");
const promises = Array.from(developerLists).map(devList => {
const apiURL = devList.getAttribute("data-value");
return setContributors(apiURL, devList);
});
await Promise.all(promises);
}
// Scroll-to-top functionality
function setupScrollToTop() {
const toTopButton = document.getElementById('toTop');
if (!toTopButton) return;
window.addEventListener('scroll', () => {
if (window.scrollY > 100) {
toTopButton.style.display = 'block';
} else {
toTopButton.style.display = 'none';
}
});
toTopButton.addEventListener('click', (e) => {
e.preventDefault();
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
// Wait for DOM content to be loaded before executing scripts
document.addEventListener('DOMContentLoaded', () => {
setAuthors(members_url).catch(console.error);
loadAllContributors();
setupScrollToTop();
});