Fakultas Ilmu Komputer UI

TopMenu.jsx 6.69 KB
Newer Older
1
import React from 'react';
2
import PropTypes from 'prop-types';
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
3
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';
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
7
import moment from 'moment';
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
8
import ModalAlert from './ModalAlert';
9

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
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
  static getInfo(user) {
    const adminRole = {
      name: 'admin',
      user: {
        email: '',
      },
      photo: null,
    };
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
22
    const role = user.role;
23
24
    if (role === 'student') {
      return user.data.student;
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
25
    } else if (role === 'company') {
26
      return user.data.company;
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
27
    } else if (role === 'supervisor') {
28
29
30
31
32
33
      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
    moment.locale('id');
    this.state = {
43
44
      // activeItem: Storage.get('menu-active-state'),
      activeItem: this.menuActiveState(),
45
      logoutLoading: false,
46
      currentDate:
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
47
48
49
        moment(new Date()).format('dddd') +
        ', ' +
        moment(new Date()).format('LL'),
50
      todayQuote: quotes(),
51
      isOpen: false,
52
      applianceInformation:
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
53
54
55
56
57
58
59
        "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",
        kapeDescription: "Kanal Akses Pendaftaran KP Elektronik"
60

61

62
    };
63
    this.logout = this.logout.bind(this);
64
    this.logoutCompany = this.logoutCompany.bind(this);
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
65
    this.handleModalClick = this.handleModalClick.bind(this);
66
    console.log(this.state.todayQuote);
67
    console.log(Storage.get('menu-active-state'));
68
  }
sirinbaisa's avatar
sirinbaisa committed
69

70
  handleItemClick = (e, { name }) => {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
71
    if(name === 'Logo') {
72
73
74
75
76
      this.setState({ activeItem: 'Beranda' });
    } else {
      this.setState({ activeItem: name });
    }
  }
77

78
  handleOpen = () => {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
79

80
    this.setState({
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
81
      isOpen: true
82
    });
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
83
84

    };
85
86
87

  handleClose = () => {
    this.setState({
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
88
      isOpen: false
89
    });
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
90
    clearTimeout(this.timeout)
91
92
93
  };


Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
94
95
  handleModalClick() {
    this.modalAlert.open(
96
97
      'Cara mendaftar di lowongan yang ada:',
      this.state.applianceInformation,
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
98
99
100
    );
  }

101

102
103
  logout = (e) => {
    e.preventDefault();
Zamil Majdy's avatar
Zamil Majdy committed
104
    this.setState({ logoutLoading: true });
105
106
107
108
109
110
111
    Server.get('/api-auth/logout/?next=/', true).then(
      () => {
        Storage.clear();
        browserHistory.push('/login');
      },
      () => this.setState({ logoutLoading: false }),
    );
112
  };
113

114
115
116
117
118
119
120
  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');
  };
121

122
123
124
125
126
127
128
  menuActiveState() {
    if (Storage.get('menu-active-state') == null) {
      return 'Beranda';
    }
    return Storage.get('menu-active-state');
  }

129
  render() {
sirinbaisa's avatar
sirinbaisa committed
130
    const { activeItem } = this.state;
131
    const data = TopMenu.getInfo(this.props.user);
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
132
133
    const btn = (
      <Button color="blue" inverted onClick={this.handleModalClick}>
134
        Cara Apply
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
135
      </Button>
136
    );
137
    return (
138
      <div>
139
140
141
142
143
144
145
146
        <div
          style={{
            padding: '10px 0',
            fontStyle: 'italic',
            textAlign: 'center',
          }}
        >
          {this.state.todayQuote}
147
        </div>
148
        {this.props.user.data.is_staff && this.props.user.data.company && (
149
          <div className="admin-bar">
150
151
            <p>
              Anda login sebagai perusahaan:
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
152
153
154
              <b> {this.props.user.data.company.name}</b> (#
              {this.props.user.data.company.id}). Untuk kembali menjadi admin,
              klik{' '}
155
156
157
158
              <a href="#" onClick={this.logoutCompany}>
                {' '}
                link ini
              </a>
159
            </p>
160
161
          </div>
        )}
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
162
163
164
165
166
        <ModalAlert
          ref={(modal) => {
            this.modalAlert = modal;
          }}
        />
167
        <Menu color="blue" pointing secondary>
168
          <Menu.Menu position="left">
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
169
170
171
172
          <Menu.Item style={{ padding: '0px' }} as={Link} to="/home" name="Logo" onClick={this.handleItemClick}>
            <Image size="small" src="/assets/img/logo.png" />
            {this.state.kapeDescription}
          </Menu.Item>
173
          </Menu.Menu>
174

175
          <Menu.Menu position="right">
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
176
          {this.props.user.role === 'student' && (
177
            <Menu.Item>{btn}</Menu.Item>
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
178
          )}
179
            <Menu.Item style={{ margin: '3px' }}>
180
              {this.state.currentDate}
181
            </Menu.Item>
182
            <Menu.Item as={Link} to="/home" name="Beranda" active={activeItem === 'Beranda'} onClick={this.handleItemClick} />
183

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
184
185
186
187
            { 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} /> }
188
            <Menu.Item as={Link} to="/notifikasi" name="Notifikasi" active={activeItem === 'Notifikasi'} onClick={this.handleItemClick} />
Zamil Majdy's avatar
Zamil Majdy committed
189
            <Menu.Item style={{ padding: '10px 0' }}>
190
              <Popup
191
192
                trigger={<Image src={(this.props.user.role === 'company' ? data.logo : data.photo) || defaultPicture} avatar />}
                open={this.state.isOpen}
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
193
194
195
                on='click'
                    onClose={this.handleClose}
                    onOpen={this.handleOpen}
196
              >
197
198
199
200
                <Card
                  header={data.name}
                  description={data.user.email}
                />
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
201
202
203
204
205
                { this.props.user.role === 'student' &&
            <Menu.Item as={Link} to="/profil-mahasiswa" style={{ margin: '10px' }} name="Profil" active={activeItem === 'Profil'} onClick={this.handleItemClick} /> }
            <Menu.Item as={Link} to="/edit-profil-mahasiswa" style={{ margin: '10px' }} name="Edit Profil" active={activeItem === 'EditProfil'} onClick={this.handleItemClick} />
            <Button.Group floated="right">
                <Button as={Link} onClick={this.logout} loading={this.state.logoutLoading} name="logout" color="blue" size="tiny">Keluar</Button></Button.Group>
206
207
208
209
210
              </Popup>
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
211
212
213
    );
  }
}