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

Responsive news reader complete #6

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
41 changes: 41 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!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" type=text/css href="main.css">
<title> news reader </title>
</head>

<body>
<div class="app">

<header class="masterhead">
<h1 class="masterheader__logo">news reader</h1>
<div class="search_container">
<form class="search" id="search">
<label class="search__label" for="search-tf">search word</label>
<input class="search__input" id="search-tf" name="keyword" placeholder="Enter a keyword" autocomplete="keyword" />
<button class="btn search__btn">Go</button>
</form>
</div>
</header>

<div class="content">
<main class="content__body">
<p>main content</p>
</main>
</div>

<footer class="footer">
<div class="footer__notes">Coded for Constructor Labs</div>
<address class="footer__address">Address:1-3 Rivington St, London EC2A 3DT</address>
<div class="footer__map"><script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script><div style='overflow:hidden;height:50px;width:250px;'><div id='gmap_canvas' style='height:50px;width:250px;'></div><div><small><a href="embedgooglemaps.com/">https://embedgooglemaps.com/</a></small></div><div><small><a href="http://promocode.com.ph/">promo code com</a></small></div><style>#gmap_canvas img{max-width:none!important;background:none!important}</style></div><script type='text/javascript'>function init_map(){var myOptions = {zoom:15,center:new google.maps.LatLng(51.5261886,-0.0833589999999731),mapTypeId: google.maps.MapTypeId.ROADMAP};map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);marker = new google.maps.Marker({map: map,position: new google.maps.LatLng(51.5261886,-0.0833589999999731)});infowindow = new google.maps.InfoWindow({content:'<strong>Constructor Labs</strong><br>1-3 Rivington St, London EC2A 3DTLondon, United Kingdom<br>'});google.maps.event.addListener(marker, 'click', function(){infowindow.open(map,marker);});infowindow.open(map,marker);}google.maps.event.addDomListener(window, 'load', init_map);</script>
</footer>

</div>
<script src="main.js"></script>
</body>
</html>
240 changes: 240 additions & 0 deletions main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
*{
box-sizing: border-box;
}

body{
margin:0;
font-family: Helvetica;
}

.app{
text-align: center;
color: black;
display:flex;
flex-direction: column;
height: 100vh;
background-image: url(https://images.unsplash.com/photo-1509624776920-0fac24a9dfda?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=42ec196ddd2f51e9dbb7bfe0adcbaa3b&auto=format&fit=crop&w=800&q=60);

}

.masterhead{
font-style: italic;
text-transform: capitalize;
display:flex;
flex-direction: column;
justify-content: space-between;

}


.search_container{
display:flex;
}

.search,
.search__input,
.btn {
transition: 0.5s background-color, 0.5s color;
padding: 5px 10px;
border: none;
border-radius: 9999px;
font: inherit;
background: #fff;
}

.btn {
text-align: center;
}

.btn:hover {
background: #333;
color: #ccc;
}

.search {
display: flex;
margin: auto;
padding: 2px 2px 2px 10px;
font-size: 16px;
background: #333;
color: #ccc;
}

.search__label {
align-self: center;

margin-right: 5px;
}

.search__input {
flex: 1;

border-top-right-radius: 0;
border-bottom-right-radius: 0;
background: #fff;
}

.search__btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
text-transform: uppercase;
background: #666;
color: #fff;
}


.content{
display: flex;
flex-direction: column;
flex:1;
}


.content__body{
flex:4;
overflow-y: auto;
}

.article__list{
display: flex;
flex-direction: column;
list-style-type: none;
text-align: left;
font-size: 12px;
line-height: small
}

.article{
display:flex;
flex-direction:column;

}

.article__image{
position:relative;
float:left;
width: 40%;
height:20%;
background-position: 50% 50%;
background-repeat:no-repeat;
background-size:cover;

}

.article__image:hover{
outline: 1px solid white;
filter: grayscale(100%);
}

.article__image:link { color: #000000; text-decoration: none}
.article__image:visited { color: #000000; text-decoration: none}
.article__image:hover { color: #3366CC; text-decoration: underline}
.article__image:active { color: #000000; text-decoration: none}


.article__title{
display:flex;
flex-direction: column;
font-style: italic;
font-weight: bold;
font-size: 18px;
margin-top:5px;
margin-bottom: 5px;
width: 40%;
}

.article__title:link { color: black; text-decoration: none}
.article__title:visited { color: navy; text-decoration: none}
.article__title:hover { color: black; text-decoration: underline}
.article__title:active { color: navy; text-decoration: none}

.article_summary{
display: flex;
float: left;
font-style: normal;
font-size: 16px;
margin: 0;
text-align: left;
width: 40%;
}

.article_source{
font-style: italic;
color: black;
}

.footer {
display: flex;
flex-wrap: wrap;
justify-content: center;
font-weight: bold;
}

.footer__notes,
.footer__address,
.footer__map {
padding: 10px;
}

@media(max-width: 445px){
.article__image{
display: none;
}
}

@media (min-width: 768px) {
.masterhead {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}

.footer,
.content {
display: flex;

}

.content__body {
flex: 5;
}

.footer__notes,
.footer__address,
.footer__map {
flex: 0.5;
justify-content: center;
}
}

@media (min-width: 960px) {
.content__body {
flex: 5;
}

.footer__notes,
.footer__address,
.footer__map {
padding: 30px;
flex: 0.5;
}
}

@media (min-width: 1200px) {

.content__body {
flex: 5;
}

.footer__notes,
.footer__address,
.footer__map {
flex: 0.5;
}

.article_summary{
display:inline-block;
}

}
63 changes: 63 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const mainBody = document.querySelector(".content__body");
const search = document.querySelector("#search");
const keyword = document.querySelector("#search-tf");

function getTopStories(){
const url ='https://newsapi.org/v2/top-headlines?country=gb&apiKey=1f83068c0e0b46eb8146486c82998a9f';
return fetch(url)
.then(function(response){
return response.json()
}).then(function(data){
// console.log(data);
const topStories = data.articles.filter(imagesOnly => imagesOnly.urlToImage).map(function(article){
return `
<li class="article">
<a href="${article.url}"><img class="article__image" src="${article.urlToImage}"></a>
<a href="${article.url}" class="article__title">${article.title}</a>
<p class="article_summary">${article.description}</p>
<p class="article_source">
${article.source['name']}, ${article.publishedAt}
</p>
</li>
`
}).join('');
mainBody.innerHTML = `<ul class="article__list">${topStories}</ul>`;
})
.catch(function(error){
console.log('error');
})
};
getTopStories();

function getArticle(keyword){
const urlKeyword =`https://newsapi.org/v2/everything?q=${keyword}&apiKey=1f83068c0e0b46eb8146486c82998a9f`;
return fetch(urlKeyword)
.then(function(response){
return response.json()
}).then(function(data){
console.log(data);
const keywordResults = data.articles.filter(imagesOnly => imagesOnly.urlToImage).map(function(article){
return `
<li class="article">
<a href="${article.url}"><img class="article__image" src="${article.urlToImage}"></a>
<a href="${article.url}" class="article__title">${article.title}</a>
<p class="article_summary">${article.description}</p>
<p class="article_source">
${article.source['name']}, ${article.publishedAt}
Copy link
Contributor

Choose a reason for hiding this comment

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

article.source.name could work as well here and make it a tiny bit cleaner

</p>
</li>
`
}).join('');
mainBody.innerHTML = `<ul class="article__list">${keywordResults}</ul>`;
})
.catch(function(error){
console.log('getArticle',error);
})
};

//Event handlers
search.addEventListener('submit', function(event){
event.preventDefault();
const keyword = document.querySelector("#search-tf");
getArticle(keyword.value);
})