Fakultas Ilmu Komputer UI

index.jsx 4.31 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 CompanyPage from './CompanyPage';
9
import CompanyProfile from './CompanyProfile';
Zamil Majdy's avatar
Zamil Majdy committed
10
import CreateVacancy from './CreateVacancy';
11
import Server from './lib/Server';
12
import Storage from './lib/Storage';
13
import ApplicantPage from './ApplicantPage';
14
import TranscriptPage from './TranscriptPage';
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
      if (App.getRole() === 'student') {
Zamil Majdy's avatar
Zamil Majdy committed
74
        replace({ pathname: '/lowongan' }); cb();
75
76
      } else if (App.getRole() === 'company') {
        replace({ pathname: '/lowongan' }); cb();
77
78
      } else if (App.getRole() === 'admin') {
        replace({ pathname: '/perusahaan' }); cb();
79
80
      } else if (App.getRole() === 'supervisor'){
        replace({ pathname:'/lowongan'}); cb();
81
      }
82

83
    }
84
    replace({ pathname: '/login' }); cb();
85
  };
86

87
  render() {
88
    const staff = this.authorization(['admin']);
89
    const student = this.authorization(['admin', 'student']);
90
    const supervisor = this.authorization(['admin', 'supervisor']);
91
92
    const company = this.authorization(['admin', 'company']);
    const commonUser = this.authorization(['admin', 'student', 'company']);
93
94
    const grownups = this.authorization(['admin', 'company', 'supervisor']);
    const facultyMember = this.authorization(['admin', 'student', 'supervisor']);
95
    const all = this.authorization(['admin', 'company', 'supervisor', 'student']);
96
97
98

    return (
      <Router history={browserHistory}>
99
        <Route path="/login" component={Login} />
100
        <Route component={all(Dashboard)} onEnter={this.handleAuth}>
101
          <Route path="/transcript/:id" component={company(TranscriptPage)} />
102
          <Route path="/lowongan" component={all(VacancyPage)} />
103
104
105
          <Route path="/buat-lowongan" component={company(CreateVacancy)} />
          <Route path="/buat-lowongan/:id" component={company(CreateVacancy)} />
          <Route path="/pelamar" component={company(ApplicantPage)} />
106
107
          <Route path="/profil" component={App.getRole() === 'student' ? student(ProfilePage) : company(CompanyProfile)} own />
          <Route path="/mahasiswa/:id" component={grownups(ProfilePage)} />
108
109
110
111
          <Route path="/perusahaan/:id" component={facultyMember(CompanyProfile)} 

          />
          <Route path="/perusahaan" component={supervisor(CompanyPage)} />
112
          <Route path="/transkrip/:id" component={facultyMember(CompanyProfile)} />
113
114
        </Route>
        <Route path="/home" onEnter={this.handleHome} />
115
        <Route path="/admin-vacancy" component={VacancyPage} />
116
117
118
119
        <Redirect from="*" to="/home" />
      </Router>
    );
  }
120
}
121
122

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