Fakultas Ilmu Komputer UI

index.jsx 4.51 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';
sirinbaisa's avatar
sirinbaisa committed
14
import AdminVacancyPage from './AdminVacancyPage';
sirinbaisa's avatar
sirinbaisa committed
15
import CompanyPage from './CompanyPage';
16
import SupervisorPage from './SupervisorPage'
sirinbaisa's avatar
sirinbaisa committed
17

18
19
export default class App extends React.Component {

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

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

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

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

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

73
  handleHome= (nextState, replace, cb) => {
74
    if (Server.isLoggedIn()) {
75
      if (App.getRole() === 'student') {
sirinbaisa's avatar
sirinbaisa committed
76
77
78
79
80
        replace({ pathname: '/lowongan' });
        cb();
      } else if (App.getRole() === 'company') {
        replace({ pathname: '/lowongan' });
        cb();
81
      } else if (App.getRole() === 'company') {
sirinbaisa's avatar
sirinbaisa committed
82
83
84
85
86
        replace({ pathname: '/lowongan' });
        cb();
      } else if (App.getRole() === 'admin') {
        replace({ pathname: '/perusahaan' });
        cb();
87
      }
88
    }
89
    replace({ pathname: '/login' }); cb();
90
  };
91

92
  render() {
sirinbaisa's avatar
sirinbaisa committed
93
    const staff = this.authorization(['admin']);
94
    const student = this.authorization(['admin', 'student']);
95
    const supervisor = this.authorization(['admin', 'supervisor']);
96
97
    const company = this.authorization(['admin', 'company']);
    const commonUser = this.authorization(['admin', 'student', 'company']);
98
99
    const grownups = this.authorization(['admin', 'company', 'supervisor']);
    const facultyMember = this.authorization(['admin', 'student', 'supervisor']);
sirinbaisa's avatar
sirinbaisa committed
100
    const all = this.authorization(['admin', 'company', 'student', 'supervisor']);
101
102
103

    return (
      <Router history={browserHistory}>
104
        <Route path="/login" component={Login} />
sirinbaisa's avatar
sirinbaisa committed
105
        <Route component={commonUser(Dashboard)} onEnter={this.handleAuth}>
106
          <Route path="/transcript/:id" component={company(TranscriptPage)} />
107
          <Route path="/lowongan" component={commonUser(VacancyPage)} />
sirinbaisa's avatar
sirinbaisa committed
108
          <Route path="/admin-lowongan" component={AdminVacancyPage} />
109
110
111
          <Route path="/buat-lowongan" component={company(CreateVacancy)} />
          <Route path="/buat-lowongan/:id" component={company(CreateVacancy)} />
          <Route path="/pelamar" component={company(ApplicantPage)} />
112
113
114
          <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)} />
sirinbaisa's avatar
sirinbaisa committed
115
          <Route path="/perusahaan" component={staff(CompanyPage)} />
116
          <Route path="/rekap" component={supervisor(SupervisorPage)} />
117
          <Route path="/transkrip/:id" component={facultyMember(CompanyProfile)} />
118
119
120
121
122
123
        </Route>
        <Route path="/home" onEnter={this.handleHome} />
        <Redirect from="*" to="/home" />
      </Router>
    );
  }
124
}
125
126

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