Fakultas Ilmu Komputer UI

index.jsx 1.67 KB
Newer Older
1
2
import React from 'react';
import ReactDOM from 'react-dom';
3
4
import { Router, Route, browserHistory, Redirect } from 'react-router';
import { Segment } from 'semantic-ui-react';
5
6
import Dashboard from './Dashboard';
import Login from './Login';
7
import VacancyPage from './VacancyPage';
8
import Server from './lib/Server';
9
import Storage from './lib/Storage';
10
11
12
13
14
15
16
17

export const Profile = () => (
  <Segment>
    <img src="http://semantic-ui.com/images/wireframe/media-paragraph.png" alt="wire-frame" />
  </Segment>
);


18
19
20
21
22
23
export default class App extends React.Component {

  constructor(props) {
    super(props);
    /* istanbul ignore next */
    this.handleAuth = this.handleAuth.bind(this);
Zamil Majdy's avatar
Zamil Majdy committed
24
    this.handleHome = this.handleHome.bind(this);
25
26
  }

Zamil Majdy's avatar
Zamil Majdy committed
27
28
29
30
  handleAuth = (nextState, replace) => {
    if (!Server.isLoggedIn()) replace({ pathname: '/login' });
    Storage.getUserData();
  };
31

32
  handleHome= (nextState, replace, cb) => {
33
    if (Server.isLoggedIn()) {
34
35
36
37
      Storage.getUserData().then((data) => {
        const next = data.student ? '/lowongan' : '/profile';
        replace({ pathname: next }); cb();
      });
38
39
40
41
    } else {
      replace({ pathname: '/login' });
    }
  };
42
43
44
45
46

  render = () => (
    <Router history={browserHistory}>
      <Route path="/login" component={Login} />
      <Route component={Dashboard} onEnter={this.handleAuth}>
47
48
49
        <Route path="/profile" component={Profile} />
        <Route path="/lowongan" component={VacancyPage} />
        <Route path="/users" component={Profile} />
50
      </Route>
Zamil Majdy's avatar
Zamil Majdy committed
51
52
      <Route path="/home" onEnter={this.handleHome} />
      <Redirect from="*" to="/home" />
53
54
55
    </Router>
  );
}
56
57

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