Fakultas Ilmu Komputer UI

CompanyRegisterModal.jsx 8.41 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

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

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  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
45
    { value: 'Perusahaan Telekomunikasi', text: 'Perusahaan Telekomunikasi' },
46
47
  ]

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

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

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

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

Zamil Majdy's avatar
Zamil Majdy committed
73

Zamil Majdy's avatar
Zamil Majdy committed
74
  handlePassword = (e) => {
Muhammad Ilham Peruzzi's avatar
Muhammad Ilham Peruzzi committed
75
76
77
78
79
    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
80

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

    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
85
    } else {
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
86
      this.passwordField.setCustomValidity('');
Muhammad Ilham Peruzzi's avatar
Muhammad Ilham Peruzzi committed
87
    }
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
88

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

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

100
101
  handleSubmit = (e) => {
    e.preventDefault();
102
    this.setState({ loading: true });
103
104
105
    Server.submit('/register/', this.state).then((response) => {
      Storage.set('user-data', response);
      browserHistory.push('/home');
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
106
    }, (error) => error.then((r) => {
107
      this.setState({ loading: false });
Zamil Majdy's avatar
Zamil Majdy committed
108
      this.modalAlert.open('Gagal Membuat Akun', Dumper.dump(r));
109
    }));
110
111
  };

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
112
  render() {
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
113
114
115
116
    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
117
      );
Vitosavero Avila Wibisono's avatar
Vitosavero Avila Wibisono committed
118
119
      benefitViews.push(benefitView);
    });
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
120
121
122
123
124
125
126
127
128
129
    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>
130
                        Register
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
131
132
133
134
135
136
137
138
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
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
              </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
196
197
198
199
            <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
200
201
202
203
204
205
206
207
208
209
210
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>
            <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>
223

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
224
225
226
227
228
229
          </Form>
        </Modal.Content>
      </Modal>
    );
  }
}