-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscript.js
35 lines (35 loc) · 1.07 KB
/
script.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
const storiesDiv = document.querySelector('#stories');
const storyNode = (story) => {
var template = document.createElement('template');
template.innerHTML = story;
return template.content.childNodes[0];
}
const addStories = (stories) => {
for (let index in stories) {
const story = stories[index];
const html = `<div class="story">
<a href="${story.url}">${story.text}</a>
</div>`;
storiesDiv.appendChild(storyNode(html));
}
}
if (localStorage.lastFetch && localStorage.stories && (new Date() - localStorage.lastFetch) < (1000*60*60)) {
addStories(JSON.parse(localStorage.stories));
} else {
if (localStorage.stories) {
addStories(JSON.parse(localStorage.stories));
}
fetch('https://api.hackernoon.com/featured-stories',{
method: 'GET',
mode: 'cors',
credentials: 'include'
})
.then(response => response.json())
.then(data => {
if (!localStorage.stories) {
addStories(data);
}
localStorage.setItem("stories", JSON.stringify(data));
localStorage.setItem("lastFetch", new Date()-1);
});
}