Fakultas Ilmu Komputer UI

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

9
10
11
12

export default class VacancyList extends React.Component {

  static propTypes = {
13
    url: React.PropTypes.string.isRequired,
14
    userId: React.PropTypes.number.isRequired,
15
    status: React.PropTypes.string,
16
    type: React.PropTypes.string,
17
18
19
  };

  static defaultProps = {
20
    status: 'Daftar',
21
    type: 'student',
22
23
  };

24
25
  constructor(props) {
    super(props);
M. Reza Qorib's avatar
M. Reza Qorib committed
26
    /* istanbul ignore next */
27
    this.state = { vacancies: [], bookmarkList: [] };
28
29
30
    if (this.props.type === 'student') {
      this.updateBookmarkList();
    }
31
    Server.get(this.props.url, false).then((data) => {
32
33
      this.setState({ vacancies: data });
    });
34
    this.updateBookmarkList = this.updateBookmarkList.bind(this);
35
    this.generateVacancies = this.generateVacancies.bind(this);
36
    this.checkBookmark = this.checkBookmark.bind(this);
37
    this.companyHeader = this.companyHeader.bind(this);
38
39
40
  }

  checkBookmark(id) {
41
42
    for (let i = 0; i < this.state.bookmarkList.length; i += 1) {
      if (id === this.state.bookmarkList[i].id) { return 1; }
43
44
    }
    return 0;
45
46
  }

47
  updateBookmarkList() {
48
    Server.get(`/students/${this.props.userId}/bookmarked-vacancies/`, false).then((data) => {
49
50
51
      this.setState({ bookmarkList: data });
    });
  }
52

53
54
55
56
57
58
59
60
61
  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);
  }));

62
  generateVacancies() {
63
64
65
66
67
68
69
70
71
72
73
74
75
    if (this.props.type === 'student') {
      return this.state.vacancies.map(vacancy =>
        (
          <Vacancy
            key={vacancy.id}
            status={this.props.status}
            bookmarked={this.checkBookmark(vacancy.id)}
            data={vacancy}
          />
        ),
      );
    }

76
77
78
79
80
    return this.state.vacancies.map(vacancy => (<CompanyVacancy
      key={vacancy.id}
      data={vacancy}
      deleteCallback={() => this.deleteVacancy(vacancy.id)}
    />),
81
82
83
    );
  }

84
85
86
  companyHeader() {
    if (this.props.type === 'company') {
      return (
87
88
89
        <Grid textAlign="center">
          <Button size="tiny" icon="eye" labelPosition="left" color="facebook" content="Lihat Semua Pendaftar" />
          <Button size="tiny" as={Link} to="/buat-lowongan" icon="add" labelPosition="left" content="Tambah Lowongan Baru" color="teal" />
90
        </Grid>
91
92
93
94
95
      );
    }

    return '';
  }
96

97
  render = () => (
98
    <Segment>
99
100
101
      <ModalAlert ref={(modal) => { this.modalAlert = modal; }} />
      <Grid container columns="eleven" doubling>
        { this.companyHeader() }
102
103
104
        <Item.Group relaxed style={{ width: '100%' }}>
          { this.generateVacancies() }
        </Item.Group>
105
      </Grid>
106
    </Segment>
107
  );
108
}