-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (79 loc) · 3.1 KB
/
Copy pathscript.js
File metadata and controls
99 lines (79 loc) · 3.1 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
const API_KEY = "a954cdc367ec41089650ccfbb1e2c5b8e";
const url = "https://newsapi.org/v2/everything?q=";
window.addEventListener("load", () => fetchNews("Top news"));
const loading = document.getElementById("loading");
const cardsContainer = document.getElementById("cardscontainer");
async function fetchNews(query) {
// Show loading indicator
loading.style.display = "block";
cardsContainer.style.display = "none";
try {
const res = await fetch(`${url}${query}&apiKey=${API_KEY}`);
const data = await res.json();
bindData(data.articles);
} catch (error) {
console.error("Error fetching data:", error);
} finally {
loading.style.display = "none";
cardsContainer.style.display = "flex";
}
}
function bindData(articles) {
const cardsContainer = document.getElementById("cardscontainer");
const newsCardTemplate = document.getElementById("template-news-card");
cardsContainer.innerHTML = "";
articles.forEach((article) => {
if (!article.urlToImage) return;
const cardClone = newsCardTemplate.content.cloneNode(true);
fillDataInCard(cardClone, article);
cardsContainer.appendChild(cardClone);
})
}
function fillDataInCard(cardClone, article) {
const newsImg = cardClone.querySelector("#news-img");
const newsTitle = cardClone.querySelector("#news-title");
const newsSource = cardClone.querySelector("#news-source");
const newsSourceTop = cardClone.querySelector("#news-source-top");
const newsDesc = cardClone.querySelector("#news-desc");
newsImg.src = article.urlToImage;
newsTitle.innerHTML = `${article.title.slice(0, 60)}...`;
newsDesc.innerHTML = `${article.description.slice(0, 150)}...`;
const date = new Date(article.publishedAt).toLocaleDateString("en-GB", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
// const date = new Date(article.publishedAt).toLocaleString("en-US", { timeZone: "Asia/Jakarta" });
newsSource.innerHTML = `Published At : ${date}`;
newsSourceTop.innerHTML = article.source.name;
cardClone.firstElementChild.addEventListener("click", () => {
window.open(article.url, "_blank");
});
}
let curSelectedNav = null;
function onNavItemClick(id) {
fetchNews(id);
const navItem = document.getElementById(id);
curSelectedNav?.classList.remove("active");
curSelectedNav = navItem;
curSelectedNav.classList.add("active");
}
const searchButton = document.getElementById("search-button");
const searchText = document.getElementById("search-text");
const searchInput = document.getElementById("search-text");
searchInput.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
const query = searchText.value;
if (!query) return;
fetchNews(query);
curSelectedNav?.classList.remove("active");
curSelectedNav = null;
}
});
searchButton.addEventListener("click", () => {
const query = searchText.value;
if (!query) return;
fetchNews(query);
curSelectedNav?.classList.remove("active");
curSelectedNav = null;
})