-
Notifications
You must be signed in to change notification settings - Fork 34
News Site #23
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
base: master
Are you sure you want to change the base?
News Site #23
Changes from all commits
c38ffe8
ab94ccd
53a883d
3268df0
e663ba6
09ac27a
68b1040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <link rel="stylesheet" href="style.css"> | ||
| <title>End of Days</title> | ||
| <link href="https://fonts.googleapis.com/css?family=Encode+Sans:300,400,700" rel="stylesheet"> | ||
| </head> | ||
| <body> | ||
| <header class="heading"> | ||
| <h1>THE END IS NIGH</h1> | ||
| <form class='form__search'> | ||
| <input class="searchClass" type="search" results=5 placeholder="Search me"> | ||
| <button class="searchB">Search</button> | ||
| </form> | ||
| </header> | ||
|
|
||
|
|
||
| <div class="hero"> | ||
| <ul id="headline"> | ||
| </ul> | ||
| <div class="infoG"> | ||
| <ul id="graphic"> | ||
| <li id="one"></li> | ||
| <li id="two"></li> | ||
| <li id="three"></li> | ||
| </ul> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="buttons"> | ||
| <button value="all-button" class="all-button">All News</button> | ||
| <button value="plague" class="all-button">Plague</button> | ||
| <button value="bitcoin" class="all-button">Bitcoin</button> | ||
| <button value="locusts" class="all-button">Locusts</button> | ||
| <button value="trump" class="all-button">Piggy</button> | ||
| <button value="brexit" class="all-button">Brexit</button> | ||
| </div> | ||
|
|
||
|
|
||
| <ul id="newz"> | ||
|
|
||
| </ul> | ||
|
|
||
| <!-- <button class="pag-button"></button> --> | ||
|
|
||
|
|
||
| <script src="news.js"></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| function displayErrorToUser(message){ | ||
| return console.log(message) | ||
| } | ||
|
|
||
| const parentNode = document.getElementById("newz"); // find the <ul in the html file | ||
|
|
||
| // Main News div insertion | ||
| function displayDataOnPage(topNews, counter){ | ||
| // console.log(topNews); | ||
| topNews.articles.forEach(content => { | ||
| const spanNode = document.createElement("li"); // make fresh <li> | ||
| spanNode.innerHTML = // news contents | ||
| `<a href="${content.url}"> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation here would make the HTML easier to read |
||
| <img src="${content.urlToImage}" alt="news image"> | ||
| <h3>${content.source.name}</h3> | ||
| <h2>${content.title}</h2> | ||
| <p>${content.description}</p></a>`; | ||
| parentNode.appendChild(spanNode); // pushing to the <ul> | ||
| }); | ||
| } | ||
| // Buttons event listener/value to fetch | ||
| const buttonSelector = document.querySelector('.buttons'); | ||
| buttonSelector.addEventListener("click", function(event){ | ||
| event.preventDefault(); | ||
| let url; | ||
| if (event.target.value === 'all-button') { | ||
| url = 'https://newsapi.org/v2/top-headlines?country=gb&apiKey=534d9b30f7bd4185b60cba8d406e11ec' | ||
| } | ||
| else { | ||
| url = `https://newsapi.org/v2/everything?q=${event.target.value}&apiKey=534d9b30f7bd4185b60cba8d406e11ec` | ||
| } | ||
|
|
||
| getData(url); | ||
| }); | ||
|
|
||
| //search event listener - BROKEN | ||
| const searchFunc = document.querySelector('.form__search'); | ||
| searchFunc.addEventListener("submit", function(event){ //COMBINE SEARCH AND INPUT?? | ||
| event.preventDefault(); | ||
| console.log(event.target['0'].value); // get the first item in teh form element. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to get the input using a selector rather than by index. That way if HTML gets moved around it will still work. |
||
| //WHAT IS VALUE??? GOOGLE IT | ||
| let url = `https://newsapi.org/v2/everything?q=${event.target['0'].value}&apiKey=534d9b30f7bd4185b60cba8d406e11ec` | ||
| getData(url); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| //button function | ||
| function fetchingButtons(url, selector) { //button fetch addresses | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function can be removed as it does not get called |
||
| const buttonall = document.querySelector(selector); | ||
| } | ||
|
|
||
| //Fetch | ||
| function getData(url){ | ||
| // main news body fetch - button changeable | ||
| fetch(url) // by default fetch makes a GET request | ||
| .then(function(response) { | ||
|
|
||
| return response.json(); | ||
| }) | ||
| .then(function(body){ | ||
| parentNode.innerHTML = ""; | ||
| displayDataOnPage(body); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
| }) | ||
| .catch(function(error) { | ||
| displayErrorToUser(`${error} ahhhh server problem`); | ||
| }); | ||
| } | ||
| getData('https://newsapi.org/v2/top-headlines?country=gb&pageSize=100&apiKey=534d9b30f7bd4185b60cba8d406e11ec') | ||
|
|
||
|
|
||
|
|
||
| //HEADLINE FETCH/DISPLAY | ||
|
|
||
| const parentNode2 = document.getElementById("headline"); // find the <ul in the html file | ||
|
|
||
| // Main News div insertion | ||
|
|
||
| function displayDataOnHead(topNews){ | ||
| // console.log(topNews); | ||
|
|
||
| topNews.articles.forEach(content => { | ||
| const spanNode2 = document.createElement("li"); // make fresh <li> | ||
| spanNode2.innerHTML = // news contents | ||
| `<a href="${content.url}"> | ||
| <img src="${content.urlToImage}" alt="news image"> | ||
| <h2>${content.title}</h2></a>`; | ||
|
|
||
| parentNode2.appendChild(spanNode2); // pushing to the <ul> | ||
| }); | ||
| } | ||
|
|
||
| function getDataH(urlH){ | ||
| // main news body fetch - button changeable | ||
| fetch(urlH) // by default fetch makes a GET request | ||
| .then(function(response) { | ||
|
|
||
| return response.json(); | ||
| }) | ||
| .then(function(body){ | ||
| // parentNode.innerHTML = ""; | ||
| displayDataOnHead(body); | ||
| }) | ||
| .catch(function(error) { | ||
| displayErrorToUser2(`${error} ahhhh server problem`); | ||
| }); | ||
| } | ||
|
|
||
| getDataH('https://newsapi.org/v2/top-headlines?sources=bbc-news&pagesize=1&apiKey=676d93104a84419493a5f6fa8fbdb6ed') | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // // Infographic FUNCTION/FETCH | ||
| const parentNode3 = document.getElementById("#graphic"); // find the <ul in the html file | ||
|
|
||
| function displayDataOnInfo(newsSource){ | ||
| newsSource.articles.forEach(content => { | ||
| const spanNode3 = document.createElement("li"); | ||
| spanNode3.attribute | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are quite a few empty lines around. It would be better to remove them and keep empty lines to just one. It will make code easier to read. |
||
|
|
||
|
|
||
| parentNode3.appendChild(spanNode3); | ||
|
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| function getDataI(urlI){ | ||
| // main news body fetch - button changeable | ||
| fetch(urlI) // by default fetch makes a GET request | ||
| .then(function(response) { | ||
|
|
||
| return response.json(); | ||
| }) | ||
| .then(function(body){ | ||
| // parentNode.innerHTML = ""; | ||
| displayDataOnInfo(body); | ||
| }) | ||
| .catch(function(error) { | ||
| displayErrorToUser3(`${error} ahhhh server problem`); | ||
| }); | ||
| } | ||
|
|
||
| getDataI('https://newsapi.org/v2/everything?q=bitcoin&pageSize=1&from=2018-08-16&sortBy=publishedAt&apiKey=534d9b30f7bd4185b60cba8d406e11ec') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean indentation please