Passed
Push — master ( f602d8...7b3414 )
by Huu-Phat
02:55 queued 11s
created

modules/home/components/nav/NavContent.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 107
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 75
mnd 2
bc 2
fnc 0
dl 0
loc 107
ccs 20
cts 20
cp 1
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React, { useState, useEffect } from 'react'
2
import styled from 'styled-components/macro'
3
import PropTypes from 'prop-types'
4
import NavLogo from '~/modules/home/components/nav/NavLogo'
5
import NavItem from '~/modules/home/components/nav/NavItem'
6
import MobileContainer from '~/modules/home/components/common/container/MobileContainer'
7
import Row from 'react-bootstrap/Row'
8
import Col from 'react-bootstrap/Col'
9
import { PAGE_LIST } from '~/modules/home/consts/pages'
10
import {
11
  getSelectedSection,
12
  setJqueryScrollEvent,
13
  cleanUpScrollEvent,
14
  isSafari,
15
  initHashLocation
16
} from '~/modules/core/utils/helpers'
17
18 2
const NavContent = ({ isShowNavContent }) => {
19 5
  const [selectedPage, setSelectedPage] = useState(getSelectedSection())
20
21 5
  useEffect(() => {
22 4
    const handleHashChange = () => {
23 1
      setSelectedPage(getSelectedSection())
24
    }
25 4
    window.addEventListener('hashchange', handleHashChange)
26 4
    initHashLocation()
27
    /*
28
     * Currently safari doesnt support smooth scrolling with scroll-behavior
29
     * This is a workaround by jquery for the case in Safari
30
     */
31 4
    isSafari() && setJqueryScrollEvent()
32
33 4
    return function cleanup() {
34 1
      window.removeEventListener('hashchange', handleHashChange, false)
35 2
      isSafari() && cleanUpScrollEvent()
36
    }
37
  })
38
39 5
  return (
40
    <NavContainer isShowNavContent={isShowNavContent}>
41
      <MobileContainer>
42
        <Row>
43
          <Col lg={2}>
44
            <NavLogo />
45
          </Col>
46
          <Col fluid="true">
47
            <NavMenu>
48
              <ul>
49
                {PAGE_LIST.map(page => (
50 35
                  <NavLi key={page.name}>
51
                    <NavItem
52
                      page={page}
53
                      isSelected={selectedPage === page.cmpName}
54
                      setSelectedPage={setSelectedPage}
55
                    />
56
                  </NavLi>
57
                ))}
58
              </ul>
59
            </NavMenu>
60
          </Col>
61
        </Row>
62
      </MobileContainer>
63
    </NavContainer>
64
  )
65
}
66
67 2
NavContent.propTypes = {
68
  isShowNavContent: PropTypes.bool
69
}
70
71
const NavContainer = styled.div`
72
  position: relative;
73 4
  min-height: ${props => props.theme.navContentMinHeight};
74 4
  background-color: ${props => props.theme.colors.colorLight};
75
  margin-top: ${props =>
76 4
    props.isShowNavContent
77
      ? props.theme.navHeaderHeight
78
      : `-${props.theme.navHeaderHeight}`};
79 4
  box-shadow: ${props => props.theme.extra.boxShadow};
80
  transition: all 0.5s;
81
82
  @media screen and (max-width: 800px) {
83
    margin-top: ${props =>
84 4
      props.isShowNavContent
85
        ? props.theme.navHeaderHeight
86
        : props.theme.navPosWhenHiding};
87
  }
88
`
89
const NavMenu = styled.nav`
90
  text-align: right;
91
92
  @media screen and (max-width: 800px) {
93
    text-align: center;
94
  }
95
`
96
const NavLi = styled.li`
97
  display: inline-block;
98
  margin-left: 18px;
99
100
  @media screen and (max-width: 800px) {
101
    margin: 0 9px;
102
    display: block;
103 28
    border-bottom: 1px dashed ${props => props.theme.colors.colorDarken};
104
  }
105
`
106
export default NavContent
107