forked from lkrryba/weekly-web-dev-challenge-poll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submissions.js
57 lines (49 loc) · 1.97 KB
/
submissions.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
export function renderSubmissions(submissions, imagePath) {
// Loop through submissions array
const shuffleArray = (array) => {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
};
shuffleArray(submissions);
submissions.forEach((submission, index) => {
// Create a new submission element with a class of submission
const submissionElement = document.createElement("div");
submissionElement.classList.add("submission");
// If it is the winner, add the winner class
if (submission.winner) submissionElement.classList.add("winner");
// Add elements to submission element
// First the anchor tag (link)
const link = document.createElement("a");
link.href = submission.entryURL;
link.target = "_blank";
link.rel = "noopener";
submissionElement.appendChild(link);
// Then, inside the anchor tag, the thumbnail and name.
const img = document.createElement("img");
img.src = `${imagePath}${submission.thumbnail}`;
img.loading = "lazy";
img.classList.add("submission-img");
link.appendChild(img);
const nameParagraph = document.createElement("p");
nameParagraph.textContent = submission.name;
link.appendChild(nameParagraph);
const altText = document.createAttribute("alt");
altText.textContent = `${submission.name}'s submission`;
img.setAttributeNode(altText);
// When most of the image is prepared, remove the placeholder
const placeholder = document.getElementById(`placeholder${index}`);
if (placeholder) placeholder.remove();
// Inject the submission element into the DOM
const takePartDiv = document.querySelector(".take-part");
takePartDiv.insertAdjacentElement("afterend", submissionElement);
});
}
export async function getSubmissions(jsonPath) {
const response = await fetch(jsonPath);
let data = await response.json();
return data;
}