forked from makersacademy/news-summary-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
137 lines (132 loc) · 4.55 KB
/
bundle.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
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// newsModel.js
var require_newsModel = __commonJS({
"newsModel.js"(exports, module) {
var NewsModel2 = class {
constructor() {
this.articles = [];
this.keyword = "undefined";
this.relevantArticles = [];
}
addArticles(articles) {
this.articles = [];
this.articles = [];
articles.forEach((article) => {
this.addArticle(article);
});
}
addArticle = (article) => {
const newArticle = {};
newArticle.title = article.webTitle;
newArticle.url = article.webUrl;
newArticle.image = article.fields.thumbnail;
this.articles.push(newArticle);
};
viewArticles() {
return this.articles;
}
matchingKeyword = (article) => {
if (article.title.includes(this.keyword)) {
this.relevantArticles.push(article);
}
};
matchingArticles(keyword) {
this.relevantArticles = [];
this.keyword = keyword;
this.articles.map(this.matchingKeyword);
return this.relevantArticles;
}
};
module.exports = NewsModel2;
}
});
// newsView.js
var require_newsView = __commonJS({
"newsView.js"(exports, module) {
var NewsView2 = class {
constructor(model2, client2) {
this.model = model2;
this.client = client2;
this.mainContainerEl = document.querySelector("#main-container");
this.buttonEl = document.querySelector("#refresh-news-button");
this.buttonEl.addEventListener("click", () => {
this.displayArticles();
});
this.searchButtonEl = document.querySelector("#search-news-button");
this.searchButtonEl.addEventListener("click", () => {
const keyword = document.querySelector("#search-term").value;
this.displayRelevantArticles(keyword);
const inputEl = document.querySelector("#search-term");
inputEl.value = "";
});
}
newArticle = (article) => {
const newArticle = document.createElement("a");
const newImage = document.createElement("img");
const text = document.createElement("h4");
text.innerHTML = article.title;
newImage.src = article.image;
newArticle.href = article.url;
newArticle.append(text);
newArticle.append(newImage);
newArticle.className = "article";
this.mainContainerEl.append(newArticle);
};
displayArticles() {
document.querySelectorAll(".article").forEach((article) => {
article.remove();
});
this.client.fetchNewsData((data) => {
let articles = data.response.results;
this.model.addArticles(articles);
this.model.viewArticles().map(this.newArticle);
});
}
displayRelevantArticles(keyword) {
document.querySelectorAll(".article").forEach((article) => {
article.remove();
});
this.client.fetchNewsData((data) => {
let articles = data.response.results;
this.model.addArticles(articles);
this.model.matchingArticles(keyword).map(this.newArticle);
});
}
};
module.exports = NewsView2;
}
});
// apiKey.js
var require_apiKey = __commonJS({
"apiKey.js"(exports, module) {
module.exports = "02257cd4-13fe-45bf-b646-9a292f5a49b4";
}
});
// newsClient.js
var require_newsClient = __commonJS({
"newsClient.js"(exports, module) {
var apiKey = require_apiKey();
var NewsClient2 = class {
fetchNewsData(callback) {
const apiUrl = `https://content.guardianapis.com/search?q=&query-fields=headline&show-fields=thumbnail,headline,byline&order-by=newest&api-key=${apiKey}`;
fetch(apiUrl).then((response) => response.json()).then((data) => {
callback(data);
});
}
};
module.exports = NewsClient2;
var anotherApi = new NewsClient2();
}
});
// index.js
var NewsModel = require_newsModel();
var NewsView = require_newsView();
var NewsClient = require_newsClient();
var client = new NewsClient();
var model = new NewsModel();
var view = new NewsView(model, client);
})();