Fakultas Ilmu Komputer UI

VacancyList.jsx 5.01 KB
Newer Older
1
import React from 'react';
2
import { Item, Grid, Container, Dropdown } 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
    sort: React.PropTypes.string.isRequired,
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
    this.state = {
      vacancies: this.props.items,
      bookmarkList: [],
      appliedList: [],
      loading: true,
29
30
31
32
33
34
35
36
      salaryFilter: 0,
      salaryFilterBy: [
        { key: 0, text: 'Semua', value: 0 },
        { key: 1, text: '< 1 juta', value: 1 },
        { key: 2, text: '1-3 juta', value: 2 },
        { key: 3, text: '3-5 juta', value: 3 },
        { key: 4, text: '> 5 juta', value: 4 },
      ],
37
    };
38
    this.generateVacancies = this.generateVacancies.bind(this);
39
    this.updateStatus = this.updateStatus.bind(this);
40
    this.handleChangeSalaryFilter = this.handleChangeSalaryFilter.bind(this);
41
  }
42

43
44
45
46
47
48
49
  updateStatus(id, status) {
    const obj = [];
    this.state.vacancies.map((vacancy) => {
      if (vacancy.id !== id) return obj.push(vacancy);
      return null;
    });
    this.setState({ vacancies: obj });
50
51
  }

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

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  filterVacanciesBasedOnSalary = (vacancies) => {
    const salaryFilter = this.state.salaryFilter;
    let filteredVacancies = vacancies;
    if (salaryFilter === 1) {
      filteredVacancies = filteredVacancies.filter((vacancy) => vacancy.props.data.salary < 1000000);
    } else if (salaryFilter === 2) {
      filteredVacancies = filteredVacancies.filter((vacancy) => vacancy.props.data.salary >= 1000000 && vacancy.props.data.salary < 3000000);
    } else if (salaryFilter === 3) {
      filteredVacancies = filteredVacancies.filter((vacancy) => vacancy.props.data.salary >= 3000000 && vacancy.props.data.salary < 5000000);
    } else if (salaryFilter === 4){
      filteredVacancies = filteredVacancies.filter((vacancy) => vacancy.props.data.salary >= 5000000);
    }

    return filteredVacancies;
  }

  handleChangeSalaryFilter = (e, data) => {
    this.setState({ salaryFilter: data.value });
  }

81
  generateVacancies() {
Zamil Majdy's avatar
Zamil Majdy committed
82
83
84
85
86
87
88
89
90
91
92
    if (this.state.vacancies.length === 0) {
      return (
        <Item className="vacancyItems">
          <Grid.Row>
            <Container textAlign="center">
              <p>Tidak ada</p><br />
            </Container>
          </Grid.Row>
        </Item>
      );
    }
93
    if (this.props.user.role === 'student') {
94
      let vacancies = this.state.vacancies.map(vacancy =>
95
96
        (
          <Vacancy
97
98
99
100
101
            key={vacancy.id}
            status={vacancy.status}
            user={this.props.user}
            bookmarked={vacancy.bookmarked ? 1 : 0}
            data={vacancy}
102
            studentId={this.props.userId}
103
            salary={this.props.salary}
104
          />
105
106
        ),
      );
107
108
109
110
111
112
113
114
115
      if (this.props.sort === 'ASC') {
        return vacancies.sort((vac1, vac2) => {
          return vac1.props.data.name.localeCompare(vac2.props.data.name);
        });
      } else if (this.props.sort === 'DESC') {
        return vacancies.sort((vac1, vac2) => {
          return vac2.props.data.name.localeCompare(vac1.props.data.name);
        });
      }
116
      vacancies = this.filterVacanciesBasedOnSalary(vacancies);
117
      return vacancies;
Zamil Majdy's avatar
Zamil Majdy committed
118
    } else if ((this.props.user.role === 'admin' && this.props.user.data.company != null)
119
      || this.props.user.role === 'company') {
120
      return this.state.vacancies.map(vacancy => (
Zamil Majdy's avatar
Zamil Majdy committed
121
122
123
124
125
126
127
128
        <Item.Group key={vacancy.id} relaxed style={{ width: '100%' }}>
          <CompanyVacancy
            key={vacancy.id}
            data={vacancy}
            deleteCallback={() => this.deleteVacancy(vacancy.id)}
          />
        </Item.Group>
      ),
129
      );
130
    }
131

sirinbaisa's avatar
sirinbaisa committed
132
    return this.state.vacancies.map(vacancy => (
133
134
135
136
137
      <AdminVacancy
        key={vacancy.id}
        data={vacancy}
        updateStatus={this.updateStatus}
      />),
138
139
140
    );
  }

141
  render = () => (
142
    <div>
143
144
145
146
147
148
149
      <Dropdown
          placeholder="Filter Berdasarkan Gaji"
          className="dropdownApplicant"
          selection
          options={this.state.salaryFilterBy}
          onChange={this.handleChangeSalaryFilter}
        />
Zamil Majdy's avatar
Zamil Majdy committed
150
      <Grid container columns="eleven" doubling style={{ display: 'block' }}>
151
152
153
        <Item.Group relaxed style={{ width: '100%' }}>
          { this.generateVacancies() }
        </Item.Group>
154
      </Grid>
155
    </div>
156
  );
157
}