Passed
Push — master ( 6fca9e...82ba0d )
by Huu-Phat
02:25 queued 10s
created

cms/src/post/containers/Post.js   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 46
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 40
mnd 1
bc 1
fnc 0
dl 0
loc 46
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
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