Completed
Push — master ( 158ed8...34f194 )
by Alejandro
04:53 queued 02:19
created

src/common/MenuLayout.js   A

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 111
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 6.45%

Importance

Changes 0
Metric Value
wmc 5
eloc 90
mnd 2
bc 2
fnc 3
dl 0
loc 111
ccs 2
cts 31
cp 0.0645
rs 10
bpm 0.6666
cpm 1.6666
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
B MenuLayout.js ➔ render 0 68 2
A MenuLayout.js ➔ componentDidMount 0 6 1
A MenuLayout.js ➔ componentDidUpdate 0 7 2
1
import React from 'react';
2
import { Route, Switch } from 'react-router-dom';
3
import Swipeable from 'react-swipeable';
4
import { faBars as burgerIcon } from '@fortawesome/free-solid-svg-icons';
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
6
import classnames from 'classnames';
7
import * as PropTypes from 'prop-types';
8
import { serverType } from '../servers/prop-types';
9
import NotFound from './NotFound';
10
import './MenuLayout.scss';
11
12
const MenuLayout = (TagsList, ShortUrls, AsideMenu, CreateShortUrl, ShortUrlVisits) =>
13
  class MenuLayout extends React.Component {
14
    static propTypes = {
15
      match: PropTypes.object,
16
      selectServer: PropTypes.func,
17
      location: PropTypes.object,
18
      selectedServer: serverType,
19
    };
20
21
    state = { showSideBar: false };
22
23
    componentDidMount() {
24
      const { match, selectServer } = this.props;
25
      const { params: { serverId } } = match;
26
27
      selectServer(serverId);
28
    }
29
30
    componentDidUpdate(prevProps) {
31
      const { location } = this.props;
32
33
      // Hide sidebar when location changes
34 2
      if (location !== prevProps.location) {
35
        this.setState({ showSideBar: false });
36
      }
37
    }
38
39
    render() {
40
      const { selectedServer, match } = this.props;
41
      const { params: { serverId } } = match;
42
      const burgerClasses = classnames('menu-layout__burger-icon', {
43
        'menu-layout__burger-icon--active': this.state.showSideBar,
44
      });
45
      const swipeMenuIfNoModalExists = (showSideBar) => () => {
46 2
        if (document.querySelector('.modal')) {
47
          return;
48
        }
49
50
        this.setState({ showSideBar });
51
      };
52
53
      return (
54
        <React.Fragment>
55
          <FontAwesomeIcon
56
            icon={burgerIcon}
57
            className={burgerClasses}
58
            onClick={() => this.setState(({ showSideBar }) => ({ showSideBar: !showSideBar }))}
59
          />
60
61
          <Swipeable
62
            delta={40}
63
            className="menu-layout__swipeable"
64
            onSwipedLeft={swipeMenuIfNoModalExists(false)}
65
            onSwipedRight={swipeMenuIfNoModalExists(true)}
66
          >
67
            <div className="row menu-layout__swipeable-inner">
68
              <AsideMenu
69
                className="col-lg-2 col-md-3"
70
                selectedServer={selectedServer}
71
                showOnMobile={this.state.showSideBar}
72
              />
73
              <div
74
                className="col-lg-10 offset-lg-2 col-md-9 offset-md-3"
75
                onClick={() => this.setState({ showSideBar: false })}
76
              >
77
                <Switch>
78
                  <Route
79
                    exact
80
                    path="/server/:serverId/list-short-urls/:page"
81
                    component={ShortUrls}
82
                  />
83
                  <Route
84
                    exact
85
                    path="/server/:serverId/create-short-url"
86
                    component={CreateShortUrl}
87
                  />
88
                  <Route
89
                    exact
90
                    path="/server/:serverId/short-code/:shortCode/visits"
91
                    component={ShortUrlVisits}
92
                  />
93
                  <Route
94
                    exact
95
                    path="/server/:serverId/manage-tags"
96
                    component={TagsList}
97
                  />
98
                  <Route
99
                    render={() => <NotFound to={`/server/${serverId}/list-short-urls/1`} btnText="List short URLs" />}
100
                  />
101
                </Switch>
102
              </div>
103
            </div>
104
          </Swipeable>
105
        </React.Fragment>
106
      );
107
    }
108
  };
109
110
export default MenuLayout;
111