-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathjavascript-workings.js
More file actions
75 lines (51 loc) · 1.98 KB
/
Copy pathjavascript-workings.js
File metadata and controls
75 lines (51 loc) · 1.98 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
const parentNode = document.querySelector(".news--area--feed");
// compile this Url = "https://newsapi.org/v2/top-headlines?sources=bbc-news,daily-mail,mirror,&apiKey=93238bcda39e4404852697d364b77971";
const baseUrl = "https://newsapi.org/v2/top-headlines?apiKey=93238bcda39e4404852697d364b77971";
const publicationList = {
"bbc-news": true,
"daily-mail": true,
"mirror": true
}
// create an array from object using key values
let publicationArray = Object.keys(publicationList);
//console.log(defaultArray);
const sourceList = `&sources=${defaultArray}`;
const fullURL = `${baseUrl}${sourceList}`;
function displayDataOnPage(newsStories) {
const newsArray = newsStories.articles;
console.log(newsStories.articles);
// add news blocks (as articles)
newsArray.forEach(function(newsitem) {
const node = document.createElement("article");
node.innerHTML = `${newsitem.source.name} (source logo)
<b>${newsitem.title}:</b> ${newsitem.description}`;
parentNode.appendChild(node);
})
// filter
let checkboxArray = document.querySelectorAll(".news--filter input");
console.log(checkboxArray)
checkboxArray.forEach(function(input) {
input.addEventListener("change", event => {
console.log(event.target.value);
if (publicationList[event.target.value]) {
publicationList[event.target.value] = false
filteredArray.push(event.target.value);
}
console.log(filteredArray)
})
})
}
function displayErrorToUser() {}
fetch(fullURL) // by default fetch makes a GET request
.then(function(response) {
return response.json();
})
.then(function(body) {
//console.log(body);
displayDataOnPage(body);
})
.catch(function(error) {
displayErrorToUser("Server failed to return data");
// need filter to NOT SHOW any news story with empty values
// if any value is empty do no show
});