Passed
Push — master ( eba97e...33ec61 )
by Huu-Phat
20:19 queued 16:15
created

cms/src/core/components/Drawer.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 75
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 62
mnd 2
bc 2
fnc 0
dl 0
loc 75
ccs 20
cts 20
cp 1
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React from 'react'
2
import PropTypes from 'prop-types'
3
import { withStyles } from '@material-ui/core/styles'
4
import Drawer from '@material-ui/core/Drawer'
5
import List from '@material-ui/core/List'
6
import ListItem from '@material-ui/core/ListItem'
7
import ListItemIcon from '@material-ui/core/ListItemIcon'
8
import ListItemText from '@material-ui/core/ListItemText'
9
import { drawerPages } from 'core/const/pages'
10
import { isPathMatchUrl } from 'core/utils/misc'
11
12 2
const LeftDrawer = ({ classes, open, history }) => (
13 3
  <Drawer
14
    className={classes.drawer}
15
    variant="persistent"
16
    anchor="left"
17
    open={open}
18
    classes={{
19
      paper: classes.drawerPaper
20
    }}
21
  >
22
    <div className={classes.drawerHeader}>Blogs</div>
23
    <NoPaddingList>{renderDrawerItems(classes, history)}</NoPaddingList>
24
  </Drawer>
25
)
26
27 2
const renderDrawerItems = (classes, history) => {
28 3
  const matchedPath = getMatchingPath(history.location.pathname)
29 3
  return Object.keys(drawerPages).map(key => {
30 18
    const page = drawerPages[key]
31 18
    const isHighLight = matchedPath === page.url
32 18
    return (
33
      <ListItem
34
        button
35
        key={key}
36
        data-test-id={`drawer-item-${key}`}
37
        className={isHighLight ? classes.highLightListItem : classes.listItem}
38 1
        onClick={() => history.push(page.url)}
39
      >
40
        <ListItemIcon>{page.icon}</ListItemIcon>
41
        <ListItemText primary={page.name} />
42
      </ListItem>
43
    )
44
  })
45
}
46
47 2
export const getMatchingPath = pathName => {
48 3
  let matchingPath = null
49 3
  Object.keys(drawerPages).map(key => {
50 18
    const page = drawerPages[key]
51 18
    const isHighLight = isPathMatchUrl(pathName, page.url, page.exact)
52 18
    if (isHighLight && !matchingPath) {
53 3
      matchingPath = page.url
54
    }
55 18
    return null
56
  })
57 3
  return matchingPath
58
}
59
60
export default LeftDrawer
61
62 2
LeftDrawer.propTypes = {
63
  classes: PropTypes.object,
64
  open: PropTypes.bool,
65
  history: PropTypes.object,
66
  openDrawer: PropTypes.func,
67
  closeDrawer: PropTypes.func
68
}
69
70 2
const NoPaddingList = withStyles({
71
  padding: {
72
    padding: 0
73
  }
74
})(List)
75