Fakultas Ilmu Komputer UI

Commit 92a0fe86 authored by M. Reza Qorib's avatar M. Reza Qorib
Browse files

[#140652771] #29 Finalize react routing

parent 1a17638c
import React from 'react';
import Tabs from './components/Tabs';
import Pane from './components/Pane';
import Storage from './lib/Storage';
import VacancyList from './components/VacancyList';
export default class VacancyPage extends React.Component {
static getRole() {
const student = Storage.get('user-data').student;
const company = Storage.get('user-data').company;
if (student) {
return { id: student.id, type: 'student' };
} else if (company) {
return { id: company.id, type: 'company' };
static propTypes = {
user: React.PropTypes.object.isRequired,
};
static getId(user) {
const role = user.role;
if (role === 'student') {
return user.data.student.id;
} else if (role === 'company') {
return user.data.company.id;
}
return { id: '0', type: 'error' };
return 0;
}
constructor(props) {
super(props);
/* istanbul ignore next */
const role = VacancyPage.getRole();
this.state = {
vacancies: [],
id: role.id,
role: role.type,
id: VacancyPage.getId(this.props.user),
};
}
generateVacancies() {
if (this.state.role === 'student') {
if (this.props.user.role === 'student') {
return (
<Tabs selected={0}>
<Pane label="Semua Lowongan" >
......@@ -57,7 +57,7 @@ export default class VacancyPage extends React.Component {
</Pane>
</Tabs>
);
} else if (this.state.role === 'company') {
} else if (this.props.user.role === 'company') {
return (
<VacancyList key={1} userId={this.state.id} url={`/companies/${this.state.id}/vacancies/`} type="company" />
);
......
......@@ -24,10 +24,10 @@ export default class VacancyList extends React.Component {
constructor(props) {
super(props);
/* istanbul ignore next */
this.state = { vacancies: [], bookmarkList: [] };
if (this.props.type === 'student') {
this.updateBookmarkList();
}
this.state = { vacancies: [], bookmarkList: [] };
Server.get(this.props.url, false).then((data) => {
this.setState({ vacancies: data });
});
......
......@@ -13,13 +13,14 @@ export default class App extends React.Component {
static getRole() {
if (Server.isLoggedIn()) {
if (Storage.get('user-data').is_staff) {
const data = Storage.get('user-data');
if (data.is_staff) {
return 'admin';
} else if (Storage.get('user-data').supervisor) {
} else if (data.supervisor) {
return 'supervisor';
} else if (Storage.get('user-data').student) {
} else if (data.student) {
return 'student';
} else if (Storage.get('user-data').company) {
} else if (data.company) {
return 'company';
}
return 'error';
......@@ -40,18 +41,19 @@ export default class App extends React.Component {
class WithAuthorization extends React.Component {
constructor(props) {
super(props);
this.state = {
user: {
role: App.getRole(),
data: Storage.get('user-data'),
},
};
}
render() {
const { role } = this.state.user;
// const props = this.props;
if (allowedRoles.includes(role)) {
return <WrappedComponent {...this.props} />;
return <WrappedComponent {...this.props} {...this.state} />;
}
return <div> { browserHistory.push('/home') } </div>;
}
......@@ -63,14 +65,15 @@ export default class App extends React.Component {
};
handleHome= (nextState, replace, cb) => {
console.log(Server.isLoggedIn());
if (Server.isLoggedIn()) {
Storage.getUserData().then(() => {
console.log('masuk');
if (App.getRole() === 'student') {
console.log('masuk3');
replace({ pathname: '/lowongan' }); cb();
} else if (App.getRole() === 'company') {
replace({ pathname: '/pelamar' }); cb();
replace({ pathname: '/lowongan' }); cb();
}
});
}
replace({ pathname: '/login' }); cb();
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment