-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (122 loc) · 4.11 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
134
const topStoriesId = new XMLHttpRequest();
let loadBar = document.querySelector(".loadbar");
function getTopStoriesId() {
topStoriesId.open(
"GET",
"https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty",
true
);
topStoriesId.onload = function () {
if (this.status === 200) {
getPosts(JSON.parse(this.responseText));
}
};
topStoriesId.send();
}
function getPosts(posts) {
// console.log(posts);
posts.forEach((post) => {
const topStories = new XMLHttpRequest();
topStories.open(
"GET",
`https://hacker-news.firebaseio.com/v0/item/${post}.json?print=pretty`,
true
);
topStories.send();
topStories.onload = function () {
updateUI(JSON.parse(topStories.responseText), posts.length);
};
});
}
logged = false;
i = 0;
function updateUI(posts, postx) {
// loadBar.setAttribute("style", `width:${(i / postx) * 100}%`);
loadBar.style.width = `${Math.floor((i / postx) * 100)}%`;
i++;
switch (loadBar.style.width) {
case "10%":
document.querySelector(".loading-message").innerText =
"Fetching posts...";
break;
case "25%":
document.querySelector(".loading-message").innerText =
"Rendering page...";
break;
case "50%":
document.querySelector(".loading-message").innerText =
"Hang on, we're halfway done...";
break;
case "75%":
document.querySelector(".loading-message").innerText = "Almost there...";
break;
case "99%":
document.querySelector(".loading-message").innerText = "Welcome";
break;
default:
break;
}
if (logged !== true) {
console.log(posts);
// console.log("This is a great example of speech syntheis in English");
}
logged = true;
let date = new Date();
date = Math.round(((Date.now() - posts.time) / 1000) % 24);
//Checking if the post has no comments, but text
if (!("kids" in posts) && "text" in posts) {
document.querySelector(".posts-container").innerHTML += `
<div class="post">
<span class="post-score">↑ ${posts.score}</span>
<span class="post-title"><a href="${posts.url}">${posts.title}</a></span>
<div class="info"><i class="post-by">${posts.by}</i> <span class="time">| ${date} hours ago</span> | <span class="comments">0 comments</span></div>
<div class="post-text">${posts.text}</div>
</div>`;
console.log(posts);
document.querySelector(".post").addEventListener("click", () => {
console.log("m");
});
} else if ("kids" in posts && !("text" in posts)) {
document.querySelector(".posts-container").innerHTML += `
<div class="post">
<div class="post-score">↑ ${posts.score}</div>
<span class="post-title"><a href="${posts.url}">${posts.title}</a></span >
<div class="info"><i class="post-by">${posts.by}</i> <span class="time">| ${date} hours ago</span> | <span class="comments">${posts.kids.length} comments</span></div>
</div>`;
[...document.querySelectorAll(".post")].forEach((el) => {
el.addEventListener("click", (e) => {
console.log(e.target);
if (e.target.classList.contains("comments")) {
alert("mme");
}
});
});
}
checkDOMLoaded(i, postx);
}
if (Math.floor(Date.now() / 60000) - parseInt(localStorage.lastTime) < 10) {
document.querySelector(".loading").classList.add("hidden");
document.querySelector(".app-container").classList.remove("hidden");
let dom = localStorage.dom;
// console.log(dom);
document.querySelector(".app-container").children[1].children[2].innerHTML= dom;
} else {
getTopStoriesId();
}
function checkDOMLoaded(i, postx) {
if (i === postx) {
localStorage.setItem(
"lastTime",
JSON.stringify(Math.floor(Date.now() / 60000))
);
console.log(localStorage.setItem);
// console.log("The i is", i;
document.querySelector(".app-container").classList.remove("hidden");
document.querySelector(".loading").classList.add("hidden");
localStorage.setItem(
"dom",
document.querySelector(".posts-container").innerHTML
);
}
}
// document.querySelector(".app-container").classList.add("hidden");