File size: 1,136 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react';
import { connect } from 'react-redux';
import PostForm from './post_form';
import { fetchPost, updatePost } from '../../actions/post_actions';

const mapStateToProps = (state, ownProps) => {
  const defaultPost = { body: '' };
  const post = state.entities.posts[ownProps.match.params.postId]
             || defaultPost;
  const formType = 'Update Post';

  return { post, formType };
};

const mapDispatchToProps = dispatch => {
  return {
    fetchPost: id => dispatch(fetchPost(id)),
    action: post => dispatch(updatePost(post)),
  };
};

class EditPostForm extends React.Component {
  componentDidMount() {
    this.props.fetchPost(this.props.match.params.postId);
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.post.id !== nextProps.match.params.postId) {
      this.props.fetchPost(nextProps.match.params.postId);
    }
  }

  render() {
    const { action, formType, post } = this.props;
    return (
      <PostForm
        action={action}
        formType={formType}
        post={post} />
    );
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(EditPostForm);