-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (132 loc) · 4.97 KB
/
app.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
const newsContainer = document.querySelector("#newsContainer");
const saveButton = document.querySelector("#saveButton");
const loadSavedButton = document.querySelector("#loadSavedButton");
const loadNewsButton = document.querySelector("#loadNewsButton");
const categorySelect = document.querySelector("#categorySelect");
const loader = document.querySelector(".loader");
function showDiv() {
document.querySelector('#hidden').style.display = "block";
}
const savedNews = JSON.parse(localStorage.getItem("savedNews")) || [];
const handleSavedNews = (savedItem) => {
savedNews.push(savedItem);
console.log(savedNews);
alert("News saved");
};
const removeSavedNewsItem = (newsItem) => {
const index = savedNews.indexOf(newsItem);
if (index !== -1) {
savedNews.splice(index, 1);
localStorage.setItem("savedNews", JSON.stringify(savedNews));
loadSavedNews();
console.log("News removed");
}
};
const getNews = (category = "science") => {
newsContainer.innerHTML = "";
loader.style.display = "block";
fetch(`https://api.currentsapi.services/v1/latest-news?apiKey=eJ47OiYS3zC815vMB9z19Di6uJ_EaL119FoMrS0geMROMtIR&category=${category}`)
.then((response) => response.json())
.then((data) => {
console.log("Data", data);
data.news.forEach((newsItem) => {
const div = document.createElement("div");
div.classList.add("newsItem");
div.innerHTML = `
<p>By <strong>${newsItem.author}</strong></p>
<h2 id="newsheading">${newsItem.title}</h2>
<div id="box">
<img src="${newsItem.image}" class="img"></img>
<div id="innerbox">
<p id="nscontent">${newsItem.description} <a href="${newsItem.url}" style="text-decoration:none">READ MORE</a></p>
<p>Date: ${newsItem.published.slice(0, 10)}</p>
<p>Time: ${newsItem.published.slice(11, 16)}</p>
</div>
</div>
`;
const button = document.createElement("button");
button.style.color = 'white';
button.style.backgroundColor = 'grey';
button.style.border = 'none';
button.style.marginTop = '10px';
button.style.cursor = 'pointer';
button.style.padding = '5px';
button.innerHTML = "Save";
button.onclick = function () {
handleSavedNews(newsItem);
};
div.appendChild(button);
newsContainer.appendChild(div);
});
loader.style.display = "none";
})
.catch((error) => {
console.error('Error:', error);
loader.style.display = "none";
});
};
const saveNews = () => {
const news = Array.from(document.querySelectorAll(".newsItem")).map(
(newsItem) => {
const title = newsItem.querySelector("h2").textContent;
const description = newsItem.querySelector("#nscontent").textContent;
const author = newsItem.querySelector("p strong").textContent;
const image = newsItem.querySelector("img").getAttribute("src");
const url = newsItem.querySelector("a").getAttribute("href");
const published = newsItem.querySelector("p:nth-child(3)").textContent.slice(6);
const time = newsItem.querySelector("p:nth-child(4)").textContent.slice(6);
return {
title,
description,
author,
image,
url,
published,
time,
};
}
);
console.log("Saved news", news);
localStorage.setItem("savedNews", JSON.stringify(news));
};
const loadSavedNews = () => {
console.log("Saved News", savedNews);
newsContainer.innerHTML = "";
//if (!savedNews) {
// return;
//}
savedNews.forEach((newsItem) => {
const div = document.createElement("div");
div.classList.add("newsItem");
div.innerHTML = `
<p>By <strong>${newsItem.author}</strong></p>
<h2 id="newsheading">${newsItem.title}</h2>
<div id="box">
<img src="${newsItem.image}" class="img"></img>
<div id="innerbox">
<p id="nscontent">${newsItem.description} <a href="${newsItem.url}" style="text-decoration:none">READ MORE</a></p>
<p>Date: ${newsItem.published.slice(0, 10)}</p>
<p>Time: ${newsItem.published.slice(11, 16)}</p>
</div>
</div>
`;
const removeButton = document.createElement("button");
removeButton.style.color = 'white';
removeButton.style.backgroundColor = 'grey';
removeButton.style.border = 'none';
removeButton.style.marginTop = '10px';
removeButton.style.cursor = 'pointer';
removeButton.style.padding = '5px';
removeButton.innerHTML = "Remove";
removeButton.onclick = function () {
removeSavedNewsItem(newsItem);
};
div.appendChild(removeButton);
newsContainer.appendChild(div);
});
};
loadSavedButton.addEventListener("click", loadSavedNews);
loadNewsButton.addEventListener("click", () => {
getNews(categorySelect.value);
});
getNews();