-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
338 lines (284 loc) · 9.05 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Initialise url parameters
const params = {
base: 'https://newsapi.org/v2/',
endpoint: 'top-headlines',
apiKey: 'f29390555fbc483ba17e7ec1cb19af1a',
language: 'en',
country: 'gb',
category: '',
query: '',
sortBy: 'publishedAt',
pageSize: 9,
pageNum: 1,
redacted: false
};
//---------------------------//
const init = () => {
getNews(params);
window.innerWidth > 768 ? getPopular() : false; // Only load sidebar stories on tablet and desktop
};
// Fetch News API data
const setURL = params => {
return `${params.base}${params.endpoint}?apiKey=${params.apiKey}&q=${
params.query
}&country=${params.country}&category=${params.category}&sortBy=${
params.sortBy
}&pageSize=${params.pageSize}&page=${params.pageNum}`;
};
const getNews = params => {
fetch(setURL(params))
.then(response => {
return response.json();
})
.then(data => {
addArticlesToFeed(data);
})
.catch(err => {
displayErrorToUser('Server failed to return data');
});
};
const getPopular = () => {
fetch(
'https://newsapi.org/v2/everything?language=en&domains=dailymail.co.uk&pageSize=8&sortBy=popularity&apiKey=f29390555fbc483ba17e7ec1cb19af1a' // hard-coded URL
)
.then(response => {
return response.json();
})
.then(data => {
addPopular(data);
})
.catch(err => {
displayErrorToUser('Server failed to return data');
});
};
// Error function
const displayErrorToUser = err => {
alert(err);
};
// Filter data to prevent stories without description or image loading
const cleanData = data => {
return data.articles.filter(
article =>
article.description != null &&
article.description != '' &&
article.urlToImage != null
);
};
// Create single news article
const createArticle = articleData => {
if (params.redacted === true) {
articleData.description = redact(articleData.description);
articleData.title = redact(articleData.title);
}
const article = document.createElement('article');
article.classList.add('news__article');
const elapsedTime = getTimeSinceArticlePublication(articleData.publishedAt);
if (window.innerWidth < 768) {
articleData.urlToImage = '';
}
article.innerHTML = `
<h2 class='news__headline'>${articleData.title}</h2>
<p class="news__date">Published ${elapsedTime} ago</p>
<img class='news__image' src="${articleData.urlToImage}" alt="">
<p class="news__story">${articleData.description}</p>
<p class="news__publication">Read the full story at <a href="${
articleData.url
}" class="news__source">${articleData.source.name}</a></p>`;
return article;
};
const createPopArticle = articleData => {
const article = document.createElement('article');
article.classList.add('news__article');
article.innerHTML = `
<img class='news__image' src="${articleData.urlToImage}">
<h3 class='news__headline'>${articleData.title}</h3>`;
return article;
};
// Aggregate the individual articles
const createArticles = data => {
const newsWrapper = document.createElement('div');
data.forEach(story => {
const newsArticle = createArticle(story);
newsWrapper.appendChild(newsArticle);
});
return newsWrapper;
};
const createPopArticles = data => {
const popWrapper = document.createElement('div');
data.forEach(story => {
const newsArticle = createPopArticle(story);
popWrapper.appendChild(newsArticle);
});
return popWrapper;
};
// Append the aggregated articles to the DOM
const addArticlesToFeed = data => {
const newsFeed = document.querySelector('section.news');
const ref = document.querySelector('section.news aside');
const feed = cleanData(data);
const stories = createArticles(feed);
newsFeed.insertBefore(stories, ref);
document.querySelector('.page-total').textContent = Math.floor(
data.totalResults / params.pageSize // Calculate the total number of pages in the feed
);
};
const addPopular = data => {
const popularFeed = document.querySelector('aside.popular');
const popFeed = cleanData(data);
const popStories = createPopArticles(popFeed);
popularFeed.appendChild(popStories);
};
// Content helper functions
// Add conditional 's' to min, day and hour when grater than 1
const pluralUnits = (val, unit) => {
return val >= 2 ? unit.replace(' ', 's ') : unit;
};
// Calculate time since publication of news article
const getTimeSinceArticlePublication = date => {
let mins = Math.floor((Date.now() - new Date(date).valueOf()) / 60000);
let hours = mins / 60;
let days = hours / 24;
let elapsedTime = '';
if (days >= 1) {
return (elapsedTime += `${Math.floor(days)} ${pluralUnits(days, 'day ')}`);
}
if (hours >= 1) {
return (elapsedTime += `${Math.floor(hours)} ${pluralUnits(
hours,
'hour '
)}`);
}
return (elapsedTime += `${mins} ${pluralUnits(mins, 'min ')}`);
};
// Pagination
// Delete current news stories from the DOM
const clearNewsFeed = () => {
document
.querySelector('section.news')
.removeChild(document.querySelector('section.news div'));
};
const nextPage = document.querySelector('.page-nav .next');
nextPage.addEventListener('click', e => {
const currentPageNum = document.querySelector('.page-current');
const totalPageNum = document.querySelector('.page-total');
e.preventDefault();
+currentPageNum.textContent < +totalPageNum.textContent // Prevent next page advancing beyond total no. of pages
? params.pageNum++
: false;
clearNewsFeed();
getNews(params);
currentPageNum.textContent = params.pageNum;
});
const prevPage = document.querySelector('.page-nav .prev');
prevPage.addEventListener('click', e => {
e.preventDefault();
params.pageNum > 1 ? params.pageNum-- : false; // Prevent previous page returning negative page number
clearNewsFeed();
getNews(params);
document.querySelector('.page-current').textContent = params.pageNum;
});
// Search
const searchForm = document.querySelector('form.search--form');
searchForm.addEventListener('submit', e => {
e.preventDefault();
const query = searchForm.lastElementChild.value;
// reset redact
params.redacted = false;
// process input value
if (query === 'Trump' || query === 'trump') {
params.redacted = true; // redact 'Trump' stories
}
if (query !== '') {
params.query = query;
params.endpoint = 'everything';
params.country = '';
params.pageNum = 1; // reset page counter
clearNewsFeed();
getNews(params);
}
searchForm.lastElementChild.value = '';
searchForm.lastElementChild.placeholder = `News about ${query}`; // Show search query as topic
//reset the search input if the user clicks on another part of the page
searchForm.lastElementChild.addEventListener('blur', e => {
searchInput.placeholder = 'Type your search query...';
searchInput.value = '';
});
});
// reset search input when the user makes consequtive searches
const searchInput = document.querySelector('#search--query');
searchInput.addEventListener('focus', e => {
searchInput.placeholder = '';
searchInput.value = '';
});
// Add current day and date to header
const todayDate = new Date();
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
document.querySelector('.hero__date').textContent = todayDate.toLocaleString(
'en-UK',
options
);
// Category select
const categories = document.querySelectorAll('.category');
categories.forEach(category => {
category.addEventListener('click', e => {
const topic = e.target.textContent;
// custom category searches based on query sting
if (topic === 'cycling') {
params.query = 'cycling bicycle cycle';
params.endpoint = 'everything';
params.country = '';
params.category = '';
} else if (topic === 'food') {
params.query = 'food nutrition cooking';
params.endpoint = 'everything';
params.country = '';
params.category = '';
// category 'general' renmaed to 'top stories'
} else if (topic === 'top stories') {
params.category = 'general';
params.query = '';
params.endpoint = 'top-headlines';
params.country = 'gb';
} else {
// category search based on API endpoint
params.category = topic;
params.query = '';
params.endpoint = 'top-headlines';
params.country = 'gb';
}
params.pageNum = 1; // reset page counter
// reset redact
params.redacted = false;
clearNewsFeed();
getNews(params);
Array.from(categories)
.filter(category => category.classList.contains('current'))
.map(category => category.classList.remove('current'));
category.classList.add('current');
});
});
// redact function wraps 5,6,8 & 9 character words in a span for CSS blackout styling
const redact = text => {
return text
.split(' ')
.map(word => {
if (
word.length === 5 ||
word.length === 6 ||
word.length === 8 ||
word.length === 9
) {
return `<span>${word}</span>`;
} else {
return word;
}
})
.join(' ');
};
// Start the app and get the news!
init();