-
Notifications
You must be signed in to change notification settings - Fork 9
/
footer.js
60 lines (53 loc) · 1.62 KB
/
footer.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
import * as idb from 'idb-keyval';
async function fetchGithubContributors(requestURL) {
let response;
if ('caches' in window) {
response = await caches.match(requestURL);
if (!response) {
response = await fetch(requestURL);
const cache = await caches.open(requestURL);
await cache.put(requestURL, response.clone());
}
} else {
// browser does not support the cache APi
response = await fetch(requestURL);
}
response = await fetch(requestURL);
const contributors = await response.json();
return contributors;
}
function createContributorsHtml(contributors) {
const contributorListElements = contributors.map((contributor)=>{
return `
<li>
<a href="${contributor.html_url}">
<img src="${contributor.avatar_url}" class='avatar-img'>
<div>${contributor.login}</div>
</a>
</li>
`;
}).join('');
return `
<span>A project by...</span>
<ul class='footer-list'>
${contributorListElements}
</ul>
`;
}
async function getContributorsHTML() {
const repo = "todaysCuriosity";
const owner = "mrhut10";
const requestURL = `https://api.github.com/repos/${owner}/${repo}/contributors`;
let contributorsHtml;
const lastContributors = await idb.get('LastGithubResponse');
if (lastContributors) {
contributorsHtml = createContributorsHtml(lastContributors);
} else {
const contributors = await fetchGithubContributors(requestURL);
contributorsHtml = createContributorsHtml(contributors);
idb.set('LastGithubResponse', contributors);
}
return contributorsHtml;
}
export default getContributorsHTML;
// getContributorsHTML();