Fakultas Ilmu Komputer UI

VacancyList.jsx 2.74 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
  constructor(props) {
    super(props);
M. Reza Qorib's avatar
M. Reza Qorib committed
23
    /* istanbul ignore next */
24
25
26
27
28
29
    this.state = {
      vacancies: this.props.items,
      bookmarkList: [],
      appliedList: [],
      loading: true,
    };
30
    this.generateVacancies = this.generateVacancies.bind(this);
31
  }
32

33
34
35
36
37
38
39
40
41
  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);
  }));

42
  generateVacancies() {
43
    if (this.props.user.role === 'student') {
44
45
46
47
      return this.state.vacancies.map(vacancy =>
        (
          <Vacancy
            key={vacancy.id}
48
49
50
            status={vacancy.status}
            user={this.props.user}
            bookmarked={vacancy.bookmarked ? 1 : 0}
51
            data={vacancy}
52
            studentId={this.props.userId}
53
54
55
56
          />
        ),
      );
    }
57
58
59
60
61
62
63
64
    if (this.props.user.role === 'admin' || this.props.user.role === 'supervisor'){
      return this.state.vacancies.map(vacancy => (<AdminVacancy
        key={vacancy.id}
        data={vacancy}
      />),
      );  
    }
    
65
66
67
68
69
    return this.state.vacancies.map(vacancy => (<CompanyVacancy
      key={vacancy.id}
      data={vacancy}
      deleteCallback={() => this.deleteVacancy(vacancy.id)}
    />),
70
71
72
    );
  }

73
  companyHeader() {
74
    if (this.props.user.role === 'company') {
75
      return (
Zamil Majdy's avatar
Zamil Majdy committed
76
        <Grid textAlign="center" style={{ paddingTop: '10px' }}>
77
          <Button size="tiny" as={Link} to="/pelamar" icon="eye" labelPosition="left" color="facebook" content="Lihat Semua Pendaftar" />
78
          <Button size="tiny" as={Link} to="/buat-lowongan" icon="add" labelPosition="left" content="Tambah Lowongan Baru" color="teal" />
79
        </Grid>
80
81
82
83
84
      );
    }

    return '';
  }
85

86
  render = () => (
87
    <div>
88
89
      <Grid container columns="eleven" doubling>
        { this.companyHeader() }
90
91
92
        <Item.Group relaxed style={{ width: '100%' }}>
          { this.generateVacancies() }
        </Item.Group>
93
      </Grid>
94
    </div>
95
  );
96
}