| Total Complexity | 1 |
| Complexity/F | 0 |
| Lines of Code | 46 |
| Function Count | 0 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import React, { useEffect } from 'react' |
||
| 2 | import PropTypes from 'prop-types' |
||
| 3 | import { connect } from 'react-redux' |
||
| 4 | import { useParams } from 'react-router-dom' |
||
| 5 | import { |
||
| 6 | fetchPost, |
||
| 7 | saveEditedContent, |
||
| 8 | createPost, |
||
| 9 | initPost |
||
| 10 | } from 'post/state/actions' |
||
| 11 | import { getIsPostLoading } from 'post/state/selectors' |
||
| 12 | |||
| 13 | import Post from 'post/components/Post' |
||
| 14 | |||
| 15 | const mapStateToProps = state => ({ |
||
| 16 | loading: getIsPostLoading(state) |
||
| 17 | }) |
||
| 18 | |||
| 19 | const mapDispatchToProps = { |
||
| 20 | fetchPost, |
||
| 21 | saveEditedContent, |
||
| 22 | createPost, |
||
| 23 | initPost |
||
| 24 | } |
||
| 25 | |||
| 26 | const PostWithHook = props => { |
||
| 27 | const { fetchPost, initPost } = props |
||
| 28 | const { slug } = useParams() |
||
| 29 | const newPost = slug === 'new' |
||
| 30 | useEffect(() => { |
||
| 31 | if (!newPost) { |
||
| 32 | fetchPost(slug) |
||
| 33 | } else { |
||
| 34 | initPost() |
||
| 35 | } |
||
| 36 | }, [slug, fetchPost, initPost, newPost]) |
||
| 37 | return <Post {...props} newPost={newPost} /> |
||
| 38 | } |
||
| 39 | PostWithHook.propTypes = { |
||
| 40 | newPost: PropTypes.bool, |
||
| 41 | fetchPost: PropTypes.func, |
||
| 42 | initPost: PropTypes.func |
||
| 43 | } |
||
| 44 | |||
| 45 | export default connect(mapStateToProps, mapDispatchToProps)(PostWithHook) |
||
| 46 |