Created
January 4, 2018 14:57
-
-
Save fpersico/13b29455aaa9bf7a189fd1a88df4c88f to your computer and use it in GitHub Desktop.
ArticleScreen
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 React, { Component } from 'react'; | |
import { Container } from 'reactstrap'; | |
import { fetchArticle } from '../api'; | |
import ArticleFull from '../components/ArticleFull'; | |
class ArticleScreen extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
article: null | |
}; | |
} | |
componentDidMount() { | |
const { uuid } = this.props.match.params; | |
this.loadArticle(uuid); | |
} | |
render() { | |
const { error, errorMessage, article } = this.state; | |
return ( | |
<Container> | |
{error && <h2>{errorMessage}</h2>} | |
{article && <ArticleFull article={article} />} | |
</Container> | |
); | |
} | |
loadArticle = uuid => { | |
fetchArticle(uuid) | |
.then(response => { | |
const body = response.body; | |
const article = body.data; | |
this.setState({ | |
error: false, | |
article: article | |
}); | |
}) | |
.catch(error => { | |
this.setState({ | |
error: true, | |
errorMessage: `Error fetching article: '${error.message}'` | |
}); | |
}); | |
}; | |
} | |
export default ArticleScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment