Fakultas Ilmu Komputer UI

index.jsx 3.08 KB
Newer Older
1
2
import React from 'react';
import ReactDOM from 'react-dom';
3
import { Router, Route, browserHistory, Redirect } from 'react-router';
4
5
import Dashboard from './Dashboard';
import Login from './Login';
6
import VacancyPage from './VacancyPage';
Zamil Majdy's avatar
Zamil Majdy committed
7
import CreateVacancy from './CreateVacancy';
8
import Server from './lib/Server';
9
import Storage from './lib/Storage';
10
import ApplicantPage from './ApplicantPage';
11

12
13
export default class App extends React.Component {

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  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';
  }

30
31
32
33
  constructor(props) {
    super(props);
    /* istanbul ignore next */
    this.handleAuth = this.handleAuth.bind(this);
Zamil Majdy's avatar
Zamil Majdy committed
34
    this.handleHome = this.handleHome.bind(this);
35
36
  }

37
38
39
  authorization = allowedRoles =>
    WrappedComponent => (
      /* eslint-disable no-multi-comp */
40
41
42
43
44
45
      class WithAuthorization extends React.Component {
        constructor(props) {
          super(props);

          this.state = {
            user: {
46
              role: App.getRole(),
47
48
49
50
51
52
53
54
55
            },
          };
        }

        render() {
          const { role } = this.state.user;
          if (allowedRoles.includes(role)) {
            return <WrappedComponent {...this.props} />;
          }
56
          return <div> { browserHistory.push('/home') } </div>;
57
58
59
        }
      });

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()) {
Zamil Majdy's avatar
Zamil Majdy committed
67
      Storage.getUserData().then(() => {
68
69
70
71
72
        if (App.getRole() === 'student') {
          replace({ pathname: '/lowongan' }); cb();
        } else if (App.getRole() === 'company') {
          replace({ pathname: '/pelamar' }); cb();
        }
73
      });
74
    }
75
    replace({ pathname: '/login' }); cb();
76
  };
77

78
  render() {
79
80
    // const student = this.authorization(['admin', 'student']);
    // const supervisor = this.authorization(['admin', 'supervisor']);
81
82
83
84
85
    const company = this.authorization(['admin', 'company']);
    const commonUser = this.authorization(['admin', 'student', 'company']);

    return (
      <Router history={browserHistory}>
86
        <Route path="/login" component={Login} />
87
88
        <Route component={Dashboard} onEnter={this.handleAuth}>
          <Route path="/lowongan" component={commonUser(VacancyPage)} />
89
90
91
          <Route path="/buat-lowongan" component={company(CreateVacancy)} />
          <Route path="/buat-lowongan/:id" component={company(CreateVacancy)} />
          <Route path="/pelamar" component={company(ApplicantPage)} />
92
93
94
95
96
97
        </Route>
        <Route path="/home" onEnter={this.handleHome} />
        <Redirect from="*" to="/home" />
      </Router>
    );
  }
98
}
99
100

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