forked from makersacademy/news-summary-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.js
126 lines (121 loc) · 4.09 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
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// apiKey.js
var require_apiKey = __commonJS({
"apiKey.js"(exports, module) {
module.exports = "e0fb070f-e312-42b9-9d23-cd3e50fb535e";
}
});
// newsClient.js
var require_newsClient = __commonJS({
"newsClient.js"(exports, module) {
var apiKey = require_apiKey();
var NewsClient2 = class {
constructor() {
}
loadArticles(callback, searchKeyword) {
if (searchKeyword == null) {
searchKeyword = "";
} else {
searchKeyword = "q=" + searchKeyword;
}
fetch(`https://content.guardianapis.com/search?${searchKeyword}&query-fields=headline&show-fields=thumbnail,headline,byline&order-by=newest&api-key=${apiKey}`).then((response) => {
return response.json();
}).then((data) => {
callback(data);
});
}
};
module.exports = NewsClient2;
}
});
// newsModel.js
var require_newsModel = __commonJS({
"newsModel.js"(exports, module) {
var NewsModel2 = class {
constructor() {
this.articles = [];
}
getArticles() {
return this.articles;
}
addArticle(article) {
this.articles.push(article);
}
setsArticles(articles) {
this.articles = articles;
}
};
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");
const searchBox = document.querySelector("#search-box");
const searchButton = document.querySelector("#search-button");
searchButton.addEventListener("click", () => {
this.clearArticles();
this.displayArticlesFromApi(searchBox.value);
});
}
displayArticles() {
this.model.getArticles().forEach((article) => {
const articleDiv = document.createElement("div");
articleDiv.className = "article";
const articleImg = document.createElement("img");
articleImg.src = this.getArticleImgUrl(article);
articleDiv.append(articleImg);
const articleAnchor = document.createElement("a");
articleAnchor.textContent = article.webTitle;
articleAnchor.setAttribute("href", article.webUrl);
articleAnchor.setAttribute("target", "_blank");
articleDiv.append(articleAnchor);
this.mainContainerEl.append(articleDiv);
});
}
displayArticlesFromApi(searchKeyword) {
this.client.loadArticles((apiData) => {
this.model.setsArticles(apiData.response.results);
console.log(this.model.getArticles());
this.displayArticles();
}, searchKeyword);
}
getArticleImgUrl(article) {
let articleImgUrl = "";
if (article.fields != null && article.fields.thumbnail != null) {
articleImgUrl = article.fields.thumbnail;
} else {
console.log(`no image available for article "${article.id}"`);
articleImgUrl = "background.webp";
}
;
return articleImgUrl;
}
clearArticles() {
const allArticleDivs = document.querySelectorAll("div.article");
allArticleDivs.forEach((div) => {
div.remove();
});
}
};
module.exports = NewsView2;
}
});
// index.js
var NewsClient = require_newsClient();
var NewsModel = require_newsModel();
var NewsView = require_newsView();
var model = new NewsModel();
var client = new NewsClient();
var view = new NewsView(model, client);
view.displayArticlesFromApi();
})();