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

Added option to sort by date published and popularity for news #16

Open
wants to merge 4 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
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ <h3>Choose from a list of news sources to get the latest popular headlines. Powe
</div>
</header>
<main class="display-area">

<select id="news-selector"></select>
<select id="sortby-selector"></select>

<div class="story-section visible" id="news-list"></div>
</main>
<div class="footer">
Expand Down
17 changes: 15 additions & 2 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ const apiKey = 'XXXXXXXXXXXX'; // 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');
const defaultSortby = 'top'
const sortbySelcector = document.querySelector('#sortby-selector')
const sortbyOptions = [
{ visibleOption : 'Top', query: 'top'},
{ visibleOption : 'Date Published', query: 'publishedAt'},
{ visibleOption : 'Popularity', query: 'popularity'}
]

if ('serviceWorker' in navigator) {
window.addEventListener('load', () =>
Expand All @@ -16,6 +23,10 @@ window.addEventListener('load', e => {
sourceSelector.value = defaultSource;
updateNews();
});
sortbySelcector.innerHTML =
sortbyOptions.map( option => `<option value="${option.query}">${option.visibleOption}</option>`)

sortbySelcector.addEventListener('change', evt => updateNews(sourceSelector.value,evt.target.value))
});

window.addEventListener('online', () => updateNews(sourceSelector.value));
Expand All @@ -29,15 +40,17 @@ async function updateNewsSources() {
.join('\n');
}

async function updateNews(source = defaultSource) {
async function updateNews(source = defaultSource,sortby = defaultSortby) {
newsArticles.innerHTML = '';
const response = await fetch(`https://newsapi.org/v2/top-headlines?sources=${source}&sortBy=top&apiKey=${apiKey}`);
const response = await fetch(`https://newsapi.org/v2/everything?sources=${source}&sortBy=${sortby}&apiKey=${apiKey}`);
const json = await response.json();
console.log('Articles fetched', json);
newsArticles.innerHTML =
json.articles.map(createArticle).join('\n');
}



function createArticle(article) {
return `
<a class="story" href="${article.url}">
Expand Down