Fakultas Ilmu Komputer UI

VacancyList.jsx 1.43 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
11
12
13
14
    studentId: React.PropTypes.number.isRequired,
  };

  static defaultProps = {
    bookmarkList: false,
15
16
  };

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

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

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

43
  generateVacancies() {
44
    return this.state.vacancies.map(vacancy =>
45
      <Vacancy key={vacancy.id} bookmarked={this.checkBookmark(vacancy.id)} data={vacancy} />,
46
47
48
    );
  }

49

50
51
52
53
54
  render = () => (
    <Item.Group relaxed>
      { this.generateVacancies() }
    </Item.Group>
  );
55
}