mirror of
https://github.com/abrendan/smolNews.git
synced 2025-06-16 12:45:04 +02:00
Added error message when no articles found
This commit is contained in:
parent
5f46363a05
commit
2ed962250c
26
script.js
26
script.js
@ -111,7 +111,21 @@ function fetchFromNYTAPI(query, topic) {
|
|||||||
|
|
||||||
function displayArticles(articles, apiType) {
|
function displayArticles(articles, apiType) {
|
||||||
const articlesDiv = document.getElementById('articles');
|
const articlesDiv = document.getElementById('articles');
|
||||||
articlesDiv.innerHTML = '';
|
articlesDiv.innerHTML = ''; // Clear previous articles
|
||||||
|
|
||||||
|
const messageDiv = document.createElement('div'); // Create a message div
|
||||||
|
|
||||||
|
if (articles.length === 0) {
|
||||||
|
// Display an error message if there are no articles
|
||||||
|
messageDiv.innerHTML = '<p class="text-danger">No articles found. Please try a different search.</p>';
|
||||||
|
messageDiv.className = 'message';
|
||||||
|
document.body.appendChild(messageDiv);
|
||||||
|
setTimeout(() => {
|
||||||
|
document.body.removeChild(messageDiv);
|
||||||
|
}, 3000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
articles.forEach(article => {
|
articles.forEach(article => {
|
||||||
const thumbnailUrl = article.image || 'default-thumbnail.jpg';
|
const thumbnailUrl = article.image || 'default-thumbnail.jpg';
|
||||||
|
|
||||||
@ -135,6 +149,16 @@ function displayArticles(articles, apiType) {
|
|||||||
`;
|
`;
|
||||||
articlesDiv.appendChild(articleElement);
|
articlesDiv.appendChild(articleElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Display success message
|
||||||
|
messageDiv.innerHTML = '<p class="text-success">Articles loaded successfully!</p>';
|
||||||
|
messageDiv.className = 'message';
|
||||||
|
document.body.appendChild(messageDiv);
|
||||||
|
|
||||||
|
// Automatically remove the message after a few seconds
|
||||||
|
setTimeout(() => {
|
||||||
|
document.body.removeChild(messageDiv);
|
||||||
|
}, 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function viewArticleDetails(article) {
|
function viewArticleDetails(article) {
|
||||||
|
20
style.css
20
style.css
@ -29,4 +29,24 @@
|
|||||||
|
|
||||||
.icon-small {
|
.icon-small {
|
||||||
width: 10%; /* Set the width to 10% */
|
width: 10%; /* Set the width to 10% */
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
position: fixed;
|
||||||
|
top: 20px;
|
||||||
|
right: 20px;
|
||||||
|
z-index: 1000;
|
||||||
|
padding: 10px 15px;
|
||||||
|
border-radius: 20px;
|
||||||
|
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
|
||||||
|
max-width: 300px;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.text-success {
|
||||||
|
color: #155724;
|
||||||
|
}
|
||||||
|
|
||||||
|
.text-danger {
|
||||||
|
color: #721c24;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user