Fakultas Ilmu Komputer UI

index.jsx 3.17 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
  static getRole() {
    if (Server.isLoggedIn()) {
16
17
      const data = Storage.get('user-data');
      if (data.is_staff) {
18
        return 'admin';
19
      } else if (data.supervisor) {
20
        return 'supervisor';
21
      } else if (data.student) {
22
        return 'student';
23
      } else if (data.company) {
24
25
26
27
28
29
30
        return 'company';
      }
      return 'error';
    }
    return 'not-logged-in';
  }

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

38
39
40
  authorization = allowedRoles =>
    WrappedComponent => (
      /* eslint-disable no-multi-comp */
41
42
43
44
45
      class WithAuthorization extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            user: {
46
              role: App.getRole(),
47
              data: Storage.get('user-data'),
48
49
50
51
52
53
            },
          };
        }

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

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

67
  handleHome= (nextState, replace, cb) => {
68
    console.log(Server.isLoggedIn());
69
    if (Server.isLoggedIn()) {
70
71
72
73
74
75
76
      console.log('masuk');
      if (App.getRole() === 'student') {
        console.log('masuk3');
        replace({ pathname: '/lowongan' }); cb();
      } else if (App.getRole() === 'company') {
        replace({ pathname: '/lowongan' }); cb();
      }
77
    }
78
    replace({ pathname: '/login' }); cb();
79
  };
80

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

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

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