Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for displaying published date in news card #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ header h3 {
/* align-items: center; */
border-radius: 1px;
transition: .75s;
position: relative;
}

.story:hover {
Expand Down Expand Up @@ -137,6 +138,19 @@ a {
margin-bottom: 15px;
}

.published-date {
font-size: 12px;
font-weight: 400;
margin: 0.5rem;
}

.published-date span {
position: absolute;
bottom: 0;
right: 0;
padding: 0.5rem;
}

.footer {
position: fixed;
left: 0;
Expand Down
19 changes: 14 additions & 5 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const apiKey = 'XXXXXXXXXXXX'; // Get API key from NewsAPI.org
const apiKey = 'XXXXXXXXXXXXX'; // Get API key from NewsAPI.org
const defaultSource = 'the-hindu'; // default source of news
const sourceSelector = document.querySelector('#news-selector');
const newsArticles = document.querySelector('#news-list');

if ('serviceWorker' in navigator) {
window.addEventListener('load', () =>
navigator.serviceWorker.register('./serviceWorker.js')
.then(registration => console.log('Service Worker registered'))
.catch(err => 'Service worker registration failed'));
.then(registration => console.log('Service Worker registered'))
.catch(err => 'Service worker registration failed'));
}

window.addEventListener('load', e => {
Expand All @@ -25,8 +25,8 @@ async function updateNewsSources() {
const json = await response.json();
sourceSelector.innerHTML =
json.sources
.map(source => `<option value="${source.id}">${source.name}</option>`)
.join('\n');
.map(source => `<option value="${source.id}">${source.name}</option>`)
.join('\n');
}

async function updateNews(source = defaultSource) {
Expand All @@ -45,6 +45,15 @@ function createArticle(article) {
<p class="headline">${article.title}</p>
<p class="author">${article.author ? article.author : ''}</p>
<p class="description">${article.description}</p>
<p class="published-date"><span>${"published on " + parseDate(article.publishedAt)}</span></p>
</a>
`;
}

function parseDate(dateString) {
const options = {
year: 'numeric', month: 'short', day: 'numeric'
};
const date = new Date(dateString);
return `${date.toLocaleDateString('en-US', options)}`;
}