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
50 changes: 14 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
# Responsive News Reader
# Overview

Let's create a responsive news reader website. To get the latest news the app will display we are going to use [News API](https://newsapi.org/). You will need to register with the service to obtain an API key.
Grabber is a simple news reader that pulls in current news circling Trump from all english-speaking countries and displays it vertically.

## Objectives
Articles are shown with any available image, description, title and date.

- [ ] Create a responsive layout that works well and looks good at mobile, tablet and desktop screen sizes. Please create the mobile version first, then tablet and then desktop.
Some tongue-in-cheek options for search refinement are given, as if to provide you with three wholly seperate perspectives.

- [ ] Fetch news articles from News API and display them including title, image, description, publication, date and link to article.
# Set Up

- [ ] Display images for tablet and desktop screens only

- [ ] Implement a feature of your choice

## Stretch goals

- [ ] Implement pagination which allows you to get the next 20 results and display them

- [ ] Add search functionality which allows the user to retrieve news about a particular topic

## Instructions

- Fork and clone this repo
- Commit your changes frequently with short and descriptive commit messages
- Create a pull request when complete
- We want to see great looking webpages
- Your code should have consistent indentation and sensible naming
- Use lots of concise functions with a clear purpose
- Add code comments where it is not immediately obvious what your code does

## Getting started

- Take a look at the API documentation to see what the response data will look like
- Start with pen and paper to draw what you want your new app to look like
- Create a basic HTML page with your layout
- Create a CSS file and a JS file and import them into the webpage.
- Use `fetch` load some news items from API and insert them into your web page.

## README.md

When finished, update this README.md file in your repo. This should explain what the project is, how to run it and how to use it. Someone who is not familiar with the project should be able to look at it and understand what it is and what to do with it. This is quite important as you want to put projects in your portfolio and the information provided here will help a reviewer understand what it is they are looking at.
Clone the repo
```
git clone [repo path]
```
Install project dependencies
```
npm install
```
Copy and paste the index.html path into your browser.
Binary file added images/flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Grabber</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
</head>

<body>
<section class="site__head">
<header class="masthead">

<a class="home" href="index.html"><h1 class="page__title">Grabber</h1></a>

<ul class="options">
<li id="trump">Planet Trump</li>
<li id="us">US</li>
<li id="gb">UK</li>
</ul>
<button class="refresh">Refresh Feed</button>
<!-- <img class="oi__cheeky" src="images/putin_smirk.png">-->
</header>
</section>

<section class="news__feed">
<ul class="articles__wrapper"></ul>
</section>

<section class="bottom">
<footer>
<button class="loadNext">Load More</button>
<div class="bottom__row">
<p class="author">Created by Luke Sikuade with the News API</p>
<button class="up">Back Up</button>
</div>
</footer>
</section>

</body>
<script src="src/fetchingData.js"></script>

</html>
201 changes: 201 additions & 0 deletions src/fetchingData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//Pagination
let pageNum = 1;
let todayGMT = new Date();
//Get yesterday's date
todayGMT.setDate(todayGMT.getDate() - 1);
let convertedToISO = todayGMT.toISOString();
let url = `https://newsapi.org/v2/everything?q=trump&language=en&from=${convertedToISO}&sortby=popularity&apiKey=2b19d03fc1cf4cd7b62ac5bb15c98827&page=${pageNum}`;
/*
Send the 20(default) articles through a series of
functions one at a time to populate our ul.
"uri" should be undefined the first time, but not subsequent times
for purposes of pagination.
*/
function grabData(uri) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than using if/else you can use default parameter values which will replace the parameter value when it is undefined. For example
function grabData(uri = url){
That way if uri is undefined, the url value will be applied instead

//Has something been passed into grabData()?
if (uri !== undefined) {
fetch(uri)
.then(response => {
return response.json();
})
.then(nextPageResults => {
console.log(uri);
console.log(nextPageResults);
passArticlesForCreation(nextPageResults);
});
} else {
fetch(url)
.then(response => {
return response.json();
})
.then(body => {
passArticlesForCreation(body);
});
}
}
const refreshButton = document.querySelector(".refresh");
refreshButton.addEventListener("click", refreshFeed);

const loadMoreButton = document.querySelector(".loadNext");
loadMoreButton.addEventListener("click", loadMore);

const topButton = document.querySelector(".up");
topButton.addEventListener("click", backUp);

const navList = document.querySelector(".options");
navList.addEventListener("click", function(event) {
if (event.target.id !== "trump") {
let country = event.target.id;
let page = 1;
let period = convertedToISO;
let newFeedUrl = `https://newsapi.org/v2/top-headlines?q=trump&language=en&from=${period}&sortby=popularity&country=${country}&apiKey=2b19d03fc1cf4cd7b62ac5bb15c98827&page=${page}`;
const feed = document.querySelector(".articles__wrapper");
feed.innerHTML = "";
grabData(newFeedUrl);
} else {
trumpify();
}
});

//Now this is what I call 'core functionality'.
function trumpify() {
console.log("present");
const title = document.querySelector(".page__title");
title.textContent = "Grabber by the p***y";
const articles = document.querySelectorAll(".article__whole");
console.log(articles[1].childNodes);
articles.forEach(article => {
article.childNodes[0].childNodes[0].textContent = randomPraise();
article.childNodes[0].childNodes[0].style.backgroundColor = "#fff";
article.style.backgroundImage = "url('images/flag.png')";
});
}
//Probably should be in own JSON file.
function randomPraise() {
let praise = [
"Trump did nothing wrong.",
"Global superstar president Trump turns down nobel prize.",
"World peace declared under the greatest man to ever live, Donald Trump.",
"American masses call for all faces on Mt. Rushmore be replaced with Trump's dynamic visage.",
"The President has saved the lives of five local firemen.",
"Nine people confirmed better off under President Trump.",
"The President of the United States is a stand up dude.",
"'Make America Great Again' to be title of Spielberg's new biopic.",
"Quentin Tarantino 'okay, for a Jew' declares Trump magnanamously.",
"Trump to fight for 'bigger' womens rights.",
"New study shows: If you don't like the President... you're a communist.",
"Oh say can you see, that Trump is the greatest.",
"Fox News declares fealty to the legend himself.",
"Melania's US birth certificate miraculously found in the hands of Administration intern.",
"Tariffs - too short a word for lexicographer Trump. Should be Tariffics.",
"'Organge is the new black. You're all racist.' Declares Trump.",
"President orders new mandates for pronunciation: 'The Jina Ammendment'.",
"Lord of the Rings to be reshot with the President.",
"Trump overheard:'Kim Jong Un... sounds delicious, when's dinner?'",
"Hillary Clinton to be forcibly renamed Shrek, under Federal Law.",
"The USA to be henceforth referred to in full, according to President Donald J Trump, Great Proclaimer, Holy Unifier and Friend to Small Woodland Creatures."
];
let rand = praise[Math.floor(Math.random() * praise.length)];
return rand;
}

function backUp() {
window.scroll(0, 0);
}

function refreshFeed() {
const title = document.querySelector(".page__title");
title.textContent = "Grabber";
//Clear the feed.
const feed = document.querySelector(".articles__wrapper");
feed.innerHTML = "";
//Fill the feed.
grabData();
}

function loadMore() {
pageNum++;
let period = convertedToISO;
let pageChange = pageNum++;
let updated = `https://newsapi.org/v2/everything?q=trump&language=en&from=${period}&sortby=popularity&apiKey=2b19d03fc1cf4cd7b62ac5bb15c98827&page=${pageChange}`;
grabData(updated);
}

function passArticlesForCreation(jsonData) {
for (let i = 0; i < jsonData.articles.length; i++) {
createListElement(jsonData.articles[i]);
}
}

//As they pass in, create list elements from them.
//Call populateNewsFeed on each one.
function createListElement(articleObject) {
/*CREATE THE LIST ITEM WITH ATTRIBUTES AND ARTICLE CONTENT */
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be easier to generate using HTML string templates?

//List element
const article = document.createElement("li");
article.className = "article__whole";
//Title
const articleLink = document.createElement("a");
articleLink.setAttribute("href", articleObject.url);
articleLink.className = "article__link";

const articleTitle = document.createElement("h2");
articleTitle.textContent = articleObject.title;

articleTitle.className = "article__title";

//Image and description
const imageDescWrapper = document.createElement("span");
imageDescWrapper.className = "image__desc";

const articleImage = document.createElement("img");
articleImage.setAttribute("src", articleObject.urlToImage);
articleImage.className = "article__image";

const articleDescription = document.createElement("p");
articleDescription.textContent = articleObject.content;
articleDescription.className = "article__describe";

//Date and publication
const datePubWrapper = document.createElement("span");
datePubWrapper.className = "date__pub";

const articleDate = document.createElement("p");
const pubDate = new Date(articleObject.publishedAt);
const ammendedDate = pubDate
.toString()
.replace("GMT+0100 (British Summer Time)", "");
articleDate.textContent = ammendedDate;
articleDate.className = "article__date";

const articlePub = document.createElement("p");
articlePub.textContent = articleObject.source.name;

/*ASSEMBLE THE LIST ITEM*/
articleLink.appendChild(articleTitle);
article.appendChild(articleLink);

imageDescWrapper.appendChild(articleImage);
imageDescWrapper.appendChild(articleDescription);
article.appendChild(imageDescWrapper);

datePubWrapper.appendChild(articleDate);
datePubWrapper.appendChild(articlePub);
article.appendChild(datePubWrapper);

populateNewsFeed(article);
}

// Populate the created list elements:
function populateNewsFeed(article) {
feed = document.querySelector(".articles__wrapper");
if (pageNum === 1) {
feed.prepend(article);
} else {
feed.appendChild(article);
}
}

//PROGRAM START
document.addEventListener("DOMContentLoaded", grabData());
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

document.querySelector(".options").style.cursor = "pointer";
Loading