Fakultas Ilmu Komputer UI

index.jsx 2.79 KB
Newer Older
1
/* eslint-disable no-multi-comp */
2
3
import React from 'react';
import ReactDOM from 'react-dom';
4
5
import { Router, Route, browserHistory, Redirect } from 'react-router';
import { Segment } from 'semantic-ui-react';
6
7
import Dashboard from './Dashboard';
import Login from './Login';
8
import VacancyPage from './VacancyPage';
9
import Server from './lib/Server';
10
import Storage from './lib/Storage';
11
12


13
14
15
16
17
18
export default class App extends React.Component {

  constructor(props) {
    super(props);
    /* istanbul ignore next */
    this.handleAuth = this.handleAuth.bind(this);
Zamil Majdy's avatar
Zamil Majdy committed
19
    this.handleHome = this.handleHome.bind(this);
20
21
  }

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  authorization = WrappedComponent =>
    allowedRoles => (
      class WithAuthorization extends React.Component {
        constructor(props) {
          super(props);

          this.state = {
            user: {
              role: WithAuthorization.getRole(),
            },
          };
        }

        static getRole() {
          if (Server.isLoggedIn()) {
            if (Storage.get('user-data').is_staff) {
              return 'admin';
            } else if (Storage.get('user-data').supervisor) {
              return 'supervisor';
            } else if (Storage.get('user-data').student) {
              return 'student';
            } else if (Storage.get('user-data').company) {
              return 'company';
            }
            return 'error';
          }
          return 'not-logged-in';
        }

        render() {
          const { role } = this.state.user;
          if (allowedRoles.includes(role)) {
            return <WrappedComponent {...this.props} />;
          }
          return browserHistory.goBack;
        }
      });

Zamil Majdy's avatar
Zamil Majdy committed
60
61
62
63
  handleAuth = (nextState, replace) => {
    if (!Server.isLoggedIn()) replace({ pathname: '/login' });
    Storage.getUserData();
  };
64

65
  handleHome= (nextState, replace, cb) => {
66
    if (Server.isLoggedIn()) {
67
68
69
70
      Storage.getUserData().then((data) => {
        const next = data.student ? '/lowongan' : '/profile';
        replace({ pathname: next }); cb();
      });
71
72
73
74
    } else {
      replace({ pathname: '/login' });
    }
  };
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  render() {
    const student = this.authorization(['admin', 'student']);
    const supervisor = this.authorization(['admin', 'supervisor']);
    const company = this.authorization(['admin', 'company']);
    const commonUser = this.authorization(['admin', 'student', 'company']);

    return (
      <Router history={browserHistory}>
        <Route path="/login" component={Login}/>
        <Route component={Dashboard} onEnter={this.handleAuth}>
          <Route path="/lowongan" component={commonUser(VacancyPage)} />
        </Route>
        <Route path="/home" onEnter={this.handleHome} />
        <Redirect from="*" to="/home" />
      </Router>
    );
  }
93
}
94
95

ReactDOM.render(<App />, document.getElementById('react-app'));