Passed
Push — master ( e61af0...aea7d3 )
by Seonkuk
09:21 queued 11s
created

App.onMenuSelected   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
/* global window */
2
import React from 'react';
3
import PropTypes from 'prop-types';
4
import {
5
  Navbar,
6
  Nav,
7
  NavItem,
8
  Grid,
9
  Row,
10
} from 'react-bootstrap';
11
12
export default class App extends React.Component {
13
  constructor(props) {
14
    super(props);
15
    this.state = { navExpanded: false };
16
  }
17
18
  onMenuSelected(viewName) {
19
    this.setState({ navExpanded: false });
20
    const menuSelected = this.props.menus.find(m => m.viewName === viewName);
21
    window.location.href = menuSelected ? menuSelected.url : '/';
22
  }
23
24
  render() {
25
    const ChildComponent = require(`./${this.props.viewName}`).default;
26
    const children = React.createElement(ChildComponent, this.props);
27
    let button;
28
    if (this.props.auth && this.props.auth.isAuthenticated) {
29
      button = <NavItem href="/logout">{this.props.auth.username} 로그아웃</NavItem>;
30
    } else {
31
      button = <NavItem href="/login">로그인</NavItem>;
32
    }
33
34
    return (
35
      <div>
36
        <Navbar expanded={this.state.navExpanded} onToggle={navExpanded => this.setState({ navExpanded })}>
37
          <Navbar.Header>
38
            <Navbar.Brand><a href="/">긴급 공지사항 등록 시스템</a></Navbar.Brand>
39
            <Navbar.Toggle />
40
          </Navbar.Header>
41
          <Navbar.Collapse>
42
            <Nav role="navigation" activeKey={this.props.viewName} onSelect={viewName => this.onMenuSelected(viewName)}>
43
              {this.props.menus.map((menu, idx) => <NavItem key={idx} eventKey={menu.viewName} href={menu.url}>{menu.title}</NavItem>)}
44
            </Nav>
45
            <Nav pullRight>{button}</Nav>
46
          </Navbar.Collapse>
47
        </Navbar>
48
        <Grid>
49
          <Row>
50
            {children}
51
          </Row>
52
        </Grid>
53
      </div>
54
    );
55
  }
56
}
57
58
App.defaultProps = {
59
  auth: {
60
    isAuthenticated: false,
61
  },
62
  menus: [],
63
};
64
65
App.propTypes = {
66
  viewName: PropTypes.string.isRequired,
67
  auth: PropTypes.shape({
68
    isAuthenticated: PropTypes.bool,
69
    username: PropTypes.string,
70
  }),
71
  menus: PropTypes.arrayOf(PropTypes.shape({
72
    viewName: PropTypes.string.isRequired,
73
    url: PropTypes.string.isRequired,
74
    title: PropTypes.string.isRequired,
75
  })),
76
};
77