-
Notifications
You must be signed in to change notification settings - Fork 34
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
week3 responsive-news-reader #7
base: master
Are you sure you want to change the base?
Changes from 1 commit
04ccf0d
9889d48
87a9f34
f9b733c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,19 @@ | |
const myMain = document.querySelector(".content__body"); | ||
const searchBox = document.querySelector(".searchform__textbar"); | ||
const myForm = document.querySelector(".searchform"); | ||
const prev= document.querySelector(".footer__prev"); | ||
const next= document.querySelector(".footer__next"); | ||
const sideBar= document.querySelector(".content__sidebar"); | ||
let page=1; | ||
let search=""; | ||
let txt=""; | ||
|
||
//functions | ||
|
||
|
||
function submitHandler(event) { | ||
event.preventDefault(); | ||
let search= searchBox.value; | ||
search= searchBox.value; | ||
fetch( | ||
`https://newsapi.org/v2/everything?q=${search}&from=2018-06-01&sortBy=popularity&apiKey=cacb9f078d714b02b4baa44f3eea29f8` | ||
) | ||
|
@@ -15,7 +23,9 @@ function submitHandler(event) { | |
}) | ||
.then(function(jsonData) { | ||
showResults(jsonData); | ||
console.log(jsonData); | ||
prev.style.display="block"; | ||
next.style.display="block"; | ||
|
||
}) | ||
.catch(function(error) { | ||
alert("error"); | ||
|
@@ -25,14 +35,13 @@ function submitHandler(event) { | |
function showResults(data){ | ||
let style=""; | ||
let counter=1; | ||
console.log(data) | ||
const news= data.articles.map(article => { | ||
if(counter ==1) | ||
style="article1" | ||
else | ||
style="article2" | ||
counter=counter*-1 | ||
return `<div class="${style}"><a href="${article.url}"><h2>Title: ${article.title}</h2></a><br> | ||
return `<div class="${style}"><a href="${article.url}"><h2>${article.title}</h2></a><br> | ||
<img class="article__img" src="${article.urlToImage}"></img> | ||
Description: ${article.description}.<br> | ||
Publication Date: ${article.publishedAt}.<br> | ||
|
@@ -41,5 +50,89 @@ function showResults(data){ | |
myMain.innerHTML=news; | ||
} | ||
|
||
function nextHandler(event){ | ||
page++; | ||
fetch( | ||
`https://newsapi.org/v2/everything?q=${search}&from=2018-06-01&page=${page}&sortBy=popularity&apiKey=cacb9f078d714b02b4baa44f3eea29f8` | ||
) | ||
.then(function(response) { | ||
return response.json(); | ||
}) | ||
.then(function(jsonData) { | ||
window.scrollTo(0, 0); | ||
showResults(jsonData); | ||
}) | ||
.catch(function(error) { | ||
alert("error"); | ||
}); | ||
} | ||
|
||
function prevHandler(event){ | ||
if(page<=1){ | ||
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. Good handling of edge case. Rather than using if/else I would return on the if to remove the need for an else. if(page<=1){
alert("This is the first page");
return;
} |
||
alert("This is the first page") | ||
} | ||
else{ | ||
page--; | ||
fetch( | ||
`https://newsapi.org/v2/everything?q=${search}&from=2018-06-01&page=${page}&sortBy=popularity&apiKey=cacb9f078d714b02b4baa44f3eea29f8` | ||
) | ||
.then(function(response) { | ||
return response.json(); | ||
}) | ||
.then(function(jsonData) { | ||
window.scrollTo(0, 0); | ||
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 |
||
showResults(jsonData); | ||
}) | ||
.catch(function(error) { | ||
alert("error"); | ||
}); | ||
} | ||
} | ||
|
||
function headlines(){ | ||
|
||
fetch( | ||
`https://newsapi.org/v2/top-headlines?country=us&apiKey=cacb9f078d714b02b4baa44f3eea29f8` | ||
) | ||
.then(function(response) { | ||
return response.json(); | ||
}) | ||
.then(function(jsonData) { | ||
for(let i=0;i<5;i++){ | ||
sideBar.innerHTML+= `<a href="${jsonData.articles[i].url}">${jsonData.articles[i].title}</a><br><br>` | ||
} | ||
}) | ||
.catch(function(error) { | ||
alert("error"); | ||
}); | ||
} | ||
|
||
//body | ||
myForm.addEventListener("submit", submitHandler); | ||
prev.addEventListener("click",prevHandler); | ||
next.addEventListener("click",nextHandler); | ||
headlines(); | ||
///////// | ||
/*var client = new XMLHttpRequest(); | ||
client.open('GET', 'mytxt.txt'); | ||
client.onreadystatechange = function() { | ||
//alert(client.responseText); | ||
txt=this.responseText; | ||
console.log(txt); | ||
|
||
} | ||
client.send(); | ||
|
||
|
||
var text = "hello world", | ||
blob = new Blob([text], { type: 'text/plain' }), anchor = document.createElement('a'); | ||
*/ | ||
var obj = { | ||
name: 'Ram', | ||
score: 100 | ||
}; | ||
|
||
localStorage.setItem('gameStorage', JSON.stringify(obj)); | ||
|
||
var obj2 = JSON.parse(localStorage.getItem('gameStorage')); | ||
console.log(obj2); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ab | ||
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. maybe add this to .gitignore |
||
cd | ||
ef |
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.
nice