Skip to content
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
Binary file added .DS_Store
Binary file not shown.
Binary file added DaveNewslogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>DaveNews</title>
</head>

<body>
<div class="app">
<header class="header">
<h1>DaveNews</h1>
<img class="logo" src="DaveNewslogo.png">
</header>
<div class="search_container">
<form class="search_form">
<input class="search_box" placeholder="Search..."></textarea>
<button class="search_button" type="submit">Search</button>
</form>
</div>
<div class="content_body">
<div class="news_content">
</div>
<div class="news_sidebar">
</div>
</div>
<div class="button_container">
<button class="load_more_button">Load more articles</button>
</div>
<div class="footer">
<p>DaveNews</p>
</div>
</div>
</body>
<script src="index.js"></script>
</html>
157 changes: 157 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//cd79c8caed5c47f4ae25ec30f7cca674 is API key


//Defaults search term to UK news, page 1
let pageNumberGlobal = 1;
let searchResult = "uk";
upDateUrl(pageNumberGlobal, searchResult);
updateSideBarUrl(pageNumberGlobal);

//Increases page number by 1 everytime the load more button is pressed
const loadMoreButton = document.querySelector(".load_more_button");
loadMoreButton.addEventListener("click", function (event){
pageNumberGlobal ++;
upDateUrl(pageNumberGlobal, searchResult);
updateSideBarUrl(pageNumberGlobal);
});


//Creates and updates search term, defaults page number to 1
let searchInput = document.querySelector(".search_form");
searchInput.addEventListener("submit", function(event){
let searchContent = document.querySelector(".search_box").value;
let previousArticles = document.querySelectorAll(".article_box");
event.preventDefault();
searchResult = searchContent;
previousArticles.forEach(node => {
node.parentNode.removeChild(node);
});
upDateUrl(1, searchResult);
});



//Updates search URL with search terms and page numbers
function upDateUrl(number, term) {
let defaultUrlFirst = "https://newsapi.org/v2/everything?q=";
let defaultUrlSecond = "&language=en&sortBy=publishedAt&apiKey=cd79c8caed5c47f4ae25ec30f7cca674&page="
newsUrl = defaultUrlFirst + term + defaultUrlSecond + number;
generateNews(newsUrl);
};


//Fetches news API
function generateNews(url) {
fetch(url)
.then(response => {
// console.log("promise has been resolved")
return response.json();
})
.then(body => {
// pullNewsObject(body);
createNewsContent(body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

});
}


// Exploratory function to examin keys in news JSON object.

function pullNewsObject(news) {
console.log(news);
news.articles.forEach(article => {
console.log(article);
})
};


//Create .innerHTML of each news article div.

function articleTemplate (article) {
let authorVar = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code would be slightly cleaner if declaration and assignment was combined into single step. That would also allow you to use const instead of let

let sourceVar = "";
let imgVar = "";
let textVar = "";
(article.author === null) ? authorVar = "" : authorVar = article.author;
(article.source.name === null) ? sourceVar = "" : sourceVar = article.source.name;
(article.urlToImage === null) ? imgVar = "jc-gellidon-714673-unsplash.jpg" : imgVar = article.urlToImage;
(article.description === null) ? textVar = article.content : textVar = article.description;
return `
<h2 class="article_title">${article.title}</h2>
<div class="image_container"><img class="article_image" src="${imgVar}"></div>
<p class="article_content">${textVar}</p>
<ul class="article_author_source_list">
<li class="author_item">${authorVar}</li>
<li class="source_item">${sourceVar}</li>
</ul>
<a class="article_link" href="${article.url}">Full article available here</a>
`
}

//Appends each div to the content container.

function createNewsContent(news) {
news.articles.forEach(item => {
if (item.content !== null && item.source.name !== "Bloomberg") {
const contentBody = document.querySelector(".news_content");
const articleBox = document.createElement("div");
articleBox.className = "article_box";
articleBox.innerHTML = articleTemplate(item)
contentBody.append(articleBox);
}
})
}

//Fetches items for the sidebar

function generateSideBar(url) {
fetch(url)
.then(response => {
// console.log("promise has been resolved")
return response.json();
})
.then(body => {
// pullNewsObject(body);
createSideBarContent(body);
});
}




//Adds more items to the sidebar as the page number for main article increases
function updateSideBarUrl(number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but I would suggest not putting the fetch call in this function. Use this function just to generate the URL and return it. Then the returned value can be used to call generateSideBar. This will reduce the amount of responsibility this function has and make it easier to test later as we will discuss later this week.

let sidebarUrl = "https://newsapi.org/v2/everything?q=celebrity&q=hollywood&q=showbix&popculture&language=en&totalResults=10&sortBy=publishedAt&apiKey=cd79c8caed5c47f4ae25ec30f7cca674&page=";
sidebarUrl = sidebarUrl + number;
generateSideBar(sidebarUrl);
};


//Creates template for sidebar articles
function sideBarTemplate (article) {
let authorVar = "";
let sourceVar = "";
(article.author === null) ? authorVar = "" : authorVar = article.author;
(article.source.name === null) ? sourceVar = "" : sourceVar = article.source.name;
return `
<h2 class="sidebar_title">${article.title}</h2>
<div class="sidebar_image_container"><img class="article_image" src="${article.urlToImage}"></div>
<ul class="sidebar_source_list">
<li class="sidebar_source_item">${sourceVar}</li>
</ul>
<a class="sidebar_link" href="${article.url}">Full article available here</a>
`
}

//Appends all new content to sidebar

function createSideBarContent(news) {
news.articles.forEach(item => {
if (item.content !== null && item.source.name !== "Bloomberg") {
const sideBar = document.querySelector(".news_sidebar");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

featureBox and sideBar lookup can be taken outside the forEach. Will save you having to run querySelector up on each loop iteration.

const featureBox = document.createElement("div");
featureBox.className = "feature_box";
featureBox.innerHTML = sideBarTemplate(item)
sideBar.append(featureBox);
}
})
}
Binary file added jc-gellidon-714673-unsplash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading