Completed
Push — master ( c8ba67...05deb1 )
by Alejandro
21s queued 12s
created

src/common/MenuLayout.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 83
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
wmc 2
eloc 71
mnd 2
bc 2
fnc 0
dl 0
loc 83
ccs 2
cts 18
cp 0.1111
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) => () => {
35 2
      if (document.querySelector('.modal')) {
36
        return;
37
      }
38
39
      callback();
40
    };
41
42
    return (
43
      <React.Fragment>
44
        <FontAwesomeIcon icon={burgerIcon} className={burgerClasses} onClick={toggleSidebar} />
45
46
        <Swipeable
47
          delta={40}
48
          className="menu-layout__swipeable"
49
          onSwipedLeft={swipeMenuIfNoModalExists(hideSidebar)}
50
          onSwipedRight={swipeMenuIfNoModalExists(showSidebar)}
51
        >
52
          <div className="row menu-layout__swipeable-inner">
53
            <AsideMenu className="col-lg-2 col-md-3" selectedServer={selectedServer} showOnMobile={sidebarVisible} />
54
            <div className="col-lg-10 offset-lg-2 col-md-9 offset-md-3" onClick={() => hideSidebar()}>
55
              <div className="menu-layout__container">
56
                <Switch>
57
                  <Route exact path="/server/:serverId/list-short-urls/:page" component={ShortUrls} />
58
                  <Route exact path="/server/:serverId/create-short-url" component={CreateShortUrl} />
59
                  <Route exact path="/server/:serverId/short-code/:shortCode/visits" component={ShortUrlVisits} />
60
                  <Route exact path="/server/:serverId/manage-tags" component={TagsList} />
61
                  <Route
62
                    render={() => <NotFound to={`/server/${serverId}/list-short-urls/1`}>List short URLs</NotFound>}
63
                  />
64
                </Switch>
65
              </div>
66
67
              <div className="menu-layout__footer text-center text-md-right">
68
                <ShlinkVersions />
69
              </div>
70
            </div>
71
          </div>
72
        </Swipeable>
73
      </React.Fragment>
74
    );
75
  };
76
77
  MenuLayoutComp.propTypes = propTypes;
78
79
  return withSelectedServer(MenuLayoutComp, ServerError);
80
};
81
82
export default MenuLayout;
83