Fakultas Ilmu Komputer UI

VacancyList.jsx 6.27 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';
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
7
import moment from 'moment';
8
9
10
11

export default class VacancyList extends React.Component {

  static propTypes = {
12
    user: React.PropTypes.object.isRequired,
13
    userId: React.PropTypes.number.isRequired,
14
    items: React.PropTypes.array,
15
    sort: React.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);
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
48
    this.handleChangeVacancyStateFilter = this.handleChangeVacancyStateFilter.bind(this);
49
  }
50

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

60
61
62
63
64
65
66
67
68
  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);
  }));

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  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;
  }

Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
85
86
87
88
89
90
91
92
93
94
95
96
  filterVacanciesBasedOnVacancyStatus = (vacancies) => {
    const vacancyStateFilter = this.state.vacancyStateFilter;
    let filteredVacancies = vacancies;
    if (vacancyStateFilter === 1) {
      filteredVacancies = filteredVacancies.filter((vacancy) => vacancy.props.data.close_time >= moment().format());
    } else if (vacancyStateFilter === 2) {
      filteredVacancies = filteredVacancies.filter((vacancy) => vacancy.props.data.close_time < moment().format());
    }

    return filteredVacancies;
  }

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

Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
101
102
103
104
  handleChangeVacancyStateFilter = (e, data) => {
    this.setState({ vacancyStateFilter: data.value });
  }

105
  generateVacancies() {
Zamil Majdy's avatar
Zamil Majdy committed
106
107
108
109
110
111
112
113
114
115
116
    if (this.state.vacancies.length === 0) {
      return (
        <Item className="vacancyItems">
          <Grid.Row>
            <Container textAlign="center">
              <p>Tidak ada</p><br />
            </Container>
          </Grid.Row>
        </Item>
      );
    }
117
    if (this.props.user.role === 'student') {
118
      let vacancies = this.state.vacancies.map(vacancy =>
119
120
        (
          <Vacancy
121
122
123
124
125
            key={vacancy.id}
            status={vacancy.status}
            user={this.props.user}
            bookmarked={vacancy.bookmarked ? 1 : 0}
            data={vacancy}
126
            studentId={this.props.userId}
127
            salary={this.props.salary}
128
          />
129
130
        ),
      );
131
132
133
134
135
136
137
138
139
      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);
        });
      }
140
      vacancies = this.filterVacanciesBasedOnSalary(vacancies);
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
141
      vacancies = this.filterVacanciesBasedOnVacancyStatus(vacancies);
142
      return vacancies;
Zamil Majdy's avatar
Zamil Majdy committed
143
    } else if ((this.props.user.role === 'admin' && this.props.user.data.company != null)
144
      || this.props.user.role === 'company') {
145
      return this.state.vacancies.map(vacancy => (
Zamil Majdy's avatar
Zamil Majdy committed
146
147
148
149
150
151
152
153
        <Item.Group key={vacancy.id} relaxed style={{ width: '100%' }}>
          <CompanyVacancy
            key={vacancy.id}
            data={vacancy}
            deleteCallback={() => this.deleteVacancy(vacancy.id)}
          />
        </Item.Group>
      ),
154
      );
155
    }
156

sirinbaisa's avatar
sirinbaisa committed
157
    return this.state.vacancies.map(vacancy => (
158
159
160
161
162
      <AdminVacancy
        key={vacancy.id}
        data={vacancy}
        updateStatus={this.updateStatus}
      />),
163
164
165
    );
  }

166
  render = () => (
167
    <div>
168
169
170
171
172
173
174
      <Dropdown
          placeholder="Filter Berdasarkan Gaji"
          className="dropdownApplicant"
          selection
          options={this.state.salaryFilterBy}
          onChange={this.handleChangeSalaryFilter}
        />
Hilya Auli Fesmia's avatar
Hilya Auli Fesmia committed
175
176
177
178
179
180
181
      <Dropdown
          placeholder='Filter Berdasarkan Batas Daftar Lowongan'
          className="dropdownApplicant"
          selection
          options={this.state.vacancyStateFilterBy}
          onChange={this.handleChangeVacancyStateFilter}
        />
Zamil Majdy's avatar
Zamil Majdy committed
182
      <Grid container columns="eleven" doubling style={{ display: 'block' }}>
183
184
185
        <Item.Group relaxed style={{ width: '100%' }}>
          { this.generateVacancies() }
        </Item.Group>
186
      </Grid>
187
    </div>
188
  );
189
}