Fakultas Ilmu Komputer UI

TopMenu.jsx 3.24 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

7
const defaultPicture = 'http://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 */
M. Reza Qorib's avatar
M. Reza Qorib committed
38
    this.state = { activeItem: 'Beranda' };
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();
47
48
49
50
51
    Server.get('/api-auth/logout/?next=/', true).then(() => {
      Storage.clear();
      browserHistory.push('/login');
    });
  };
52

53
54
55
56
57
58
59
  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');
  };
60

61
  render() {
sirinbaisa's avatar
sirinbaisa committed
62
    const { activeItem } = this.state;
63
    const data = TopMenu.getInfo(this.props.user);
64
    return (
65
66
67
      <div>
        { this.props.user.data.is_staff && this.props.user.data.company && (
          <div className="admin-bar">
68
69
70
71
72
73
74
            <br/>
            <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>
            <br/>
75
76
77
78
79
          </div>
        )}
        <Menu color="blue" pointing secondary>
          <Image as="a" size="small" src="/assets/img/logo.png" href="/" />
          <Menu.Menu position="right">
M. Reza Qorib's avatar
M. Reza Qorib committed
80
81
82
83
            <Menu.Item as={Link} to="/home" name="Beranda" active={activeItem === 'Beranda'} onClick={this.handleItemClick} />
            { this.props.user.role === 'student' &&
            <Menu.Item as={Link} to="/profil" name="Profil" active={activeItem === 'Profil'} onClick={this.handleItemClick} />
            }
84
85
86
            <Menu.Item>
              <Popup
                trigger={<Image
M. Reza Qorib's avatar
M. Reza Qorib committed
87
                  as={Link} to="/profil" src={(this.props.user.role === 'company' ? data.logo : data.photo) || defaultPicture} avatar
88
89
90
91
92
93
94
95
96
                  onClick={this.handleItemClick}
                />}
                flowing
                hoverable
              >
                <Card
                  header={data.name}
                  description={data.user.email}
                />
M. Reza Qorib's avatar
M. Reza Qorib committed
97
                <Button as={Link} onClick={this.logout} name="logout" color="blue" size="tiny">Keluar</Button>
sirinbaisa's avatar
sirinbaisa committed
98

99
              </Popup>
sirinbaisa's avatar
sirinbaisa committed
100

101
102
103
104
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
105
106
107
    );
  }
}