Fakultas Ilmu Komputer UI

TopMenu.jsx 5.34 KB
Newer Older
1
import React from 'react';
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
2
import { Menu, Image, Popup, Icon, Card, Button } 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';
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
7
import ModalAlert from './ModalAlert';
8

9
var quotes = require('starwars');
Zamil Majdy's avatar
Zamil Majdy committed
10
const defaultPicture = 'https://semantic-ui.com/images/avatar/small/elliot.jpg';
11

12
export default class TopMenu extends React.Component {
sirinbaisa's avatar
sirinbaisa committed
13

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

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

38
39
40
  constructor(props) {
    super(props);
    /* istanbul ignore next */
41
42
43
44
45
    moment.locale('id');
    this.state = {
      activeItem: 'Beranda',
      logoutLoading: false,
      currentDate: moment(new Date()).format('dddd') + ', ' + moment(new Date()).format('LL'),
46
      todayQuote: quotes(),
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
47
48
49
50
51
52
53
54
      applianceInformation: (
        "1. Pastikan anda sudah mengupdate profile anda\n" +
        "2. Cari lowongan internship yang ada di tab lowongan\n" +
        "3. Klik tombol 'Detail'\n" +
        "4. Pastikan persyaratan sudah terpenuhi\n" +
        "5. Jelaskan mengapa anda ingin mengikuti lowongan internship tersebut di 'Cover Letter'\n"+
        "6. Klik tombol 'Daftar'\n"
      )
55
    };
56
    this.logout = this.logout.bind(this);
57
    this.logoutCompany = this.logoutCompany.bind(this);
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
58
    this.handleModalClick = this.handleModalClick.bind(this);
59
    console.log(this.state.todayQuote);
60
  }
sirinbaisa's avatar
sirinbaisa committed
61

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

Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
64
65
66
67
68
69
70
  handleModalClick() {
    this.modalAlert.open(
      "Cara mendaftar di lowongan yang ada:",
      this.state.applianceInformation
    );
  }

71
72
  logout = (e) => {
    e.preventDefault();
Zamil Majdy's avatar
Zamil Majdy committed
73
    this.setState({ logoutLoading: true });
74
75
76
    Server.get('/api-auth/logout/?next=/', true).then(() => {
      Storage.clear();
      browserHistory.push('/login');
Zamil Majdy's avatar
Zamil Majdy committed
77
    }, () => this.setState({ logoutLoading: false }));
78
  };
79

80
81
82
83
84
85
86
  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');
  };
87

88
  render() {
sirinbaisa's avatar
sirinbaisa committed
89
    const { activeItem } = this.state;
90
    const data = TopMenu.getInfo(this.props.user);
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
91
92
93
94
95
    const btn = (
      <Button color="blue" inverted onClick={this.handleModalClick}>
        ?
      </Button>
    )
96
    return (
97
      <div>
98
99
100
        <div style={{ padding: '10px 0', fontStyle: 'italic', textAlign: 'center' }}>
          { this.state.todayQuote }
        </div>
101
102
        { this.props.user.data.is_staff && this.props.user.data.company && (
          <div className="admin-bar">
103
104
105
106
107
            <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>
108
109
          </div>
        )}
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
110
111
112
113
114
        <ModalAlert
          ref={(modal) => {
            this.modalAlert = modal;
          }}
        />
115
        <Menu color="blue" pointing secondary>
Zamil Majdy's avatar
Zamil Majdy committed
116
          <Image as={Link} size="small" src="/assets/img/logo.png" to="/home" />
117
          <Menu.Menu position="right">
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
118
119
120
            <Menu.Item>
            { btn }
            </Menu.Item>
121
122
123
            <Menu.Item style={{ margin: '3px' }}>
              { this.state.currentDate }
            </Menu.Item>
M. Reza Qorib's avatar
M. Reza Qorib committed
124
            <Menu.Item as={Link} to="/home" name="Beranda" active={activeItem === 'Beranda'} onClick={this.handleItemClick} />
Zamil Majdy's avatar
Zamil Majdy committed
125
            { this.props.user.role === 'student' &&
126
            <Menu.Item as={Link} to="/profil-mahasiswa" name="Profil" active={activeItem === 'Profil'} onClick={this.handleItemClick} /> }
127
            <Menu.Item as={Link} to="/edit-profil-mahasiswa" name="Edit Profil" active={activeItem === 'EditProfil'} onClick={this.handleItemClick} /> }
128
129
            { 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
130
131
            { this.props.user.data.is_staff &&
            <Menu.Item as={Link} to="/lowongan" name="Lowongan" active={activeItem === 'Lowongan'} onClick={this.handleItemClick} /> }
132
            <Menu.Item as={Link} to="/notifikasi" name="Notifikasi" active={activeItem === 'Notifikasi'} onClick={this.handleItemClick} />
Zamil Majdy's avatar
Zamil Majdy committed
133
            <Menu.Item style={{ padding: '10px 0' }}>
134
              <Popup
Zamil Majdy's avatar
Zamil Majdy committed
135
136
                trigger={<Image src={(this.props.user.role === 'company' ? data.logo : data.photo) || defaultPicture} avatar />}
                flowing hoverable
137
138
139
140
141
              >
                <Card
                  header={data.name}
                  description={data.user.email}
                />
Zamil Majdy's avatar
Zamil Majdy committed
142
                <Button as={Link} onClick={this.logout} loading={this.state.logoutLoading} name="logout" color="blue" size="tiny">Keluar</Button>
143
144
145
146
147
              </Popup>
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
148
149
150
    );
  }
}