Fakultas Ilmu Komputer UI

index.jsx 3.84 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';
7
import ProfilePage from './ProfilePage';
8
import CompanyProfile from './CompanyProfile';
Zamil Majdy's avatar
Zamil Majdy committed
9
import CreateVacancy from './CreateVacancy';
10
import Server from './lib/Server';
11
import Storage from './lib/Storage';
12
import ApplicantPage from './ApplicantPage';
13

14
15
export default class App extends React.Component {

16
17
  static getRole() {
    if (Server.isLoggedIn()) {
18
19
      const data = Storage.get('user-data');
      if (data.is_staff) {
20
        return 'admin';
21
      } else if (data.supervisor) {
22
        return 'supervisor';
23
      } else if (data.student) {
24
        return 'student';
25
      } else if (data.company) {
26
27
28
29
30
31
32
        return 'company';
      }
      return 'error';
    }
    return 'not-logged-in';
  }

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

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

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

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

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

83
  render() {
84
    const student = this.authorization(['admin', 'student']);
85
    // const supervisor = this.authorization(['admin', 'supervisor']);
86
87
    const company = this.authorization(['admin', 'company']);
    const commonUser = this.authorization(['admin', 'student', 'company']);
88
89
    const grownups = this.authorization(['admin', 'company', 'supervisor']);
    const facultyMember = this.authorization(['admin', 'student', 'supervisor']);
90
91
92

    return (
      <Router history={browserHistory}>
93
        <Route path="/login" component={Login} />
sirinbaisa's avatar
sirinbaisa committed
94
        <Route component={App.getRole() === 'student' ? student(Dashboard) : company(Dashboard)} onEnter={this.handleAuth} own>
95
          <Route path="/lowongan" component={commonUser(VacancyPage)} />
96
97
98
          <Route path="/buat-lowongan" component={company(CreateVacancy)} />
          <Route path="/buat-lowongan/:id" component={company(CreateVacancy)} />
          <Route path="/pelamar" component={company(ApplicantPage)} />
99
100
101
102
          <Route path="/profil" component={App.getRole() === 'student' ? student(ProfilePage) : company(CompanyProfile)} own />
          <Route path="/mahasiswa/:id" component={grownups(ProfilePage)} />
          <Route path="/perusahaan/:id" component={facultyMember(CompanyProfile)} />
          <Route path="/transkrip/:id" component={facultyMember(CompanyProfile)} />
103
104
105
106
107
108
        </Route>
        <Route path="/home" onEnter={this.handleHome} />
        <Redirect from="*" to="/home" />
      </Router>
    );
  }
109
}
110
111

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