Created
August 30, 2018 22:24
-
-
Save ChangoMan/dc557843e2bf751780721ab6975306b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { h, Component } from "preact"; | |
import style from "./style"; | |
const apiKey = "YOUR_NEWS_API_KEY"; | |
export default class Home extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
error: null, | |
isLoaded: false, | |
selectedSource: "techcrunch", | |
articles: [], | |
sources: [] | |
}; | |
} | |
componentDidMount() { | |
this.updateNews(this.state.selectedSource); | |
this.updateSources(); | |
} | |
updateNews = async (source = this.state.selectedSource) => { | |
try { | |
const res = await fetch(`https://newsapi.org/v2/top-headlines?sources=${source}&apiKey=${apiKey}`); | |
const json = await res.json(); | |
this.setState(() => ({ | |
isLoaded: true, | |
articles: json.articles, | |
selectedSource: source | |
})); | |
} catch (error) { | |
this.setState(() => ({ error })); | |
} | |
}; | |
updateSources = async () => { | |
try { | |
const res = await fetch(`https://newsapi.org/v2/sources?apiKey=${apiKey}`); | |
const json = await res.json(); | |
this.setState(() => ({ | |
sources: json.sources | |
})); | |
} catch (error) { | |
this.setState(() => ({ error })); | |
} | |
}; | |
render() { | |
const { error, isLoaded, articles } = this.state; | |
if (error) { | |
return ( | |
<div className="error"> | |
<div className="alert alert--error">Error: {error.message}</div> | |
</div> | |
); | |
} else if (!isLoaded) { | |
return <div>Loading...</div>; | |
} else { | |
return ( | |
<div className={style.home}> | |
<div className="site-header"> | |
<div className="select"> | |
<select | |
value={this.state.selectedSource} | |
onChange={e => { | |
this.updateNews(e.target.value); | |
}}> | |
{this.state.sources.map(source => { | |
return ( | |
<option value={source.id} key={source.id}> | |
{source.name} | |
</option> | |
); | |
})} | |
</select> | |
</div> | |
</div> | |
<main> | |
{articles.map((article, index) => ( | |
<div className="article" key={index}> | |
<h2> | |
<a href={article.url}>{article.title}</a> | |
</h2> | |
<img src={article.urlToImage} alt="" /> | |
<p>{article.description}</p> | |
</div> | |
))} | |
</main> | |
</div> | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment