-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathjavascript-workings2.js
More file actions
128 lines (111 loc) · 3.32 KB
/
javascript-workings2.js
File metadata and controls
128 lines (111 loc) · 3.32 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
124
125
126
127
128
const baseUrl = "https://newsapi.org/v2/top-headlines?apiKey=93238bcda39e4404852697d364b77971";
const parentNode = document.querySelector(".news--area--feed");
let checkboxArray = document.querySelectorAll(".news--filter input");
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
----------------------
*/
const createCheckboxFilter = function() {
// Reset UserOpted to false
userOpted = false;
checkboxArray.forEach(function(input) {
input.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;
userOpted = true;
}
else {
publicationList[event.target.value] = false;
}
console.log(publicationList)
generateFetchURL(publicationList);
})
})
}
/* this function to run generateFetchURL() */
createCheckboxFilter();
/*
---------------------
ERROR HANDLER - TO DO
---------------------
*/
function displayErrorToUser() {}
// Initial call to fetch data
generateFetchURL(baseUrl);