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
    this.updateStatus = this.updateStatus.bind(this);
32
  }
33

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

43
44
45
46
47
48
49
50
51
  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);
  }));

52
  generateVacancies() {
53
    if (this.props.user.role === 'student') {
54
55
56
      return this.state.vacancies.map(vacancy =>
        (
          <Vacancy
57
58
59
60
61
            key={vacancy.id}
            status={vacancy.status}
            user={this.props.user}
            bookmarked={vacancy.bookmarked ? 1 : 0}
            data={vacancy}
62
            studentId={this.props.userId}
63
          />
64
65
        ),
      );
66
67
    }  else if ((this.props.user.role === 'admin' && this.props.user.data.company != null)
      || this.props.user.role === 'company') {
68
      return this.state.vacancies.map(vacancy => (
69
70
71
72
73
74
75
76
          <Item.Group key={vacancy.id} relaxed style={{ width: '100%' }}>
            <CompanyVacancy
              key={vacancy.id}
              data={vacancy}
              deleteCallback={() => this.deleteVacancy(vacancy.id)}
            />
          </Item.Group>
        ),
77
      );
78
    }
79

sirinbaisa's avatar
sirinbaisa committed
80
    return this.state.vacancies.map(vacancy => (
81
82
83
84
85
      <AdminVacancy
        key={vacancy.id}
        data={vacancy}
        updateStatus={this.updateStatus}
      />),
86
87
88
    );
  }

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