Fakultas Ilmu Komputer UI

index.jsx 4.4 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
16
import CompanyPage from './CompanyPage';

17
18
export default class App extends React.Component {

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

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

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

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

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

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

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

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

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