-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
175 lines (148 loc) · 4.11 KB
/
main.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
window.addEventListener('load', function() {
utils.waterfall(database, [
getSources,
renderOptions, // <-- DOM manipulation
renderLogo, // <-- DOM manipulation
getHeadlines,
renderArticles // <-- DOM manipulation
], function(err, res){
console.log('err',err);
console.log('res',res);
});
});
function changeDropDown () {
utils.waterfall(option_list.value, [
renderLogo,
getHeadlines,
renderArticles
], function(err, res) {
console.log('err',err);
console.log('res',res);
});
}
/**
* In memory database
* @type {Object}
*
* Example:
* {
* 'The New York Times': {
* id: 'the-new-york-times',
* logo: 'http://i.newsapi.org/the-new-york-times-s.png',
* name: 'The New York Times',
* headlines: [
* {
* description: '"They said technical difficulties!" Trump tweeted Sunday.',
* publishedAt: '2017-02-12T15:18:11Z',
* title: 'Trump spreads debunked story about CNN cutting off Sanders for calling the network "fake news"',
* url: 'http://www.businessinsider.com/trump-spreads-debunked-story-about-cnn-fake-news-2017-2',
* urlToImage: '...'
* }
* ]
* }
* }
*/
var database = {};
/**
* Sources are newspapers and journals like CNN or NYT
* @param {Object} database
* @param {Function} cb
* @return {Undefined}
*/
function getSources (database, cb) {
var newsUrl = [
'https://newsapi.org',
'/v1/sources',
'?language=en',
'&country=us',
'&apikey=' + config.NEWS_KEY
].join('');
utils.request({
method: 'GET',
url: newsUrl
}, function(err, data) {
if(err) {
utils.handleError('Error with the sources');
cb(err, undefined);
return;
}
// save in the db
data.sources.forEach(utils.buildDb(database));
cb(undefined, database);
});
}
/**
* Creates options HTML and appends it to the `options_container`
* @param {Object} database
* @param {Function} cb
* @return {Undefined}
*/
function renderOptions (database, cb) {
var opt = '<option value="{{option}}">{{option}}</option>';
var select = '<select id="option_list">{{options}}</select>';
var optionList = Object.keys(database).map(function(elm) {
return utils.template(opt,{option:elm});
}).join('');
// append to DOM
options_container.innerHTML = utils.template(select,{options:optionList});
option_list.addEventListener('change', changeDropDown);
cb(undefined, option_list.value);
}
/**
* Creates logo HTML and appends it to the `logo_container`
* @param {String} sourceKey key of the database source
* @param {Function} cb
* @return {Undefined}
*/
function renderLogo (sourceKey, cb) {
var logoUrl = database[sourceKey]['logo'];
var imgTmpl = '<img src="{{url}}"/>';
// append to DOM
img_container.innerHTML = utils.template(imgTmpl,{url:logoUrl});
cb(undefined, sourceKey);
}
/**
* Given a specific sourceKey (newspaper or journal)
* fetches all the top line articles
* @param {String} sourceKey key of the database source used the get the id
* @param {Function} cb
* @return {Undefined}
*/
function getHeadlines (sourceKey, cb) {
var source = database[sourceKey];
var articleUrl = [
'https://newsapi.org',
'/v1/articles',
'?source=' + source.id,
'&apikey=' + config.NEWS_KEY
].join('');
utils.request({
method: 'GET',
url: articleUrl
}, function (err, data) {
if(err) {
utils.handleError('Error with the articles');
return;
}
utils.addHeadlines(database, source, data.articles);
cb(undefined, data.articles);
});
}
/**
* Constracts the HTML for all the articles and appends it to `articles_container`
* @param {Array} articles array of articles objects
* @param {Function} cb
* @return {Undefined}
*/
function renderArticles (articles, cb) {
var articleTmpl = [
'<article>',
'<h1>{{title}}</h1>',
'<img style="width:128px;height:128px;" src="{{urlToImage}}"/>',
'<a href="{{url}}">Read more...</a>',
'</article>',
].join('');
articles_container.innerHTML = articles.map(function(elm) {
return utils.template(articleTmpl,elm);
});
}