Fakultas Ilmu Komputer UI

TopMenu.jsx 6.41 KB
Newer Older
1
import React from 'react';
2
3
import PropTypes from 'prop-types';
import { Menu, Image, Popup, Card, Button } from 'semantic-ui-react';
4
import { Link, browserHistory } from 'react-router';
5
import Server from '../lib/Server';
6
import Storage from '../lib/Storage';
7
import moment from 'moment';
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
8
import ModalAlert from './ModalAlert';
9

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

13
export default class TopMenu extends React.Component {
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: PropTypes.object.isRequired,
36
37
  };

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

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

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

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

85
86
87
88
89
90
91
  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');
  };
92

93
  render() {
sirinbaisa's avatar
sirinbaisa committed
94
    const { activeItem } = this.state;
95
    const data = TopMenu.getInfo(this.props.user);
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
96
97
98
99
    const btn = (
      <Button color="blue" inverted onClick={this.handleModalClick}>
        ?
      </Button>
100
    );
101
    return (
102
      <div>
103
104
105
106
107
108
109
110
        <div
          style={{
            padding: '10px 0',
            fontStyle: 'italic',
            textAlign: 'center',
          }}
        >
          {this.state.todayQuote}
111
        </div>
112
        {this.props.user.data.is_staff && this.props.user.data.company && (
113
          <div className="admin-bar">
114
115
            <p>
              Anda login sebagai perusahaan:
116
117
118
119
120
121
122
              <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>
123
            </p>
124
125
          </div>
        )}
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
126
127
128
129
130
        <ModalAlert
          ref={(modal) => {
            this.modalAlert = modal;
          }}
        />
131
        <Menu color="blue" pointing secondary>
Zamil Majdy's avatar
Zamil Majdy committed
132
          <Image as={Link} size="small" src="/assets/img/logo.png" to="/home" />
133
          <Menu.Menu position="right">
134
            <Menu.Item>{btn}</Menu.Item>
135
            <Menu.Item style={{ margin: '3px' }}>
136
              {this.state.currentDate}
137
            </Menu.Item>
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
            <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-mahasiswa"
                name="Profil"
                active={activeItem === 'Profil'}
                onClick={this.handleItemClick}
              />
            )}
            <Menu.Item
              as={Link}
              to="/edit-profil-mahasiswa"
              name="Edit Profil"
              active={activeItem === 'EditProfil'}
              onClick={this.handleItemClick}
            />{' '}
            }
            {this.props.user.role === 'company' && (
              <Menu.Item
                as={Link}
                to="/profil-perusahaan"
                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}
              />
            )}
            <Menu.Item
              as={Link}
              to="/notifikasi"
              name="Notifikasi"
              active={activeItem === 'Notifikasi'}
              onClick={this.handleItemClick}
            />
Zamil Majdy's avatar
Zamil Majdy committed
187
            <Menu.Item style={{ padding: '10px 0' }}>
188
              <Popup
189
190
191
192
193
194
195
196
197
198
199
200
                trigger={
                  <Image
                    src={
                      (this.props.user.role === 'company'
                        ? data.logo
                        : data.photo) || defaultPicture
                    }
                    avatar
                  />
                }
                flowing
                hoverable
201
              >
202
203
204
205
206
207
208
209
210
211
212
                <Card header={data.name} description={data.user.email} />
                <Button
                  as={Link}
                  onClick={this.logout}
                  loading={this.state.logoutLoading}
                  name="logout"
                  color="blue"
                  size="tiny"
                >
                  Keluar
                </Button>
213
214
215
216
217
              </Popup>
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
218
219
220
    );
  }
}