Fakultas Ilmu Komputer UI

CreateVacancy.jsx 8.01 KB
Newer Older
1
import React from 'react';
2
import PropTypes from 'prop-types';
Syahrul Findi's avatar
Syahrul Findi committed
3
import { Segment, Button, Form, Header, Icon, Input } from 'semantic-ui-react';
4
5
6
import { browserHistory } from 'react-router';
import DatePicker from 'react-datepicker';
import moment from 'moment';
7
import CKEditor from 'react-ckeditor-wrapper';
8
9
10
11
12
13
14
15
16
import ModalAlert from './components/ModalAlert';
import Server from './lib/Server';

export default class CreateVacancy extends React.Component {
  constructor(props) {
    super(props);
    /* istanbul ignore next */
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
17
    this.handleDescription = this.handleDescription.bind(this);
Hadlina Rahmadinni's avatar
Hadlina Rahmadinni committed
18
    this.handleRequirements = this.handleRequirements.bind(this);
19
    this.handleResponsibilities = this.handleResponsibilities.bind(this);
20
21
    this.setCloseTime = this.setCloseTime.bind(this);
    this.setOpenTime = this.setOpenTime.bind(this);
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
22
23
    this.handleDescription = this.handleDescription.bind(this);
    this.handleBenefits = this.handleBenefits.bind(this);
24

25
26
    this.state = {
      formLoading: false,
Syahrul Findi's avatar
Syahrul Findi committed
27
28
29
      loading: !!props.params.id,
      company: props.user.data.company,
      vacancyId: props.params.id,
30
31
      open_time: moment(),
      close_time: moment(),
32
33
      name: '',
      description: '',
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
34
      benefits: '',
Hadlina Rahmadinni's avatar
Hadlina Rahmadinni committed
35
      requirements: '',
36
      salary: 0,
RANI LASMA ULI's avatar
RANI LASMA ULI committed
37
      tag: '',
Kemas Khaidar Ali's avatar
Kemas Khaidar Ali committed
38
      responsibilities: '',
39
    };
40

Syahrul Findi's avatar
Syahrul Findi committed
41
42
    if (props.params.id) {
      Server.get(`/vacancies/${props.params.id}/`).then((r) => {
43
44
        this.setState({
          description: r.description,
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
45
          benefits: r.benefits,
46
          requirements: r.requirements,
47
          responsibilities: r.responsibilities,
48
          name: r.name,
49
          salary: r.salary,
50
51
52
          open_time: moment(r.open_time),
          close_time: moment(r.close_time),
          loading: false,
RANI LASMA ULI's avatar
RANI LASMA ULI committed
53
          tag: r.tag,
Fajrin Kingwijati's avatar
Fajrin Kingwijati committed
54
55
          working_period: r.working_period,
          max_accepted_applicants: r.max_accepted_applicants,
56
        });
57
      });
58
    }
59
60
  }

61
62
63
64
65
66
67
68
  setOpenTime(date) {
    this.setState({ open_time: date });
  }

  setCloseTime(date) {
    this.setState({ close_time: date });
  }

69
70
71
72
73
  handleChange = (e) => {
    this.setState({ [e.target.name]: e.target.value });
  };

  handleSubmit = (e) => {
Syahrul Findi's avatar
Syahrul Findi committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    const {
      name,
      tag,
      responsibilities,
      description,
      benefits,
      requirements,
      salary,
      open_time,
      close_time,
      max_accepted_applicants,
      working_period,
      company,
      vacancyId,
    } = this.state;
89
90
91
    e.preventDefault();
    this.setState({ formLoading: true });

92
    const data = {};
Syahrul Findi's avatar
Syahrul Findi committed
93
94
95
96
97
98
99
100
101
102
103
104
105
    data.name = name;
    data.tag = tag;
    data.responsibilities = responsibilities;
    data.description = description;
    data.benefits = benefits;
    data.requirements = requirements;
    data.salary = salary;
    data.open_time = open_time.format();
    data.close_time = close_time.format();
    data.max_accepted_applicants = max_accepted_applicants;
    data.working_period = working_period;
    if (!vacancyId) {
      data.company = company.id;
106
    }
107

Syahrul Findi's avatar
Syahrul Findi committed
108
109
    const url = vacancyId ? `/vacancies/${vacancyId}/` : '/vacancies/';
    const method = vacancyId ? 'PATCH' : 'POST';
110

111
112
113
114
    Server.sendRequest(url, method, data).then(
      () => {
        browserHistory.push('/lowongan');
      },
Syahrul Findi's avatar
Syahrul Findi committed
115
116
117
118
119
      (error) =>
        error.then((r) => {
          this.modalAlert.open('Gagal Membuat Lowongan', r.error);
          this.setState({ formLoading: false });
        }),
120
    );
121
122
  };

Syahrul Findi's avatar
Syahrul Findi committed
123
124
125
126
127
128
129
130
131
132
133
  handleResponsibilities(value) {
    this.setState({ responsibilities: value });
  }

  handleRequirements(value) {
    this.setState({ requirements: value });
  }

  handleBenefits(value) {
    this.setState({ benefits: value });
  }
RANI LASMA ULI's avatar
RANI LASMA ULI committed
134

Syahrul Findi's avatar
Syahrul Findi committed
135
136
137
  handleDescription(value) {
    this.setState({ description: value });
  }
RANI LASMA ULI's avatar
RANI LASMA ULI committed
138

Syahrul Findi's avatar
Syahrul Findi committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  render = () => {
    const {
      name,
      tag,
      responsibilities,
      description,
      benefits,
      requirements,
      salary,
      open_time,
      close_time,
      max_accepted_applicants,
      working_period,
      company,
      vacancyId,
      loading,
      formLoading,
    } = this.state;
    return (
      <div className="create-lowongan">
        <ModalAlert
          ref={(modal) => {
            this.modalAlert = modal;
          }}
        />
        <Segment className="form-segment">
          <Header as="h2" icon textAlign="center">
            <Icon name="briefcase" circular />
            <Header.Content>Lowongan KP</Header.Content>
          </Header>
          <Form loading={formLoading} onSubmit={this.handleSubmit}>
170
            <Form.Field
Syahrul Findi's avatar
Syahrul Findi committed
171
172
173
174
175
              label="Posisi"
              name="name"
              control={Input}
              onChange={this.handleChange}
              value={name}
176
177
              required
            />
Syahrul Findi's avatar
Syahrul Findi committed
178
179
180
181
182
183
184
185
            <label htmlFor="description">
              {' '}
              <b> Deskripsi Lowongan </b>{' '}
            </label>
            {!loading && (
              <CKEditor value={description} onChange={this.handleDescription} />
            )}
            <script>CKEDITOR.replace( &lsquo;description&rsquo; );</script>
186
            <Form.Field
Syahrul Findi's avatar
Syahrul Findi committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
              label="Periode Kerja"
              name="working_period"
              control={Input}
              onChange={this.handleChange}
              value={working_period}
            />
            <label htmlFor="responsibilities">
              {' '}
              <b> Tanggung Jawab Lowongan </b>{' '}
            </label>
            {!loading && (
              <CKEditor
                value={responsibilities}
                onChange={this.handleResponsibilities}
              />
            )}
            <script>CKEDITOR.replace( &lsquo;responsibilities&rsquo; );</script>
            <br />
            <Form.Field
              label="Jumlah yang Diterima"
              name="max_accepted_applicants"
              type="number"
              min="1"
              control={Input}
              onChange={this.handleChange}
              value={max_accepted_applicants}
213
214
              required
            />
Syahrul Findi's avatar
Syahrul Findi committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
            <label htmlFor="benefits">
              {' '}
              <b> Keuntungan </b>{' '}
            </label>
            {!loading && (
              <CKEditor value={benefits} onChange={this.handleBenefits} />
            )}
            <script>CKEDITOR.replace( &lsquo;benefits&rsquo; );</script>
            <br />
            <label htmlFor="requirements">
              {' '}
              <b> Persyaratan </b>{' '}
            </label>
            {!loading && (
              <CKEditor
                value={requirements}
                onChange={this.handleRequirements}
              />
            )}
            <script>CKEDITOR.replace( &lsquo;requirements&rsquo; );</script>
            <br />

            <Form.Field
              label="Tag"
              name="tag"
              control={Input}
              onChange={this.handleChange}
              value={tag}
            />
            <br />

            <Form.Field
              label="Gaji / Insentif (Rupiah per bulan)"
              type="number"
              min="0"
              name="salary"
              control={Input}
              onChange={this.handleChange}
              value={salary}
            />
            <Form.Group widths="equal">
              <Form.Field
                className="open-time-field"
                control={DatePicker}
                label="Waktu Buka Lowongan"
                selected={open_time}
                onChange={this.setOpenTime}
                required
              />
              <Form.Field
                className="close-time-field"
                control={DatePicker}
                label="Waktu Tutup Lowongan"
                selected={close_time}
                onChange={this.setCloseTime}
                required
              />
            </Form.Group>
            <Button type="submit" primary floated="right">
              Submit
            </Button>
          </Form>
        </Segment>
      </div>
    );
  };
281
}
Syahrul Findi's avatar
Syahrul Findi committed
282
283
284
285
286

CreateVacancy.propTypes = {
  params: PropTypes.object.isRequired,
  user: PropTypes.object.isRequired,
};