Fakultas Ilmu Komputer UI

index.tsx 10.8 KB
Newer Older
jahnsmichael's avatar
jahnsmichael committed
1
2
3
4
5
6
7
8
9
10
11
12
import React, { useState, useContext, useEffect } from 'react';
import styled from 'styled-components/native';

import { ActivityIndicator, ScrollView, View, Modal, Image } from 'react-native';

import { Header, Field, Button, Text } from '../../components';
import error_img from '../OfficerSignupForm/img/folder.png';
import { useFormState } from 'helpers';
import { KECAMATAN_VALUES, KELURAHAN_VALUES } from '../OfficerSignupForm/constants';
import { useNavigation } from '@react-navigation/native';
import { AppContext } from 'contexts';
import translateError from '../OfficerSignupForm/utilities'
jahnsmichael's avatar
jahnsmichael committed
13
14

const EditProfileForm = () => {
jahnsmichael's avatar
jahnsmichael committed
15
16
17
18
19
20
  const navigation = useNavigation();
  const global = useContext(AppContext);
  const headingSize = 0.028 * global.vh;
  const imageSize = 0.15 * global.vh;

  const [form, setField] = useFormState({
21
22
23
24
25
26
27
28
    name: { type: 'text' },
    username: { type: 'any' },
    password: { type: 'password' },
    confirm_password: { type: 'password' },
    email: { type: 'email' },
    phone_number: { type: 'phone' },
    district: { type: 'any' },
    sub_district: { type: 'any' },
jahnsmichael's avatar
jahnsmichael committed
29
30
31
32
  });

  const [serverHasError, setServerHasError] = useState(false);
  const [messageErrors, setMessageErrors] = useState({
33
34
35
36
37
38
39
40
    name: '',
    username: '',
    password: '',
    confirm_password: '',
    email: '',
    phone_number: '',
    district: '',
    sub_district: '',
jahnsmichael's avatar
jahnsmichael committed
41
42
43
44
45
46
  })

  const [modalVisible, setModalVisible] = useState(false);
  const [isLoading, setIsLoading] = useState(false);

  const initFieldValue = () => {
47
48
49
50
51
52
    setField('name', global.user.name);
    setField('username', global.user.username);
    setField('email', global.user.email);
    setField('phone_number', global.user.phone_number);
    setField('district', global.user.district);
    setField('sub_district', global.user.sub_district);
jahnsmichael's avatar
jahnsmichael committed
53
54
55
  }
  useEffect(initFieldValue, []);

jahnsmichael's avatar
jahnsmichael committed
56
  return (
jahnsmichael's avatar
jahnsmichael committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    <View>
      <ScrollView>
        <Header headerText="Edit Profile" />
        <ContainerContent>
          <Modal animationType="fade" transparent={true} visible={isLoading}>
            <ModalContainer>
              <ActivityIndicator
                animating={isLoading}
                size="large"
                color="#fff"
              />
            </ModalContainer>
          </Modal>
          <Modal
            id="errorModal"
            animationType="fade"
            transparent={true}
            visible={modalVisible}
            onDismiss={() => setServerHasError(false)}>
            <ModalContainer>
              <ModalInner>
                <Image
                  style={{ width: imageSize, height: imageSize }}
                  source={error_img}
                />
                <HeadingField>
                  {serverHasError ? (
                    <>
                      <Text isBold={true} fontSize={headingSize}>
                        Unknown Server Error
                      </Text>
                    </>
                  ) : (
                    <>
                      <Text isBold={true} fontSize={headingSize}>
                        Terjadi kesalahan, periksa kembali data anda.
                      </Text>
                    </>
                  )}
                </HeadingField>
                <Button id="modalBack" type={2} onPress={() => setModalVisible(false)}>
                  Kembali
                </Button>
              </ModalInner>
            </ModalContainer>
          </Modal>
          <InputField>
            <Field
105
              id="name"
jahnsmichael's avatar
jahnsmichael committed
106
107
108
              name="Nama"
              placeholder="Nama Lengkap Anda"
              isRequired={true}
109
110
111
              information={messageErrors.name}
              value={form.fields.name.value}
              updateValue={val => setField('name', val)}
jahnsmichael's avatar
jahnsmichael committed
112
113
114
115
116
            />
          </InputField>

          <InputField>
            <Field
117
              id="username"
jahnsmichael's avatar
jahnsmichael committed
118
119
120
              name="Username"
              placeholder="Username Anda"
              isRequired={true}
121
122
123
              information={messageErrors.username}
              value={form.fields.username.value}
              updateValue={val => setField('username', val)}
jahnsmichael's avatar
jahnsmichael committed
124
125
126
127
128
            />
          </InputField>

          <InputField>
            <Field
129
              id="password"
jahnsmichael's avatar
jahnsmichael committed
130
131
132
133
              name="Password"
              placeholder="Password Anda"
              isRequired={true}
              shouldSecure={true}
134
135
136
              information={messageErrors.password}
              value={form.fields.password.value}
              updateValue={val => setField('password', val)}
jahnsmichael's avatar
jahnsmichael committed
137
138
139
140
141
            />
          </InputField>

          <InputField>
            <Field
142
              id="confirm_password"
jahnsmichael's avatar
jahnsmichael committed
143
144
145
146
              name="Konfirmasi Password"
              placeholder="Konfirmasi Password Anda"
              isRequired={true}
              shouldSecure={true}
147
148
149
              information={messageErrors.confirm_password}
              value={form.fields.confirm_password.value}
              updateValue={val => setField('confirm_password', val)}
jahnsmichael's avatar
jahnsmichael committed
150
151
152
153
154
            />
          </InputField>

          <InputField>
            <Field
155
              id="email"
jahnsmichael's avatar
jahnsmichael committed
156
157
158
              name="Email"
              placeholder="Email Anda"
              isRequired={true}
159
160
161
              information={messageErrors.email}
              value={form.fields.email.value}
              updateValue={val => setField('email', val)}
jahnsmichael's avatar
jahnsmichael committed
162
163
164
165
166
            />
          </InputField>

          <InputField>
            <Field
167
              id="phone_number"
jahnsmichael's avatar
jahnsmichael committed
168
169
170
              name="Nomor Handphone"
              placeholder="No HP Anda"
              isRequired={true}
171
172
173
              information={messageErrors.phone_number}
              value={form.fields.phone_number.value}
              updateValue={val => setField('phone_number', val)}
jahnsmichael's avatar
jahnsmichael committed
174
175
176
177
178
            />
          </InputField>

          <InputField>
            <Field
179
              id="district"
jahnsmichael's avatar
jahnsmichael committed
180
181
182
183
              type={3}
              name="Kecamatan"
              placeholder="Pilih Kecamatan"
              isRequired={true}
184
185
186
              information={messageErrors.district}
              value={form.fields.district.value || KECAMATAN_VALUES[0].value}
              updateValue={val => setField('district', val)}
jahnsmichael's avatar
jahnsmichael committed
187
188
189
190
191
192
              values={KECAMATAN_VALUES}
            />
          </InputField>

          <InputField>
            <Field
193
              id="sub_district"
jahnsmichael's avatar
jahnsmichael committed
194
195
196
197
              type={3}
              name="Kelurahan"
              placeholder="Pilih Kelurahan"
              isRequired={true}
198
199
200
201
              information={messageErrors.sub_district}
              value={form.fields.sub_district.value || KELURAHAN_VALUES[KECAMATAN_VALUES[0].value][0].value}
              updateValue={val => setField('sub_district', val)}
              values={(form.fields.district.value) ? KELURAHAN_VALUES[form.fields.district.value] : KELURAHAN_VALUES[KECAMATAN_VALUES[0].value]}
jahnsmichael's avatar
jahnsmichael committed
202
203
204
205
206
207
208
209
210
            />
          </InputField>
          <Button
            id="submit"
            type={1}
            onPress={async () => {
              setIsLoading(true)

              const response = await global.services.main.editProfile({
211
212
213
214
215
216
217
218
                name: form.fields.name.value,
                username: form.fields.username.value.replace(/\s+$/, ''),
                password: form.fields.password.value,
                confirm_password: form.fields.confirm_password.value,
                email: form.fields.email.value,
                phone_number: form.fields.phone_number.value,
                district: form.fields.district.value,
                sub_district: form.fields.sub_district.value
jahnsmichael's avatar
jahnsmichael committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
              })

              if (response.status === 200) {
                setIsLoading(false);
                const meRes = await global.services.main.me()
                if (meRes.status === 200) {
                  global.setUser(meRes.data)
                } else {
                  global.setToken('')
                }
                navigation.reset({
                  index: 0,
                  routes: [{ name: "profile" }],
                });
              } else if (response.status === 400) {
                const errors = {
235
236
237
238
239
240
241
242
                  name: response.data.name || [],
                  username: response.data.username || [],
                  password: response.data.password || [],
                  confirm_password: response.data.confirm_password || [],
                  email: response.data.email || [],
                  phone_number: response.data.phone_number || [],
                  district: response.data.district || [],
                  sub_district: response.data.sub_district || [],
jahnsmichael's avatar
jahnsmichael committed
243
244
245
246
247
                }

                if (response.data.non_field_errors) {
                  switch (response.data.non_field_errors[0]) {
                    case "Inconsistent password and confirmation":
248
249
                      errors.confirm_password = [
                        ...errors.confirm_password,
jahnsmichael's avatar
jahnsmichael committed
250
251
252
253
                        "Password dan konfirmasinya tidak konsisten"
                      ]
                      break;
                    case "Inconsistent district and sub district value":
254
255
                      errors.district = [
                        ...errors.district,
jahnsmichael's avatar
jahnsmichael committed
256
257
258
259
260
261
262
263
264
                        "Kecamatan dan kelurahan tidak konsisten"
                      ]
                      break;
                    default:
                      break;
                  }
                }

                setMessageErrors({
265
266
267
268
269
270
271
272
                  name: translateError(errors.name[0] || ""),
                  username: translateError(errors.username[0] || ""),
                  password: translateError(errors.password[0] || ""),
                  confirm_password: translateError(errors.confirm_password[0] || ""),
                  email: translateError(errors.email[0] || ""),
                  phone_number: translateError(errors.phone_number[0] || ""),
                  district: translateError(errors.district[0] || ""),
                  sub_district: translateError(errors.sub_district[0] || ""),
jahnsmichael's avatar
jahnsmichael committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
                })
              } else if (response.status === 500) {
                if (response.data.includes("IntegrityError")) {
                  setServerHasError(false);
                } else {
                  setServerHasError(true);
                }
                setModalVisible(true);
              }

              setIsLoading(false);
            }}>
            Edit Profile
          </Button>
        </ContainerContent>
      </ScrollView>
    </View>
  );
};

const ContainerContent = styled.View`
  padding: 5%;
  background-color: white;
`;

const InputField = styled.View`
  margin-bottom: 5%;
`;

const ModalContainer = styled.View`
  flex: 1;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #00000080;
`;

const HeadingField = styled.View`
  margin: 10% 0;
  align-items: center;
`;
const ModalInner = styled.View`
  align-items: center;
  background-color: #fff;
  padding: 15%;
  height: ${props => props.theme.vh * 0.55};
`;
jahnsmichael's avatar
jahnsmichael committed
320

jahnsmichael's avatar
jahnsmichael committed
321
export default EditProfileForm;