Fakultas Ilmu Komputer UI

VacancyList.jsx 1.53 KB
Newer Older
1
import React from 'react';
2
import { Item } from 'semantic-ui-react';
3
import Vacancy from './Vacancy';
4
import Server from '../lib/Server';
5
6
7
8

export default class VacancyList extends React.Component {

  static propTypes = {
9
    url: React.PropTypes.string.isRequired,
10
    studentId: React.PropTypes.number.isRequired,
11
    status: React.PropTypes.string,
12
13
14
  };

  static defaultProps = {
15
    status: 'Daftar',
16
17
  };

18
19
  constructor(props) {
    super(props);
M. Reza Qorib's avatar
M. Reza Qorib committed
20
    /* istanbul ignore next */
21
    this.updateBookmarkList();
22
    Server.get(this.props.url, false).then((data) => {
23
24
      this.setState({ vacancies: data });
    });
25
26
    this.state = { vacancies: [], bookmarkList: [] };
    this.updateBookmarkList = this.updateBookmarkList.bind(this);
27
    this.generateVacancies = this.generateVacancies.bind(this);
28
29
30
31
    this.checkBookmark = this.checkBookmark.bind(this);
  }

  checkBookmark(id) {
32
33
    for (let i = 0; i < this.state.bookmarkList.length; i += 1) {
      if (id === this.state.bookmarkList[i].id) { return 1; }
34
35
    }
    return 0;
36
37
  }

38
39
40
41
42
  updateBookmarkList() {
    Server.get(`/students/${this.props.studentId}/bookmarked-vacancies/`, false).then((data) => {
      this.setState({ bookmarkList: data });
    });
  }
43

44
  generateVacancies() {
45
    return this.state.vacancies.map(vacancy =>
46
47
48
49
50
51
      <Vacancy
        key={vacancy.id}
        status={this.props.status}
        bookmarked={this.checkBookmark(vacancy.id)}
        data={vacancy}
      />,
52
53
54
    );
  }

55

56
57
58
59
60
  render = () => (
    <Item.Group relaxed>
      { this.generateVacancies() }
    </Item.Group>
  );
61
}