Fakultas Ilmu Komputer UI

VacancyList.jsx 6.5 KB
Newer Older
1
import React from 'react';
2
import PropTypes from 'prop-types';
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
3
import { Item, Grid, Container, Dropdown } from 'semantic-ui-react';
4
import Vacancy from './Vacancy';
5
import CompanyVacancy from './CompanyVacancy';
6
import AdminVacancy from './AdminVacancy';
7
import Server from '../lib/Server';
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
8
import moment from 'moment';
9
10
11

export default class VacancyList extends React.Component {
  static propTypes = {
12
13
14
15
    user: PropTypes.object.isRequired,
    userId: PropTypes.number.isRequired,
    items: PropTypes.array,
    sort: PropTypes.string.isRequired,
16
17
18
  };

  static defaultProps = {
19
    items: [],
20
21
  };

22
23
  constructor(props) {
    super(props);
M. Reza Qorib's avatar
M. Reza Qorib committed
24
    /* istanbul ignore next */
25
26
27
28
29
    this.state = {
      vacancies: this.props.items,
      bookmarkList: [],
      appliedList: [],
      loading: true,
30
31
32
33
34
35
36
37
      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 },
      ],
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
38
39
40
41
42
43
      vacancyStateFilter: 0,
      vacancyStateFilterBy: [
        { key: 0, text: 'Semua', value: 0 },
        { key: 1, text: 'Masih Menerima', value: 1 },
        { key: 2, text: 'Sudah Tutup', value: 2 },
      ],
44
    };
45
    this.generateVacancies = this.generateVacancies.bind(this);
46
    this.updateStatus = this.updateStatus.bind(this);
47
    this.handleChangeSalaryFilter = this.handleChangeSalaryFilter.bind(this);
48
49
50
    this.handleChangeVacancyStateFilter = this.handleChangeVacancyStateFilter.bind(
      this,
    );
51
  }
52

53
54
55
56
57
58
59
  updateStatus(id, status) {
    const obj = [];
    this.state.vacancies.map((vacancy) => {
      if (vacancy.id !== id) return obj.push(vacancy);
      return null;
    });
    this.setState({ vacancies: obj });
60
61
  }

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  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);
        }),
    );
77

78
  filterVacanciesBasedOnSalary = (vacancies) => {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
79
    const salaryFilter = this.state.salaryFilter;
80
81
    let filteredVacancies = vacancies;
    if (salaryFilter === 1) {
82
83
84
      filteredVacancies = filteredVacancies.filter(
        (vacancy) => vacancy.props.data.salary < 1000000,
      );
85
    } else if (salaryFilter === 2) {
86
      filteredVacancies = filteredVacancies.filter(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
87
88
89
        (vacancy) =>
          vacancy.props.data.salary >= 1000000 &&
          vacancy.props.data.salary < 3000000,
90
      );
91
    } else if (salaryFilter === 3) {
92
      filteredVacancies = filteredVacancies.filter(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
93
94
95
        (vacancy) =>
          vacancy.props.data.salary >= 3000000 &&
          vacancy.props.data.salary < 5000000,
96
97
98
99
100
      );
    } else if (salaryFilter === 4) {
      filteredVacancies = filteredVacancies.filter(
        (vacancy) => vacancy.props.data.salary >= 5000000,
      );
101
102
103
    }

    return filteredVacancies;
104
  };
105

Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
106
  filterVacanciesBasedOnVacancyStatus = (vacancies) => {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
107
    const vacancyStateFilter = this.state.vacancyStateFilter;
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
108
109
    let filteredVacancies = vacancies;
    if (vacancyStateFilter === 1) {
110
111
112
      filteredVacancies = filteredVacancies.filter(
        (vacancy) => vacancy.props.data.close_time >= moment().format(),
      );
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
113
    } else if (vacancyStateFilter === 2) {
114
115
116
      filteredVacancies = filteredVacancies.filter(
        (vacancy) => vacancy.props.data.close_time < moment().format(),
      );
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
117
118
119
    }

    return filteredVacancies;
120
  };
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
121

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

Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
126
127
  handleChangeVacancyStateFilter = (e, data) => {
    this.setState({ vacancyStateFilter: data.value });
128
  };
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
129

130
  generateVacancies() {
Zamil Majdy's avatar
Zamil Majdy committed
131
132
133
134
135
    if (this.state.vacancies.length === 0) {
      return (
        <Item className="vacancyItems">
          <Grid.Row>
            <Container textAlign="center">
136
137
              <p>Tidak ada</p>
              <br />
Zamil Majdy's avatar
Zamil Majdy committed
138
139
140
141
142
            </Container>
          </Grid.Row>
        </Item>
      );
    }
143
    if (this.props.user.role === 'student') {
144
145
146
147
148
149
150
151
152
153
154
      let vacancies = this.state.vacancies.map((vacancy) => (
        <Vacancy
          key={vacancy.id}
          status={vacancy.status}
          user={this.props.user}
          bookmarked={vacancy.bookmarked ? 1 : 0}
          data={vacancy}
          studentId={this.props.userId}
          salary={this.props.salary}
        />
      ));
155
      if (this.props.sort === 'ASC') {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
156
157
158
159
160
161
162
        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);
        });
163
      }
164
      vacancies = this.filterVacanciesBasedOnSalary(vacancies);
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
165
      vacancies = this.filterVacanciesBasedOnVacancyStatus(vacancies);
166
      return vacancies;
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
167
168
169
170
    } else if (
      (this.props.user.role === 'admin' &&
        this.props.user.data.company != null) ||
      this.props.user.role === 'company'
171
172
    ) {
      return this.state.vacancies.map((vacancy) => (
Zamil Majdy's avatar
Zamil Majdy committed
173
174
175
176
177
178
179
        <Item.Group key={vacancy.id} relaxed style={{ width: '100%' }}>
          <CompanyVacancy
            key={vacancy.id}
            data={vacancy}
            deleteCallback={() => this.deleteVacancy(vacancy.id)}
          />
        </Item.Group>
180
      ));
181
    }
182

183
    return this.state.vacancies.map((vacancy) => (
184
185
186
187
      <AdminVacancy
        key={vacancy.id}
        data={vacancy}
        updateStatus={this.updateStatus}
188
189
      />
    ));
190
191
  }

192
  render = () => (
193
    <div>
194
      <Dropdown
195
196
197
198
199
200
        placeholder="Filter Berdasarkan Gaji"
        className="dropdownApplicant"
        selection
        options={this.state.salaryFilterBy}
        onChange={this.handleChangeSalaryFilter}
      />
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
201
      <Dropdown
202
203
204
205
206
207
        placeholder="Filter Berdasarkan Batas Daftar Lowongan"
        className="dropdownApplicant"
        selection
        options={this.state.vacancyStateFilterBy}
        onChange={this.handleChangeVacancyStateFilter}
      />
Zamil Majdy's avatar
Zamil Majdy committed
208
      <Grid container columns="eleven" doubling style={{ display: 'block' }}>
209
        <Item.Group relaxed style={{ width: '100%' }}>
210
          {this.generateVacancies()}
211
        </Item.Group>
212
      </Grid>
213
    </div>
214
  );
215
}