Fakultas Ilmu Komputer UI

index.jsx 3.93 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
import TranscriptPage from './TranscriptPage';
14
import AdminVacancyPage from './AdminVacancyPage';
15

16
17
export default class App extends React.Component {

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

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

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

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

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

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

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

    return (
      <Router history={browserHistory}>
92
        <Route path="/login" component={Login} />
93
        <Route component={Dashboard} onEnter={this.handleAuth}>
94
          <Route path="/transcript/:id" component={company(TranscriptPage)} />
95
          <Route path="/lowongan" component={commonUser(VacancyPage)} />
96
          <Route path="/admin-lowongan" component={AdminVacancyPage} />
97
98
99
          <Route path="/buat-lowongan" component={company(CreateVacancy)} />
          <Route path="/buat-lowongan/:id" component={company(CreateVacancy)} />
          <Route path="/pelamar" component={company(ApplicantPage)} />
100
101
102
103
          <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)} />
104
105
106
107
108
109
        </Route>
        <Route path="/home" onEnter={this.handleHome} />
        <Redirect from="*" to="/home" />
      </Router>
    );
  }
110
}
111
112

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