Fakultas Ilmu Komputer UI

TopMenu.jsx 3.9 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
import moment from 'moment';
7

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

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

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  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;
  }

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

36
37
38
  constructor(props) {
    super(props);
    /* istanbul ignore next */
39
40
41
42
43
44
    moment.locale('id');
    this.state = {
      activeItem: 'Beranda',
      logoutLoading: false,
      currentDate: moment(new Date()).format('dddd') + ', ' + moment(new Date()).format('LL'),
    };
45
    this.logout = this.logout.bind(this);
46
    this.logoutCompany = this.logoutCompany.bind(this);
47
  }
sirinbaisa's avatar
sirinbaisa committed
48

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

51
52
  logout = (e) => {
    e.preventDefault();
Zamil Majdy's avatar
Zamil Majdy committed
53
    this.setState({ logoutLoading: true });
54
55
56
    Server.get('/api-auth/logout/?next=/', true).then(() => {
      Storage.clear();
      browserHistory.push('/login');
Zamil Majdy's avatar
Zamil Majdy committed
57
    }, () => this.setState({ logoutLoading: false }));
58
  };
59

60
61
62
63
64
65
66
  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');
  };
67

68
  render() {
sirinbaisa's avatar
sirinbaisa committed
69
    const { activeItem } = this.state;
70
    const data = TopMenu.getInfo(this.props.user);
71
    return (
72
73
74
      <div>
        { this.props.user.data.is_staff && this.props.user.data.company && (
          <div className="admin-bar">
75
76
77
78
79
            <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>
80
81
82
          </div>
        )}
        <Menu color="blue" pointing secondary>
Zamil Majdy's avatar
Zamil Majdy committed
83
          <Image as={Link} size="small" src="/assets/img/logo.png" to="/home" />
84
          <Menu.Menu position="right">
85
86
87
            <Menu.Item style={{ margin: '3px' }}>
              { this.state.currentDate }
            </Menu.Item>
M. Reza Qorib's avatar
M. Reza Qorib committed
88
            <Menu.Item as={Link} to="/home" name="Beranda" active={activeItem === 'Beranda'} onClick={this.handleItemClick} />
Zamil Majdy's avatar
Zamil Majdy committed
89
            { this.props.user.role === 'student' &&
90
91
92
            <Menu.Item as={Link} to="/profil-mahasiswa" name="Profil" active={activeItem === 'Profil'} onClick={this.handleItemClick} /> }
            { this.props.user.role === 'company' &&
            <Menu.Item as={Link} to="/profil-perusahaan" name="Profil" active={activeItem === 'Profil'} onClick={this.handleItemClick} /> }
Zamil Majdy's avatar
Zamil Majdy committed
93
94
            { 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
95
            <Menu.Item style={{ padding: '10px 0' }}>
96
              <Popup
Zamil Majdy's avatar
Zamil Majdy committed
97
98
                trigger={<Image src={(this.props.user.role === 'company' ? data.logo : data.photo) || defaultPicture} avatar />}
                flowing hoverable
99
100
101
102
103
              >
                <Card
                  header={data.name}
                  description={data.user.email}
                />
Zamil Majdy's avatar
Zamil Majdy committed
104
                <Button as={Link} onClick={this.logout} loading={this.state.logoutLoading} name="logout" color="blue" size="tiny">Keluar</Button>
105
106
107
108
109
              </Popup>
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
110
111
112
    );
  }
}