Spaces:
Running
Running
Where is news
Browse files- components/news-card.js +99 -0
- index.html +34 -2
- script.js +33 -1
components/news-card.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class NewsCard extends HTMLElement {
|
| 2 |
+
static get observedAttributes() {
|
| 3 |
+
return ['title', 'source', 'date', 'summary', 'image', 'url'];
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
attributeChangedCallback(name, oldValue, newValue) {
|
| 7 |
+
if (this.shadowRoot) {
|
| 8 |
+
this.render();
|
| 9 |
+
}
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
connectedCallback() {
|
| 13 |
+
this.attachShadow({ mode: 'open' });
|
| 14 |
+
this.render();
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
render() {
|
| 18 |
+
const title = this.getAttribute('title') || 'No title';
|
| 19 |
+
const source = this.getAttribute('source') || 'Unknown source';
|
| 20 |
+
const date = this.getAttribute('date') || 'Just now';
|
| 21 |
+
const summary = this.getAttribute('summary') || 'No summary available';
|
| 22 |
+
const image = this.getAttribute('image') || 'http://static.photos/news/640x360/1';
|
| 23 |
+
const url = this.getAttribute('url') || '#';
|
| 24 |
+
|
| 25 |
+
this.shadowRoot.innerHTML = `
|
| 26 |
+
<style>
|
| 27 |
+
.news-card {
|
| 28 |
+
background: white;
|
| 29 |
+
border-radius: 0.5rem;
|
| 30 |
+
overflow: hidden;
|
| 31 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 32 |
+
transition: transform 0.2s;
|
| 33 |
+
}
|
| 34 |
+
.news-card:hover {
|
| 35 |
+
transform: translateY(-2px);
|
| 36 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 37 |
+
}
|
| 38 |
+
.news-image {
|
| 39 |
+
width: 100%;
|
| 40 |
+
height: 180px;
|
| 41 |
+
object-fit: cover;
|
| 42 |
+
}
|
| 43 |
+
.news-content {
|
| 44 |
+
padding: 1rem;
|
| 45 |
+
}
|
| 46 |
+
.news-title {
|
| 47 |
+
font-size: 1.1rem;
|
| 48 |
+
font-weight: 600;
|
| 49 |
+
margin-bottom: 0.5rem;
|
| 50 |
+
color: #111827;
|
| 51 |
+
}
|
| 52 |
+
.news-meta {
|
| 53 |
+
display: flex;
|
| 54 |
+
justify-content: space-between;
|
| 55 |
+
color: #6b7280;
|
| 56 |
+
font-size: 0.8rem;
|
| 57 |
+
margin-bottom: 0.75rem;
|
| 58 |
+
}
|
| 59 |
+
.news-summary {
|
| 60 |
+
color: #4b5563;
|
| 61 |
+
font-size: 0.9rem;
|
| 62 |
+
line-height: 1.4;
|
| 63 |
+
margin-bottom: 1rem;
|
| 64 |
+
}
|
| 65 |
+
.read-more {
|
| 66 |
+
color: #3b82f6;
|
| 67 |
+
text-decoration: none;
|
| 68 |
+
font-weight: 500;
|
| 69 |
+
font-size: 0.9rem;
|
| 70 |
+
display: inline-flex;
|
| 71 |
+
align-items: center;
|
| 72 |
+
}
|
| 73 |
+
.read-more:hover {
|
| 74 |
+
text-decoration: underline;
|
| 75 |
+
}
|
| 76 |
+
.read-more i {
|
| 77 |
+
margin-left: 0.25rem;
|
| 78 |
+
}
|
| 79 |
+
</style>
|
| 80 |
+
<div class="news-card">
|
| 81 |
+
<img src="${image}" alt="${title}" class="news-image">
|
| 82 |
+
<div class="news-content">
|
| 83 |
+
<h3 class="news-title">${title}</h3>
|
| 84 |
+
<div class="news-meta">
|
| 85 |
+
<span>${source}</span>
|
| 86 |
+
<span>${date}</span>
|
| 87 |
+
</div>
|
| 88 |
+
<p class="news-summary">${summary}</p>
|
| 89 |
+
<a href="${url}" target="_blank" class="read-more">
|
| 90 |
+
Read more <i data-feather="arrow-right"></i>
|
| 91 |
+
</a>
|
| 92 |
+
</div>
|
| 93 |
+
</div>
|
| 94 |
+
`;
|
| 95 |
+
feather.replace();
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
customElements.define('news-card', NewsCard);
|
index.html
CHANGED
|
@@ -10,6 +10,7 @@
|
|
| 10 |
<script src="https://unpkg.com/feather-icons"></script>
|
| 11 |
<script src="components/navbar.js"></script>
|
| 12 |
<script src="components/notification-card.js"></script>
|
|
|
|
| 13 |
</head>
|
| 14 |
<body class="bg-gray-100 min-h-screen">
|
| 15 |
<custom-navbar></custom-navbar>
|
|
@@ -80,9 +81,40 @@
|
|
| 80 |
</button>
|
| 81 |
</form>
|
| 82 |
</div>
|
| 83 |
-
</main>
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
feather.replace();
|
| 87 |
</script>
|
| 88 |
<script src="script.js"></script>
|
|
|
|
| 10 |
<script src="https://unpkg.com/feather-icons"></script>
|
| 11 |
<script src="components/navbar.js"></script>
|
| 12 |
<script src="components/notification-card.js"></script>
|
| 13 |
+
<script src="components/news-card.js"></script>
|
| 14 |
</head>
|
| 15 |
<body class="bg-gray-100 min-h-screen">
|
| 16 |
<custom-navbar></custom-navbar>
|
|
|
|
| 81 |
</button>
|
| 82 |
</form>
|
| 83 |
</div>
|
|
|
|
| 84 |
|
| 85 |
+
<div class="mt-12">
|
| 86 |
+
<h2 class="text-2xl font-bold mb-6 text-gray-800">Latest News Updates</h2>
|
| 87 |
+
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
| 88 |
+
<news-card
|
| 89 |
+
title="New Tax Regulations Announced"
|
| 90 |
+
source="Financial Times"
|
| 91 |
+
date="2 hours ago"
|
| 92 |
+
summary="The government has announced new tax regulations that will affect small businesses starting next fiscal year."
|
| 93 |
+
image="http://static.photos/finance/640x360/1"
|
| 94 |
+
url="https://example.com/news/1">
|
| 95 |
+
</news-card>
|
| 96 |
+
|
| 97 |
+
<news-card
|
| 98 |
+
title="Tech Giant Releases Quarterly Report"
|
| 99 |
+
source="Tech News"
|
| 100 |
+
date="5 hours ago"
|
| 101 |
+
summary="Major tech company reports record profits in Q2, exceeding analyst expectations."
|
| 102 |
+
image="http://static.photos/technology/640x360/2"
|
| 103 |
+
url="https://example.com/news/2">
|
| 104 |
+
</news-card>
|
| 105 |
+
|
| 106 |
+
<news-card
|
| 107 |
+
title="Market Trends for 2023"
|
| 108 |
+
source="Business Insider"
|
| 109 |
+
date="1 day ago"
|
| 110 |
+
summary="Experts predict continued growth in the tech sector despite economic uncertainties."
|
| 111 |
+
image="http://static.photos/business/640x360/3"
|
| 112 |
+
url="https://example.com/news/3">
|
| 113 |
+
</news-card>
|
| 114 |
+
</div>
|
| 115 |
+
</div>
|
| 116 |
+
</main>
|
| 117 |
+
<script>
|
| 118 |
feather.replace();
|
| 119 |
</script>
|
| 120 |
<script src="script.js"></script>
|
script.js
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
document.addEventListener('DOMContentLoaded', function() {
|
| 2 |
-
|
|
|
|
| 3 |
setInterval(() => {
|
| 4 |
const cards = document.querySelectorAll('custom-notification-card');
|
| 5 |
cards.forEach(card => {
|
|
|
|
| 1 |
+
|
| 2 |
+
// News API integration
|
| 3 |
+
async function fetchNews() {
|
| 4 |
+
try {
|
| 5 |
+
const response = await fetch('https://newsapi.org/v2/top-headlines?country=in&category=business&apiKey=YOUR_API_KEY');
|
| 6 |
+
const data = await response.json();
|
| 7 |
+
|
| 8 |
+
if (data.articles && data.articles.length > 0) {
|
| 9 |
+
const newsContainer = document.querySelector('.grid.grid-cols-1.md\\:grid-cols-2.lg\\:grid-cols-3.gap-6');
|
| 10 |
+
if (newsContainer) {
|
| 11 |
+
// Clear placeholder cards
|
| 12 |
+
newsContainer.innerHTML = '';
|
| 13 |
+
|
| 14 |
+
// Add real news cards
|
| 15 |
+
data.articles.slice(0, 3).forEach(article => {
|
| 16 |
+
const newsCard = document.createElement('news-card');
|
| 17 |
+
newsCard.setAttribute('title', article.title || 'No title');
|
| 18 |
+
newsCard.setAttribute('source', article.source.name || 'Unknown source');
|
| 19 |
+
newsCard.setAttribute('date', new Date(article.publishedAt).toLocaleString());
|
| 20 |
+
newsCard.setAttribute('summary', article.description || 'No description available');
|
| 21 |
+
newsCard.setAttribute('image', article.urlToImage || 'http://static.photos/news/640x360/1');
|
| 22 |
+
newsCard.setAttribute('url', article.url || '#');
|
| 23 |
+
newsContainer.appendChild(newsCard);
|
| 24 |
+
});
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
} catch (error) {
|
| 28 |
+
console.error('Error fetching news:', error);
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
document.addEventListener('DOMContentLoaded', function() {
|
| 33 |
+
fetchNews();
|
| 34 |
+
// Simulate checking for updates
|
| 35 |
setInterval(() => {
|
| 36 |
const cards = document.querySelectorAll('custom-notification-card');
|
| 37 |
cards.forEach(card => {
|