Fakultas Ilmu Komputer UI

TopMenu.jsx 3.43 KB
Newer Older
1
import React from 'react';
sirinbaisa's avatar
sirinbaisa committed
2
import { Menu, Image, Popup, Button, Card } from 'semantic-ui-react';
3
import { Link, browserHistory } from 'react-router';
4
import Server from '../lib/Server';
5
import Storage from '../lib/Storage';
6

Zamil Majdy's avatar
Zamil Majdy committed
7
const defaultPicture = 'https://semantic-ui.com/images/avatar/small/elliot.jpg';
8

9
export default class TopMenu extends React.Component {
sirinbaisa's avatar
sirinbaisa committed
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  static getInfo(user) {
    const adminRole = {
      name: 'admin',
      user: {
        email: '',
      },
      photo: null,
    };
    const role = user.role;
    if (role === 'student') {
      return user.data.student;
    } else if (role === 'company') {
      return user.data.company;
    } else if (role === 'supervisor') {
      return user.data.supervisor;
    }

    return adminRole;
  }

31
  static propTypes = {
32
    user: React.PropTypes.object.isRequired,
33
34
  };

35
36
37
  constructor(props) {
    super(props);
    /* istanbul ignore next */
Zamil Majdy's avatar
Zamil Majdy committed
38
    this.state = { activeItem: 'Beranda', logoutLoading: false };
39
    this.logout = this.logout.bind(this);
40
    this.logoutCompany = this.logoutCompany.bind(this);
41
  }
sirinbaisa's avatar
sirinbaisa committed
42

43
44
  handleItemClick = (e, { name }) => this.setState({ activeItem: name });

45
46
  logout = (e) => {
    e.preventDefault();
Zamil Majdy's avatar
Zamil Majdy committed
47
    this.setState({ logoutLoading: true });
48
49
50
    Server.get('/api-auth/logout/?next=/', true).then(() => {
      Storage.clear();
      browserHistory.push('/login');
Zamil Majdy's avatar
Zamil Majdy committed
51
    }, () => this.setState({ logoutLoading: false }));
52
  };
53

54
55
56
57
58
59
60
  logoutCompany = /* istanbul ignore next */ (e) => {
    e.preventDefault();
    const userData = Storage.get('user-data');
    userData.company = null;
    Storage.set('user-data', userData);
    window.location.replace('/home');
  };
61

62
  render() {
sirinbaisa's avatar
sirinbaisa committed
63
    const { activeItem } = this.state;
64
    const data = TopMenu.getInfo(this.props.user);
65
    return (
66
67
68
      <div>
        { this.props.user.data.is_staff && this.props.user.data.company && (
          <div className="admin-bar">
69
70
71
72
73
            <p>
              Anda login sebagai perusahaan:
              <b> {this.props.user.data.company.name}</b> (#{this.props.user.data.company.id}).
              Untuk kembali menjadi admin, klik <a href="#" onClick={this.logoutCompany}> link ini</a>
            </p>
74
75
76
          </div>
        )}
        <Menu color="blue" pointing secondary>
Zamil Majdy's avatar
Zamil Majdy committed
77
          <Image as={Link} size="small" src="/assets/img/logo.png" to="/home" />
78
          <Menu.Menu position="right">
M. Reza Qorib's avatar
M. Reza Qorib committed
79
            <Menu.Item as={Link} to="/home" name="Beranda" active={activeItem === 'Beranda'} onClick={this.handleItemClick} />
Zamil Majdy's avatar
Zamil Majdy committed
80
81
82
83
            { this.props.user.role === 'student' &&
            <Menu.Item as={Link} to="/profil" name="Profil" active={activeItem === 'Profil'} onClick={this.handleItemClick} /> }
            { this.props.user.data.is_staff &&
            <Menu.Item as={Link} to="/lowongan" name="Lowongan" active={activeItem === 'Lowongan'} onClick={this.handleItemClick} /> }
Zamil Majdy's avatar
Zamil Majdy committed
84
            <Menu.Item style={{ padding: '10px 0' }}>
85
              <Popup
Zamil Majdy's avatar
Zamil Majdy committed
86
87
                trigger={<Image src={(this.props.user.role === 'company' ? data.logo : data.photo) || defaultPicture} avatar />}
                flowing hoverable
88
89
90
91
92
              >
                <Card
                  header={data.name}
                  description={data.user.email}
                />
Zamil Majdy's avatar
Zamil Majdy committed
93
                <Button as={Link} onClick={this.logout} loading={this.state.logoutLoading} name="logout" color="blue" size="tiny">Keluar</Button>
94
95
96
97
98
              </Popup>
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
99
100
101
    );
  }
}