Completed
Pull Request — master (#227)
by Alejandro
06:25
created

src/common/MenuLayout.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 86
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 10.53%

Importance

Changes 0
Metric Value
wmc 2
eloc 74
mnd 2
bc 2
fnc 0
dl 0
loc 86
ccs 2
cts 19
cp 0.1053
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React, { useState, useEffect } 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 { withSelectedServer } from '../servers/helpers/withSelectedServer';
10
import NotFound from './NotFound';
11
import './MenuLayout.scss';
12
13
const propTypes = {
14
  match: PropTypes.object,
15
  location: PropTypes.object,
16
  selectedServer: serverType,
17
};
18
19
const MenuLayout = (TagsList, ShortUrls, AsideMenu, CreateShortUrl, ShortUrlVisits, ShlinkVersions, ServerError) => {
20
  const MenuLayoutComp = ({ match, location, selectedServer }) => {
21
    const [ showSideBar, setShowSidebar ] = useState(false);
22
    const { params: { serverId } } = match;
23
24
    useEffect(() => setShowSidebar(false), [ location ]);
25
26 2
    if (selectedServer.serverNotReachable) {
27
      return <ServerError type="not-reachable" />;
28
    }
29
30
    const burgerClasses = classNames('menu-layout__burger-icon', {
31
      'menu-layout__burger-icon--active': showSideBar,
32
    });
33
    const swipeMenuIfNoModalExists = (showSideBar) => () => {
34 2
      if (document.querySelector('.modal')) {
35
        return;
36
      }
37
38
      setShowSidebar(showSideBar);
39
    };
40
41
    return (
42
      <React.Fragment>
43
        <FontAwesomeIcon
44
          icon={burgerIcon}
45
          className={burgerClasses}
46
          onClick={() => setShowSidebar(!showSideBar)}
47
        />
48
49
        <Swipeable
50
          delta={40}
51
          className="menu-layout__swipeable"
52
          onSwipedLeft={swipeMenuIfNoModalExists(false)}
53
          onSwipedRight={swipeMenuIfNoModalExists(true)}
54
        >
55
          <div className="row menu-layout__swipeable-inner">
56
            <AsideMenu className="col-lg-2 col-md-3" selectedServer={selectedServer} showOnMobile={showSideBar} />
57
            <div className="col-lg-10 offset-lg-2 col-md-9 offset-md-3" onClick={() => setShowSidebar(false)}>
58
              <div className="menu-layout__container">
59
                <Switch>
60
                  <Route exact path="/server/:serverId/list-short-urls/:page" component={ShortUrls} />
61
                  <Route exact path="/server/:serverId/create-short-url" component={CreateShortUrl} />
62
                  <Route exact path="/server/:serverId/short-code/:shortCode/visits" component={ShortUrlVisits} />
63
                  <Route exact path="/server/:serverId/manage-tags" component={TagsList} />
64
                  <Route
65
                    render={() => <NotFound to={`/server/${serverId}/list-short-urls/1`}>List short URLs</NotFound>}
66
                  />
67
                </Switch>
68
              </div>
69
70
              <div className="menu-layout__footer text-center text-md-right">
71
                <ShlinkVersions />
72
              </div>
73
            </div>
74
          </div>
75
        </Swipeable>
76
      </React.Fragment>
77
    );
78
  };
79
80
  MenuLayoutComp.propTypes = propTypes;
81
82
  return withSelectedServer(MenuLayoutComp, ServerError);
83
};
84
85
export default MenuLayout;
86