Completed
Push — master ( fad0bf...85dc1d )
by Alejandro
19s queued 12s
created

src/common/MenuLayout.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 87
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 15%

Importance

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