-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathjavascript-workings3.js
More file actions
123 lines (103 loc) · 3.19 KB
/
Copy pathjavascript-workings3.js
File metadata and controls
123 lines (103 loc) · 3.19 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const baseUrl = "https://newsapi.org/v2/top-headlines?apiKey=93238bcda39e4404852697d364b77971";
const parentNode = document.querySelector(".news--area--feed");
let checkboxArray = document.querySelector(".news--filter");
let userOpted = false;
/*
--------------
FETCH FUNCTION
--------------
*/
const goFetch = function(fullURL) {
fetch(fullURL) // by default fetch makes a GET request
.then(function(response) {
return response.json();
})
.then(function(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
});
}
/*
--------------------
DISPLAY DATA ON PAGE
--------------------
*/
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 = `<figure class="news--article-image"><img src="${newsitem.urlToImage}"></figure>
<section class="news--article-content">
<header class="${newsitem.source.name}"><h2>${newsitem.title}</h2></header>
<h3>${newsitem.description}</h3>
<p>${newsitem.content}</p>
<p><a href="${newsitem.url}" title="Visit news article: ${newsitem.title}">Read full article</a>
</section>`;
parentNode.appendChild(node);
})
}
/*
------------------
GENERATE FETCH URL
------------------
*/
// news sources object
let publicationList = {
"daily-mail": false,
"mirror": false,
"metro": false,
"the-telegraph": false,
"financial-times": false,
"bbc-news": false,
}
const generateFetchURL = function (publicationList) {
// baseUrl and default
const defaultArray = ["bbc-news","daily-mail","mirror"];
// create an array from object using key values
let publicationArray = Object.keys(publicationList);
let filteredArray = publicationArray.filter(function(pub) {
return publicationList[pub] === true;
});
// compile fetch url
let defaultArrayUrl = `&sources=${defaultArray}`;
let filteredPublicationUrl = `&sources=${filteredArray}`;
let fullURL = "";
if (userOpted === true) {
// RETURN VALUES - fullURL
fullURL = `${baseUrl}${filteredPublicationUrl}`;
} else {
fullURL = `${baseUrl}${defaultArrayUrl}`;
}
goFetch(fullURL)
}
/*
----------------------
CREATE CHECKBOX FILTER
----------------------
*/
// Attach change event on checkboxes.
checkboxArray.addEventListener("change", function(event) {
// new assigned value to match object key
// assign object value if checked is true
if (event.target.checked === true) {
publicationList[event.target.value] = true;
} else {
publicationList[event.target.value] = false;
}
// Verify if any of the options are being selected.
userOpted = Object.values(publicationList).reduce(function (val1, val2) { return val1 || val2 })
generateFetchURL(publicationList)
})
/*
---------------------
ERROR HANDLER - TO DO
---------------------
*/
function displayErrorToUser() {}
// Initial call to fetch data
generateFetchURL(baseUrl);