Created
May 25, 2018 18:46
-
-
Save AntonioErdeljac/b54ed5c70e6828c165051e8bca5c4cbe to your computer and use it in GitHub Desktop.
This file contains 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 from 'react'; | |
import axios from 'axios'; | |
import moment from 'moment'; | |
import { connect } from 'react-redux'; | |
import { Form } from '../../components/Article'; | |
class Home extends React.Component { | |
constructor(props) { | |
super(props); | |
this.handleDelete = this.handleDelete.bind(this); | |
} | |
componentDidMount() { | |
const { onLoad } = this.props; | |
axios('http://localhost:8000/api/articles') | |
.then((res) => onLoad(res.data)); | |
} | |
handleDelete(id) { | |
const { onDelete } = this.props; | |
return axios.delete(`http://localhost:8000/api/articles/${id}`) | |
.then(() => onDelete(id)); | |
} | |
render() { | |
const { articles } = this.props; | |
return ( | |
<div className="container"> | |
<div className="row pt-5"> | |
<div className="col-12 col-lg-6 offset-lg-3"> | |
<h1 className="text-center">LightBlog</h1> | |
</div> | |
<Form /> | |
</div> | |
<div className="row pt-5"> | |
<div className="col-12 col-lg-6 offset-lg-3"> | |
{articles.map((article) => { | |
return ( | |
<div className="card my-3"> | |
<div className="card-header"> | |
{article.title} | |
</div> | |
<div className="card-body"> | |
{article.body} | |
<p className="mt-5 text-muted"><b>{article.author}</b> {moment(new Date(article.createdAt)).fromNow()}</p> | |
</div> | |
<div className="card-footer"> | |
<div className="row"> | |
<button className="btn btn-primary mx-3"> | |
Edit | |
</button> | |
<button onClick={() => this.handleDelete(article._id)} className="btn btn-danger"> | |
Delete | |
</button> | |
</div> | |
</div> | |
</div> | |
) | |
})} | |
</div> | |
</div> | |
</div> | |
); | |
} | |
} | |
const mapStateToProps = state => ({ | |
articles: state.home.articles, | |
}); | |
const mapDispatchToProps = dispatch => ({ | |
onLoad: data => dispatch({ type: 'HOME_PAGE_LOADED', data }), | |
onDelete: id => dispatch({ type: 'DELETE_ARTICLE', id }), | |
}); | |
export default connect(mapStateToProps, mapDispatchToProps)(Home); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment