-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewsApp.html
More file actions
279 lines (237 loc) · 8.65 KB
/
NewsApp.html
File metadata and controls
279 lines (237 loc) · 8.65 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>News App</title>
<!-- Link to Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Poppins', sans-serif;
background-color: #f9f3f3;
/* Soft off-white background */
color: #333;
}
h1 {
font-weight: 600;
color: #4b4b4b;
/* Dark Gray */
margin-bottom: 40px;
text-align: center;
}
/* Colorful Button */
.btn-primary {
background-color: #ff6f61;
/* Coral red */
border-color: #ff6f61;
padding: 12px 24px;
border-radius: 50px;
font-weight: 600;
transition: all 0.3s ease;
}
.btn-primary:hover {
background-color: #e85a4f;
/* Darker Coral */
border-color: #e85a4f;
transform: translateY(-2px);
}
/* Search Bar */
.search-bar {
margin-bottom: 40px;
text-align: center;
}
.form-control {
border-radius: 50px;
font-size: 1.1rem;
border: 2px solid #ff6f61;
}
.form-control:focus {
border-color: #ff6f61;
box-shadow: 0px 4px 8px rgba(255, 111, 97, 0.5);
}
/* Card Styling */
.card-body {
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0px 6px 12px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.card-body:hover {
box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2);
}
.card {
border-radius: 10px;
overflow: hidden;
background-color: #fff;
transition: transform 0.3s ease;
margin-bottom: 30px;
}
.card:hover {
transform: scale(1.05);
}
.card-img-top {
height: 250px;
object-fit: cover;
border-radius: 10px 10px 0 0;
}
.card-title {
font-size: 1.3rem;
font-weight: 600;
color: #3d3d3d;
}
.card-text {
font-size: 1rem;
color: #6c757d;
margin-bottom: 20px;
}
.publish-date {
font-size: 0.9rem;
color: #007bff;
margin-top: 10px;
}
.publish-date span {
font-weight: 600;
}
.card-footer {
background-color: #f1f1f1;
/* Light Gray */
border-top: 1px solid #e0e0e0;
padding: 15px;
text-align: center;
}
.card-footer a {
font-size: 1rem;
color: #ff6f61;
/* Coral */
text-decoration: none;
}
.card-footer a:hover {
color: #e85a4f;
/* Darker Coral */
text-decoration: underline;
}
/* Loading Spinner */
#loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: none;
}
/* Responsive */
@media (max-width: 767px) {
.card-body {
padding: 15px;
}
.search-bar {
margin-bottom: 20px;
}
}
</style>
</head>
<body>
<div class="container mt-5">
<h1>Latest News</h1>
<!-- Search bar -->
<div class="search-bar">
<form id="search-form" class="form-inline justify-content-center">
<input type="text" class="form-control mr-2" id="search-input" placeholder="Search for news..."
aria-label="Search">
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
<div id="news-container" class="row">
<!-- News articles will be inserted here -->
</div>
</div>
<!-- Loading spinner -->
<div id="loading" class="text-center">
<div class="spinner-border text-primary" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
<!-- Script to fetch news articles -->
<script>
const apiKey = '81d4af075a4b40fa9a35cdab096aec9a'; // Your News API key
const apiUrl = `https://newsapi.org/v2/top-headlines?country=us&apiKey=${apiKey}`;
const newsContainer = document.getElementById('news-container');
const loadingElement = document.getElementById('loading');
const searchForm = document.getElementById('search-form');
const searchInput = document.getElementById('search-input');
async function fetchNews(query = '') {
try {
loadingElement.style.display = 'block'; // Show loading spinner
const url = query ?
`https://newsapi.org/v2/everything?q=${query}&apiKey=${apiKey}` :
''; // Empty string means no news is fetched on initial load.
if (url === '') {
loadingElement.style.display = 'none';
return; // Do nothing if no query is entered yet.
}
const response = await fetch(url);
const data = await response.json();
// Hide loading spinner
loadingElement.style.display = 'none';
if (data.status === 'ok') {
displayNews(data.articles);
} else {
newsContainer.innerHTML = ''; // Keep empty if no news found
}
} catch (error) {
loadingElement.style.display = 'none';
newsContainer.innerHTML = ''; // Keep empty in case of error
}
}
function displayNews(articles) {
// Clear any existing news articles
newsContainer.innerHTML = '';
// Loop through the news articles and add them to the container
articles.forEach(article => {
// Do nothing if the article doesn't have a valid image or link
if (!article.urlToImage || !article.url) return;
// Format the date
const publishDate = new Date(article.publishedAt);
const formattedDate = publishDate.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
});
// If valid, append the article details with title, description, and publish date
const newsItem = document.createElement('div');
newsItem.classList.add('col-md-4', 'mb-4');
// Display the article content (image, title, description, publish date, and Read more button)
newsItem.innerHTML = `
<div class="card">
<img src="${article.urlToImage}" class="card-img-top" alt="news image">
<div class="card-body">
<h5 class="card-title">${article.title}</h5>
<p class="card-text">${article.description || 'No description available.'}</p>
<p class="publish-date">Published on: <span>${formattedDate}</span></p>
</div>
<div class="card-footer">
<a href="${article.url}" target="_blank">Read more</a>
</div>
</div>
`;
newsContainer.appendChild(newsItem);
});
}
// Handle form submission for search
searchForm.addEventListener('submit', function (e) {
e.preventDefault();
const query = searchInput.value.trim();
if (query) {
fetchNews(query); // Fetch news based on search query
}
});
</script>
<!-- Scripts for Bootstrap and dependencies -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>