Fakultas Ilmu Komputer UI

CompanyRegisterModal.jsx 8.9 KB
Newer Older
1
import React from 'react';
Zamil Majdy's avatar
Zamil Majdy committed
2
import { browserHistory } from 'react-router';
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
3
4
5
6
7
8
9
import {
  Modal, Button, Form, Input, TextArea, Header, Icon, Dropdown,
} from 'semantic-ui-react';
import ModalAlert from './ModalAlert';
import Server from '../lib/Server';
import Storage from '../lib/Storage';
import Dumper from '../lib/Dumper';
10
import ReCAPTCHA from "react-google-recaptcha";
11

12
export default class CompanyRegisterModal extends React.Component {
13
14
15
  constructor(props) {
    super(props);
    /* istanbul ignore next */
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
16
17
18
    this.state = {
      loading: false,
      benefits: [
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
19
20
21
22
        { text: 'Mendapatkan peserta magang dari lulusan universitas terbaik di Indonesia' },
        { text: 'Mempermudah mencari calon peserta magang' },
        { text: 'Dan banyak keuntungan lainnya' },
      ],
23
      recaptchaValue: null,
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
24
    };
25
    this.handleChange = this.handleChange.bind(this);
26
    this.handleFile = this.handleFile.bind(this);
27
28
29
    this.handleSubmit = this.handleSubmit.bind(this);
  }

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  categoryOptions = [
    { value: 'Perusahaan Asuransi', text: 'Perusahaan Asuransi' },
    { value: 'Perusahaan Bioteknologi', text: 'Perusahaan Bioteknologi' },
    { value: 'Perusahaan Elektronik', text: 'Perusahaan Elektronik' },
    { value: 'Perusahaan Energi', text: 'Perusahaan Energi' },
    { value: 'Perusahaan Farmasi', text: 'Perusahaan Farmasi' },
    { value: 'Firma Akuntansi', text: 'Firma Akuntansi' },
    { value: 'Agen Periklanan', text: 'Agen Periklanan' },
    { value: 'Perusahaan Internet', text: 'Perusahaan Internet' },
    { value: 'Perusahaan Jasa', text: 'Perusahaan Jasa' },
    { value: 'Perusahaan Komputer', text: 'Perusahaan Komputer' },
    { value: 'Perusahaan Konsultansi', text: 'Perusahaan Konsultansi' },
    { value: 'Perusahaan Logistik', text: 'Perusahaan Logistik' },
    { value: 'Perusahaan Media', text: 'Perusahaan Media' },
    { value: 'Perusahaan Perangkat Keras', text: 'Perusahaan Perangkat Keras' },
    { value: 'Perusahaan Perangkat Lunak', text: 'Perusahaan Perangkat Lunak' },
    { value: 'Perusahaan Teknologi', text: 'Perusahaan Teknologi' },
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
47
    { value: 'Perusahaan Telekomunikasi', text: 'Perusahaan Telekomunikasi' },
48
49
  ]

50
51
52
53
54
55
56
57
58
59
60
61
62
  componentWillUpdate() {
    this.fixBody();
  }

  componentDidUpdate() {
    this.fixBody();
  }

  fixBody = () => {
    const anotherModal = document.getElementsByClassName('ui page modals').length;
    if (anotherModal > 0) document.body.classList.add('scrolling', 'dimmable', 'dimmed');
  };

63
64
65
66
  handleChange = (e) => {
    this.setState({ [e.target.name]: e.target.value });
  };

67
68
69
70
  handleSelectChange = (e, data) => {
    this.setState({ [data.name]: data.value });
  }

71
72
73
74
  handleFile = (e) => {
    this.setState({ [e.target.name]: e.target.files[0] });
  };

Zamil Majdy's avatar
Zamil Majdy committed
75

Zamil Majdy's avatar
Zamil Majdy committed
76
  handlePassword = (e) => {
Muhammad Ilham Peruzzi's avatar
Muhammad Ilham Peruzzi committed
77
78
79
80
81
    if (e.target.name === 'password') {
      this.passwordField = e.target;
    } else if (e.target.name === 'password-confirm') {
      this.passwordConfirmField = e.target;
    }
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
82

alif fadila safriyanto's avatar
alif fadila safriyanto committed
83
    const validatePassword = /(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}/g;
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
84
85
86

    if (!validatePassword.test(this.passwordField.value)) {
      this.passwordField.setCustomValidity('Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters');
Muhammad Ilham Peruzzi's avatar
Muhammad Ilham Peruzzi committed
87
    } else {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
88
      this.passwordField.setCustomValidity('');
Muhammad Ilham Peruzzi's avatar
Muhammad Ilham Peruzzi committed
89
    }
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
90

Zamil Majdy's avatar
Zamil Majdy committed
91
    const isExist = this.passwordField && this.passwordConfirmField;
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
92

Zamil Majdy's avatar
Zamil Majdy committed
93
94
95
96
97
98
99
100
101
    if (isExist) {
      if (this.passwordField.value !== this.passwordConfirmField.value) {
        this.passwordConfirmField.setCustomValidity("Passwords Don't Match");
      } else {
        this.passwordConfirmField.setCustomValidity('');
      }
    }
  };

102
103
104
105
  handleRecaptchaValue = (value) => {
    this.setState({ recaptchaValue: value });
  };

106
107
  handleSubmit = (e) => {
    e.preventDefault();
108
109
110
111
112
113
114
115
116
117
118
119
120
    if(this.state.recaptchaValue != null){
      this.setState({ loading: true });
      Server.submit('/register/', this.state).then((response) => {
        Storage.set('user-data', response);
        browserHistory.push('/home');
      }, (error) => error.then((r) => {
        this.setState({ loading: false });
        this.modalAlert.open('Gagal Membuat Akun', Dumper.dump(r));
      }));
    }
    else {
      this.modalAlert.open('Pastikan anda bukan robot', Dumper.dump());
    }
121
122
  };

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
123
  render() {
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
124
125
126
127
    const benefitViews = [];
    this.state.benefits.map((benefit, idx) => {
      const benefitView = (
        <li key={idx}>{benefit.text}</li>
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
128
      );
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
129
130
      benefitViews.push(benefitView);
    });
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
131
132
133
134
135
136
137
138
139
140
    return (
      <Modal trigger={<Button primary floated="right">Register</Button>} closeIcon="close">
        <Header icon="archive" content="Register for More Benefits" />
        <ul>{benefitViews}</ul>
        <Modal.Content>
          <ModalAlert ref={(modal) => { this.modalAlert = modal; }} />
          <Form onSubmit={this.handleSubmit}>
            <Header as="h2" icon textAlign="center">
              <Icon name="signup" circular />
              <Header.Content>
141
                        Register
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
              </Header.Content>
            </Header>
            <Form.Field required>
              <label htmlFor="email">Email</label>
              <Input onChange={this.handleChange} type="email" name="email" icon="user" iconPosition="left" placeholder="email" required />
            </Form.Field>
            <Form.Field required>
              <label htmlFor="password">Password</label>
              <Input
                onChange={(e) => { this.handleChange(e); this.handlePassword(e); }}
                type="password"
                id="password"
                name="password"
                icon="key"
                iconPosition="left"
                placeholder="password"
                required
              />
            </Form.Field>
            <Form.Field required>
              <label htmlFor="password-confirm">Konfirmasi Password</label>
              <Input
                onChange={(e) => { this.handleChange(e); this.handlePassword(e); }}
                type="password"
                id="password-confirm"
                name="password-confirm"
                icon="key"
                iconPosition="left"
                placeholder="password"
                required
              />
            </Form.Field>

            <Form.Field required>
              <label htmlFor="name">Nama Perusahaan</label>
              <Input onChange={this.handleChange} placeholder="Nama Perusahaan" name="name" required />
            </Form.Field>
            <Form.Field required>
              <label htmlFor="name">Kategori Perusahaan</label>
              <Dropdown
                placeholder="Kategori Perusahaan"
                fluid
                name="category"
                onChange={this.handleSelectChange}
                search
                selection
                options={this.categoryOptions}
              />
            </Form.Field>
            <Form.Field required></Form.Field>
            <Form.Field required>
              <label htmlFor="logo">Logo</label>
              <Input
                onChange={this.handleFile}
                name="logo"
                icon={{ name: 'attach', circular: true, link: true }}
                placeholder="attach logo"
                type="File"
                required
              />
            </Form.Field>
            <Form.Field required>
              <label htmlFor="description">Deskripsi</label>
              <TextArea onChange={this.handleChange} placeholder="Tell us more" name="description" autoHeight required />
            </Form.Field>
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
207
208
209
210
            <Form.Field required>
              <label htmlFor="founded">Founded</label>
              <TextArea onChange={this.handleChange} placeholder="YYYY-MM-DD" name="founded" required />
            </Form.Field>
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
211
212
213
214
215
216
217
218
219
220
221
222
            <Form.Field required>
              <label htmlFor="address">Alamat</label>
              <Input onChange={this.handleChange} placeholder="Alamat" name="address" required />
            </Form.Field>
            <Form.Field required>
              <label htmlFor="size">Jumlah karyawan (contoh: 1000)</label>
              <Input onChange={this.handleChange} placeholder="Jumlah karyawan" name="size" required />
            </Form.Field>
            <Form.Field required>
              <label htmlFor="address">Website</label>
              <Input onChange={this.handleChange} placeholder="Website perusahaan anda" name="website" required />
            </Form.Field>
223
224
225
226
            <ReCAPTCHA
              sitekey="6LfUYMYUAAAAAF_Yj073qov9JOMrYQaNIu3-UIF7"
              onChange={(value) => { this.handleRecaptchaValue(value); }}
            />
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
227
228
229
230
231
232
233
234
235
236
237
            <Modal.Actions style={{ textAlign: 'right' }}>
              <Button type="reset" color="red">
                <Icon name="remove" />
Reset
              </Button>
              <Button loading={this.state.loading} type="submit" color="blue">
                {' '}
                <Icon name="checkmark" />
Submit
              </Button>
            </Modal.Actions>
238

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
239
240
241
242
243
244
          </Form>
        </Modal.Content>
      </Modal>
    );
  }
}