Fakultas Ilmu Komputer UI

VacancyList.jsx 3.03 KB
Newer Older
1
import React from 'react';
Zamil Majdy's avatar
Zamil Majdy committed
2
import { Item, Grid, Container } from 'semantic-ui-react';
3
import Vacancy from './Vacancy';
4
import CompanyVacancy from './CompanyVacancy';
5
import AdminVacancy from './AdminVacancy';
6
import Server from '../lib/Server';
7
8
9
10

export default class VacancyList extends React.Component {

  static propTypes = {
11
    user: React.PropTypes.object.isRequired,
12
    userId: React.PropTypes.number.isRequired,
13
    items: React.PropTypes.array,
14
15
16
  };

  static defaultProps = {
17
    items: [],
18
19
  };

20
21
  constructor(props) {
    super(props);
M. Reza Qorib's avatar
M. Reza Qorib committed
22
    /* istanbul ignore next */
23
24
25
26
27
28
    this.state = {
      vacancies: this.props.items,
      bookmarkList: [],
      appliedList: [],
      loading: true,
    };
29
    this.generateVacancies = this.generateVacancies.bind(this);
30
    this.updateStatus = this.updateStatus.bind(this);
31
  }
32

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

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

51
  generateVacancies() {
Zamil Majdy's avatar
Zamil Majdy committed
52
53
54
55
56
57
58
59
60
61
62
    if (this.state.vacancies.length === 0) {
      return (
        <Item className="vacancyItems">
          <Grid.Row>
            <Container textAlign="center">
              <p>Tidak ada</p><br />
            </Container>
          </Grid.Row>
        </Item>
      );
    }
63
    if (this.props.user.role === 'student') {
64
65
66
      return this.state.vacancies.map(vacancy =>
        (
          <Vacancy
67
68
69
70
71
            key={vacancy.id}
            status={vacancy.status}
            user={this.props.user}
            bookmarked={vacancy.bookmarked ? 1 : 0}
            data={vacancy}
72
            studentId={this.props.userId}
73
            salary={this.props.salary}
74
          />
75
76
        ),
      );
Zamil Majdy's avatar
Zamil Majdy committed
77
    } else if ((this.props.user.role === 'admin' && this.props.user.data.company != null)
78
      || this.props.user.role === 'company') {
79
      return this.state.vacancies.map(vacancy => (
Zamil Majdy's avatar
Zamil Majdy committed
80
81
82
83
84
85
86
87
        <Item.Group key={vacancy.id} relaxed style={{ width: '100%' }}>
          <CompanyVacancy
            key={vacancy.id}
            data={vacancy}
            deleteCallback={() => this.deleteVacancy(vacancy.id)}
          />
        </Item.Group>
      ),
88
      );
89
    }
90

sirinbaisa's avatar
sirinbaisa committed
91
    return this.state.vacancies.map(vacancy => (
92
93
94
95
96
      <AdminVacancy
        key={vacancy.id}
        data={vacancy}
        updateStatus={this.updateStatus}
      />),
97
98
99
    );
  }

100
  render = () => (
101
    <div>
Zamil Majdy's avatar
Zamil Majdy committed
102
      <Grid container columns="eleven" doubling style={{ display: 'block' }}>
103
104
105
        <Item.Group relaxed style={{ width: '100%' }}>
          { this.generateVacancies() }
        </Item.Group>
106
      </Grid>
107
    </div>
108
  );
109
}