-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (92 loc) · 2.94 KB
/
index.js
File metadata and controls
102 lines (92 loc) · 2.94 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
function getPathWithoutFile() {
const path = window.location.pathname;
const segments = path.split('/');
const lastSegment = segments[segments.length - 1];
if (lastSegment.includes('.')) {
segments.pop();
}
return segments.join('/') || '/';
}
const pathname = getPathWithoutFile();
const upperCaseEveryWord = (word) =>
word
.split('-')
.filter((word) => word !== 'main' && word !== 'master')
.map((word) => `${word[0].toUpperCase() + word.slice(1)}`)
.join(' ');
const frontendmentor = (name, url) => {
let newName = name
.split('-')
.filter((word) => word !== 'main' && word !== 'master')
.join('-');
newName = newName.replace(/(\d)-/, '$1');
return newName + '-' + url;
};
const createListItemHTML = (item) => {
const { name, tech, description } = item;
const newName = upperCaseEveryWord(name);
return `
<li>
<div class="image-container">
<a href="${pathname}${name}">
<div class="image-placeholder">
<span class="placeholder-content">Loading...</span>
</div>
<img
src="${pathname}${name}/design/desktop-preview.jpg"
alt="${newName}"
loading="lazy"
width="375"
height="180"
onload="this.classList.add('loaded'); this.previousElementSibling.style.opacity='0';"
onerror="this.classList.add('error'); this.previousElementSibling.innerHTML='<span class=\\'placeholder-content\\'>Failed to load</span>';"
/>
</a>
</div>
<div class="tech">
${tech
.split('-')
.map((name) => `<span class="${name}">${name}</span>`)
.join(' ')}
</div>
<h2>${newName}</h2>
<p class="description">${description}</p>
<div class="links">
<a
class="link"
href="https://www.frontendmentor.io/challenges/${frontendmentor(
name,
item.fmpage
)}"
><span>Challenge</span></a>
<a
class="link"
href="https://github.com/sarfarazstark/frontendmentor/tree/main/${name}"
><span>Repo</span></a>
<a class="link" href="${pathname}${name}"
><span>Live</span></a>
</div>
</li>
`;
};
const main = document.querySelector('main');
const li = main.appendChild(document.createElement('ul'));
fetch('./data.json')
.then((response) => response.json())
.then((data) => {
data.sort((a, b) => new Date(a.submitDate) - new Date(b.submitDate));
for (const item of data) {
if ('status' in item) continue;
// console.log(item);
li.insertAdjacentHTML('afterbegin', createListItemHTML(item));
}
document.querySelector('.total').textContent = data.length;
});
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("./service-worker.js")
.then((reg) => console.log("Service Worker registered ✅", reg.scope))
.catch((err) => console.error("Service Worker registration failed ❌", err));
});
}