-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (122 loc) · 4.73 KB
/
Copy pathscript.js
File metadata and controls
147 lines (122 loc) · 4.73 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
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
const API_KEY = "824703ee848b4404b51e035a09edbc47";
const url = "https://newsapi.org/v2/everything?q=";
window.addEventListener("load", () => loader());
// Function for loader
function loader() {
let i = 0;
let content = document.querySelector(".content");
let loader = document.querySelector(".loader");
let loaderContainer = document.querySelector(".loader-container");
let body = document.querySelector("body");
let number = document.querySelector(".number");
let percentBar = document.querySelector(".percentBar");
let interval = setInterval(function () {
number.innerHTML = i + '<span>%</span>';
percentBar.style.width = i + '%';
i++;
if (i == 101) {
clearInterval(interval)
setTimeout(function () {
loader.style.opacity = '0';
loader.style.visibility = 'hidden';
loaderContainer.style.opacity = '0';
loaderContainer.style.visibility = 'hidden';
loaderContainer.style.display = 'none';
content.style.opacity = '1';
content.style.visibility = 'visible';
fetchNews("India"); // Calling the API for the first time !
})
}
}, 100)
}
// Function to reload the page
function reload() {
window.location.reload();
}
// Function to show a message when no data is found
function showNoDataMessage() {
const cardsContainer = document.getElementById("cards-container");
cardsContainer.innerHTML = "<p>No articles found. Please try a different search.</p>";
}
// Function to handle errors when fetching news
function showErrorMessage() {
const cardsContainer = document.getElementById("cards-container");
cardsContainer.innerHTML = "<p>Error fetching data. Please try again later.</p>";
}
// Fetch news based on the query
async function fetchNews(query) {
try {
const res = await fetch(`${url}${query}&apiKey=${API_KEY}`);
// If the response is not successful, show an error message
if (!res.ok) {
throw new Error("Failed to fetch news.");
}
const data = await res.json();
// If no articles are found, show a no data message
if (data.articles.length === 0) {
showNoDataMessage();
} else {
bindData(data.articles);
}
} catch (error) {
console.error("Error fetching data:", error);
showErrorMessage();
}
}
// Function to bind articles to the UI
function bindData(articles) {
const cardsContainer = document.getElementById("cards-container");
const newsCardTemplate = document.getElementById("template-news-card");
cardsContainer.innerHTML = ""; // Clear the existing content
articles.forEach((article) => {
if (!article.urlToImage) return; // Skip articles without an image
const cardClone = newsCardTemplate.content.cloneNode(true);
fillDataInCard(cardClone, article);
cardsContainer.appendChild(cardClone);
});
}
// Function to fill data into each card
function fillDataInCard(cardClone, article) {
const newsImg = cardClone.querySelector("#news-img");
const newsTitle = cardClone.querySelector("#news-title");
const newsSource = cardClone.querySelector("#news-source");
const newsDesc = cardClone.querySelector("#news-desc");
const readMoreBtn = cardClone.querySelector("#read");
newsImg.src = article.urlToImage;
newsTitle.innerHTML = article.title;
newsDesc.innerHTML = article.description;
const date = new Date(article.publishedAt).toLocaleString("en-US", {
timeZone: "Asia/Jakarta",
});
newsSource.innerHTML = `${article.source.name} · ${date}`;
// Set up the "Read More" button
readMoreBtn.addEventListener("click", () => {
// Store article details in localStorage
localStorage.setItem("articleDetails", JSON.stringify(article));
window.location.href = "article.html";
});
}
// Function to handle navigation item clicks
let curSelectedNav = null;
function onNavItemClick(id) {
fetchNews(id);
const navItem = document.getElementById(id);
curSelectedNav?.classList.remove("active");
curSelectedNav = navItem;
curSelectedNav.classList.add("active");
}
// Search functionality
const searchButton = document.getElementById("search-button");
const searchText = document.getElementById("search-text");
searchButton.addEventListener("click", () => {
const query = searchText.value;
if (!query) return;
fetchNews(query);
curSelectedNav?.classList.remove("active");
curSelectedNav = null;
});
// Function to handle Light/Dark Mode
function toggleMode() {
var element = document.body;
element.classList.toggle("dark-mode");
}