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