Fakultas Ilmu Komputer UI

TopMenu.jsx 7.2 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
import CompanyInfo from './CompanyInfo';
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
        "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",
59
60
        kapeDescription: "Kanal Akses Pendaftaran KP Elektronik",
        welcomeStudent: "Selamat Datang, "
61
    };
62
    this.logout = this.logout.bind(this);
63
    this.logoutCompany = this.logoutCompany.bind(this);
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
64
    this.handleModalClick = this.handleModalClick.bind(this);
65
    console.log(this.state.todayQuote);
66
    console.log(Storage.get('menu-active-state'));
67
  }
sirinbaisa's avatar
sirinbaisa committed
68

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

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

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

    };
84
85
86

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


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

100

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

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

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

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

181
          <Menu.Menu position="right">
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
182
          {this.props.user.role === 'student' && (
183
            <Menu.Item>{btn}</Menu.Item>
184
185
186
          )}
          {this.props.user.role === 'company' && (
            <CompanyInfo>Company Info</CompanyInfo>
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
187
          )}
188
            <Menu.Item style={{ margin: '3px' }}>
189
              {this.state.currentDate}
190
            </Menu.Item>
191
            <Menu.Item as={Link} to="/home" name="Beranda" active={activeItem === 'Beranda'} onClick={this.handleItemClick} />
192

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
193
194
195
196
            { 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} /> }
197
            <Menu.Item as={Link} to="/notifikasi" name="Notifikasi" active={activeItem === 'Notifikasi'} onClick={this.handleItemClick} />
Zamil Majdy's avatar
Zamil Majdy committed
198
            <Menu.Item style={{ padding: '10px 0' }}>
199
              <Popup
200
201
                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
202
203
204
                on='click'
                    onClose={this.handleClose}
                    onOpen={this.handleOpen}
205
              >
206
207
208
209
                <Card
                  header={data.name}
                  description={data.user.email}
                />
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
210
                { this.props.user.role === 'student' &&
Wildan Fahmi's avatar
Wildan Fahmi committed
211
212
213
214
                <div>
                    <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} />
                </div>}
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
215
216
            <Button.Group floated="right">
                <Button as={Link} onClick={this.logout} loading={this.state.logoutLoading} name="logout" color="blue" size="tiny">Keluar</Button></Button.Group>
217
218
219
220
221
              </Popup>
            </Menu.Item>
          </Menu.Menu>
        </Menu>
      </div>
222
223
224
    );
  }
}