Completed
Push — master ( 6ac893...01672b )
by Alejandro
22s queued 10s
created

src/common/MenuLayout.js   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 98
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 16%

Importance

Changes 0
Metric Value
wmc 4
eloc 84
mnd 4
bc 4
fnc 0
dl 0
loc 98
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
ccs 4
cts 25
cp 0.16
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 Message from '../utils/Message';
10
import NotFound from './NotFound';
11
import './MenuLayout.scss';
12
13
const propTypes = {
14
  match: PropTypes.object,
15
  selectServer: PropTypes.func,
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, selectServer }) => {
22
    const [ showSideBar, setShowSidebar ] = useState(false);
23
    const { params: { serverId } } = match;
24
25
    useEffect(() => {
26
      selectServer(serverId);
27
    }, [ serverId ]);
28
    useEffect(() => setShowSidebar(false), [ location ]);
29
30 2
    if (!selectedServer) {
31
      return <Message loading />;
32
    }
33
34 2
    if (selectedServer.serverNotFound) {
35
      return <ServerError type="not-found" />;
36
    }
37
38 2
    if (selectedServer.serverNotReachable) {
39
      return <ServerError type="not-reachable" />;
40
    }
41
42
    const burgerClasses = classNames('menu-layout__burger-icon', {
43
      'menu-layout__burger-icon--active': showSideBar,
44
    });
45
    const swipeMenuIfNoModalExists = (showSideBar) => () => {
46 2
      if (document.querySelector('.modal')) {
47
        return;
48
      }
49
50
      setShowSidebar(showSideBar);
51
    };
52
53
    return (
54
      <React.Fragment>
55
        <FontAwesomeIcon
56
          icon={burgerIcon}
57
          className={burgerClasses}
58
          onClick={() => setShowSidebar(!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 className="col-lg-2 col-md-3" selectedServer={selectedServer} showOnMobile={showSideBar} />
69
            <div className="col-lg-10 offset-lg-2 col-md-9 offset-md-3" onClick={() => setShowSidebar(false)}>
70
              <div className="menu-layout__container">
71
                <Switch>
72
                  <Route exact path="/server/:serverId/list-short-urls/:page" component={ShortUrls} />
73
                  <Route exact path="/server/:serverId/create-short-url" component={CreateShortUrl} />
74
                  <Route exact path="/server/:serverId/short-code/:shortCode/visits" component={ShortUrlVisits} />
75
                  <Route exact path="/server/:serverId/manage-tags" component={TagsList} />
76
                  <Route
77
                    render={() => <NotFound to={`/server/${serverId}/list-short-urls/1`}>List short URLs</NotFound>}
78
                  />
79
                </Switch>
80
              </div>
81
82
              <div className="menu-layout__footer text-center text-md-right">
83
                <ShlinkVersions />
84
              </div>
85
            </div>
86
          </div>
87
        </Swipeable>
88
      </React.Fragment>
89
    );
90
  };
91
92
  MenuLayoutComp.propTypes = propTypes;
93
94
  return MenuLayoutComp;
95
};
96
97
export default MenuLayout;
98