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

first draft #9

Open
wants to merge 1 commit 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
47 changes: 47 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!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="myCSS.css">
<title>Document</title>
</head>

<body>
<div class="container">
<div class="header">
<h1 class="header__logo">
<u>News Reader</u>
</h1>
<ul class="header__buttons">
<li id="bbc" class="header__buttons__li">BBC news</li>
<li id="top" class="header__buttons__li">Top headlines</li>
<li class="header__buttons__li">button 3</li>
</ul>
</div>

<div class="content">
<div class="content__section1">News search
<form id='searchfield'>
<input placeholder="Search here...">
</input>
<button>search</button>
</form>
</div>
<div id="newsbody" class="content__body">News:</div>
<!-- <div id="photobody" class="content__section3">Section 3</div> -->
</div>


<div class="footer">
<div class="footer_section1">Footer 1</div>
<div class="footer_section2">Footer 2</div>
<div class="footer_section3">Footer 3</div>
</div>
</div>
<script src="index.js" type="text/javascript"></script>
</body>

</html>
100 changes: 100 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const newsDisplay = document.querySelector("#newsbody");
const photodisplat = document.querySelector("#photopbody");
const form = document.querySelector("#searchfield");
const input = document.querySelector("input");
const bbcButton = document.querySelector("#bbc");
const topHeadlines = document.querySelector("#top");

fetch(
"https://newsapi.org/v2/top-headlines?" +
"country=us&" +
"apiKey=1a4b48d17f0e44148656196d121fb9c0"
)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
displayNews(data);
})
.catch(function(error) {
console.log(error);
});

function displayNews(data) {
const html = data.articles
.map(function(news) {
return `
<div class="newscontainer">
<div class="newsfeed">
<p><strong>${news.title}</strong>
<span> (${news.source.name})</span>
</p>
<a href="${news.url}"
<p2>${news.description}</p2></a>
</div>
<div><img class="newsimage" src="${news.urlToImage}" alt=""></div>
</div>
`;
})
.join("");

newsDisplay.innerHTML = html;
}

form.addEventListener("submit", function(event) {
event.preventDefault();
let searchContent = input.value;
fetch(
`https://newsapi.org/v2/everything?` +
`q=${searchContent}&` +
`from=2018-06-15&` +
`sortBy=popularity&apiKey=1a4b48d17f0e44148656196d121fb9c0`
)
.then(function(response) {
return response.json();
})
.then(function(data) {
displayNews(data);
})
.catch(function(error) {
console.log(error);
});
});

bbcButton.addEventListener("click", function(event) {
event.preventDefault();
fetch(
`https://newsapi.org/v2/top-headlines?` +
`sources=bbc-news&` +
`apiKey=1a4b48d17f0e44148656196d121fb9c0`
)
.then(function(response) {
return response.json();
})
.then(function(data) {
displayNews(data);
})
.catch(function(error) {
console.log(error);
});
});

topHeadlines.addEventListener("click", function(event) {
event.preventDefault();
fetch(
"https://newsapi.org/v2/top-headlines?" +
"country=us&" +
"apiKey=1a4b48d17f0e44148656196d121fb9c0"
)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
displayNews(data);
Copy link
Contributor

Choose a reason for hiding this comment

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

I like how the logic is broken out into own function

})
.catch(function(error) {
console.log(error);
});
});
177 changes: 177 additions & 0 deletions myCSS.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
* {
box-sizing: border-box;
}

body {
margin: 0;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

.container {
display: flex;
flex-direction: column;
height: 100vh;
}

.header {
/* background-color: darksalmon; */
display: flex;
flex-direction: column;
text-align: center;
/* height: 10vh; */
padding: 10px;
}

.header__logo {
margin: 0;
font-weight: 700;
background-color: rgba(45, 45, 45, 0.2);
}

.header__buttons {
margin: 0;
padding: 0;
display: flex;
list-style: none;
justify-content: center;
}

.header__buttons__li {
margin-right: 10px;
border: solid 1px;
border-radius: 5px;
text-align: center;
border-radius: 5px;
text-justify: auto;
padding: 2px;
}

.content {
display: flex;
flex-direction: column;
height: 80vh;
justify-content: space-evenly;
text-align: center;
}

.content__section1 {
background-color: rgba(210, 235, 181, 0.9);
flex: 1;
background-image: url(https://images.unsplash.com/photo-1484807088772-e156a5761699?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=c3bffb018350a4ea518e56b1be15ac19&auto=format&fit=crop&w=1951&q=80);
background-position: center;
border-radius: 20px;
}
.newsfeed {
float: left;
}
.newscontainer {
display: flex;
flex-direction: row;
font-size: 15px;
float: left;
padding: 10px;
border: solid 0.5px;
border-radius: 30px;
border-color: rgba(88, 85, 85, 1);
text-align: center;
justify-content: center;
}

.newsimage {
display: none;
height: 200px;
width: 200px;
border-radius: 25px;
justify-content: flex-end;
padding: 4px;
}

.content__body {
background-color: rgba(228, 235, 150, 0.1);
flex: 5;
overflow: auto;
padding: 10px;
}

/* .content__section3 {
background-color: mediumslateblue;
display: none;
flex: 2;
} */

.footer {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 10vh;
/* text-align: center; */
margin: 0;
}

.footer_section1 {
background-color: rgba(84, 81, 81, 0.2);
flex: 1;
text-align: center;
}

.footer_section2 {
background-color: rgba(84, 81, 81, 0.3);
flex: 1;
text-align: center;
}

.footer_section3 {
background-color: rgba(84, 81, 81, 0.5);
flex: 1;
text-align: center;
}

@media (min-width: 768px) {
.content__section1 {
flex: 1;
}

.content__body {
flex: 3;
}

.content {
flex-direction: row;
}
}

@media (min-width: 960px) {
.content__section1 {
flex: 1;
}

.footer {
display: flex;
flex-direction: row;
flex: 1;
text-align: center;
}

.newsimage {
display: block;
}
}

@media (min-width: 1200px) {
.content__body {
flex: 3;
}
.content__section1 {
flex: 1;
}

.content {
flex-direction: row;
}

.footer {
display: flex;
flex-direction: row;
flex: 1;
}
}