Fakultas Ilmu Komputer UI

VacancyList.jsx 3.52 KB
Newer Older
1
import React from 'react';
2
import { Item, Button, Grid } from 'semantic-ui-react';
3
import { Link } from 'react-router';
4
import Vacancy from './Vacancy';
5
import CompanyVacancy from './CompanyVacancy';
6
import AdminVacancy from './AdminVacancy';
7
import Server from '../lib/Server';
8
9
10
11

export default class VacancyList extends React.Component {

  static propTypes = {
12
    user: React.PropTypes.object.isRequired,
13
    userId: React.PropTypes.number.isRequired,
14
    items: React.PropTypes.array,
15
16
17
  };

  static defaultProps = {
18
    items: [],
19
20
  };

21

22
23
  constructor(props) {
    super(props);
M. Reza Qorib's avatar
M. Reza Qorib committed
24
    /* istanbul ignore next */
25
26
27
28
29
30
    this.state = {
      vacancies: this.props.items,
      bookmarkList: [],
      appliedList: [],
      loading: true,
    };
31
    this.generateVacancies = this.generateVacancies.bind(this);
32
    this.updateStatus = this.updateStatus.bind(this);
33
  }
34

35
36
37
38
39
40
41
  updateStatus(id, status) {
    const obj = [];
    this.state.vacancies.map((vacancy) => {
      if (vacancy.id !== id) return obj.push(vacancy);
      return null;
    });
    this.setState({ vacancies: obj });
42
43
  }

44
45
46
47
48
49
50
51
52
  deleteVacancy = id => Server.delete(`/vacancies/${id}/`, this.state).then(() => {
    this.modalAlert.open('Hapus Lowongan', 'Lowongan berhasil dihapus');
    const newVacancies = [];
    this.state.vacancies.map(vacancy => vacancy.id !== id && newVacancies.push(vacancy));
    this.setState({ vacancies: newVacancies });
  }, error => error.then((r) => {
    this.modalAlert.open('Gagal Menghapus Lowongan', r.error);
  }));

53
  generateVacancies() {
54
    if (this.props.user.role === 'student') {
55
56
57
      return this.state.vacancies.map(vacancy =>
        (
          <Vacancy
58
59
60
61
62
            key={vacancy.id}
            status={vacancy.status}
            user={this.props.user}
            bookmarked={vacancy.bookmarked ? 1 : 0}
            data={vacancy}
63
            studentId={this.props.userId}
64
          />
65
66
67
        ),
      );
    }
M. Reza Qorib's avatar
M. Reza Qorib committed
68
    if ((this.props.user.role === 'admin' && this.props.user.data.supervisor != null) || this.props.user.role === 'supervisor') {
69
70
71
72
73
74
75
      return this.state.vacancies.map(vacancy => (
        <AdminVacancy
          key={vacancy.id}
          data={vacancy}
          updateStatus={this.updateStatus}
        />),
      );
76
    }
77

sirinbaisa's avatar
sirinbaisa committed
78
    return this.state.vacancies.map(vacancy => (
79
80
81
82
83
84
85
      <Item.Group key={vacancy.id} relaxed style={{ width: '100%' }}>
        <CompanyVacancy
          key={vacancy.id}
          data={vacancy}
          deleteCallback={() => this.deleteVacancy(vacancy.id)}
        />
      </Item.Group>
86
      ),
87
88
89
    );
  }

90
  companyHeader() {
M. Reza Qorib's avatar
M. Reza Qorib committed
91
    if (this.props.user.role === 'admin' || this.props.user.role === 'company') {
92
      return (
M. Reza Qorib's avatar
M. Reza Qorib committed
93
        <div>
94
          <Button size="tiny" as={Link} to="/pelamar" icon="eye" labelPosition="left" color="facebook" content="Lihat Semua Pendaftar" />
95
          <Button size="tiny" as={Link} to="/buat-lowongan" icon="add" labelPosition="left" content="Tambah Lowongan Baru" color="teal" />
M. Reza Qorib's avatar
M. Reza Qorib committed
96
        </div>
97
98
99
100
101
      );
    }

    return '';
  }
102

103
  render = () => (
104
    <div>
105
      <Grid container columns="eleven" doubling>
M. Reza Qorib's avatar
M. Reza Qorib committed
106
107
108
109
110
111
        <Grid textAlign="center" style={{ paddingTop: '10px' }}>
          { this.companyHeader() }<br />
          {(this.props.user.role === 'admin' || this.props.user.role === 'supervisor') &&
          <Button size="tiny" as={Link} to="/rekap" icon="dashboard" labelPosition="left" color="facebook" content="Rekap Pendaftaran" />
          }
        </Grid>
112
113
114
        <Item.Group relaxed style={{ width: '100%' }}>
          { this.generateVacancies() }
        </Item.Group>
115
      </Grid>
116
    </div>
117
  );
118
}