Fakultas Ilmu Komputer UI

index.tsx 11.3 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  const navigation = useNavigation();
  const global = useContext(AppContext);
  const headingSize = 0.028 * global.vh;
  const imageSize = 0.15 * global.vh;

  const [form, setField] = useFormState({
    new_name: { type: 'text' },
    new_username: { type: 'any' },
    new_password: { type: 'password' },
    confirm_new_password: { type: 'password' },
    new_email: { type: 'email' },
    new_phone_number: { type: 'phone' },
    new_district: { type: 'any' },
    new_sub_district: { type: 'any' },
  });

  const [serverHasError, setServerHasError] = useState(false);
  const [messageErrors, setMessageErrors] = useState({
    new_name: '',
    new_username: '',
    new_password: '',
    confirm_new_password: '',
    new_email: '',
    new_phone_number: '',
    new_district: '',
    new_sub_district: '',
  })

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

  const initFieldValue = () => {
    setField('new_name', global.user.name);
    setField('new_username', global.user.username);
    setField('new_email', global.user.email);
    setField('new_phone_number', global.user.phone_number);
    setField('new_district', global.user.district);
    setField('new_sub_district', global.user.sub_district);
  }
  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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
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
    <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
              id="new_name"
              name="Nama"
              placeholder="Nama Lengkap Anda"
              isRequired={true}
              information={messageErrors.new_name}
              value={form.fields.new_name.value}
              updateValue={val => setField('new_name', val)}
            />
          </InputField>

          <InputField>
            <Field
              id="new_username"
              name="Username"
              placeholder="Username Anda"
              isRequired={true}
              information={messageErrors.new_username}
              value={form.fields.new_username.value}
              updateValue={val => setField('new_username', val)}
            />
          </InputField>

          <InputField>
            <Field
              id="new_password"
              name="Password"
              placeholder="Password Anda"
              isRequired={true}
              shouldSecure={true}
              information={messageErrors.new_password}
              value={form.fields.new_password.value}
              updateValue={val => setField('new_password', val)}
            />
          </InputField>

          <InputField>
            <Field
              id="confirm_new_password"
              name="Konfirmasi Password"
              placeholder="Konfirmasi Password Anda"
              isRequired={true}
              shouldSecure={true}
              information={messageErrors.confirm_new_password}
              value={form.fields.confirm_new_password.value}
              updateValue={val => setField('confirm_new_password', val)}
            />
          </InputField>

          <InputField>
            <Field
              id="new_email"
              name="Email"
              placeholder="Email Anda"
              isRequired={true}
              information={messageErrors.new_email}
              value={form.fields.new_email.value}
              updateValue={val => setField('new_email', val)}
            />
          </InputField>

          <InputField>
            <Field
              id="new_phone_number"
              name="Nomor Handphone"
              placeholder="No HP Anda"
              isRequired={true}
              information={messageErrors.new_phone_number}
              value={form.fields.new_phone_number.value}
              updateValue={val => setField('new_phone_number', val)}
            />
          </InputField>

          <InputField>
            <Field
              id="new_district"
              type={3}
              name="Kecamatan"
              placeholder="Pilih Kecamatan"
              isRequired={true}
              information={messageErrors.new_district}
              value={form.fields.new_district.value || KECAMATAN_VALUES[0].value}
              updateValue={val => setField('new_district', val)}
              values={KECAMATAN_VALUES}
            />
          </InputField>

          <InputField>
            <Field
              id="new_sub_district"
              type={3}
              name="Kelurahan"
              placeholder="Pilih Kelurahan"
              isRequired={true}
              information={messageErrors.new_sub_district}
              value={form.fields.new_sub_district.value || KELURAHAN_VALUES[KECAMATAN_VALUES[0].value][0].value}
              updateValue={val => setField('new_sub_district', val)}
              values={(form.fields.new_district.value) ? KELURAHAN_VALUES[form.fields.new_district.value] : KELURAHAN_VALUES[KECAMATAN_VALUES[0].value]}
            />
          </InputField>
          <Button
            id="submit"
            type={1}
            onPress={async () => {
              setIsLoading(true)

              const response = await global.services.main.editProfile({
                new_name: form.fields.new_name.value,
                new_username: form.fields.new_username.value.replace(/\s+$/, ''),
                new_password: form.fields.new_password.value,
                confirm_new_password: form.fields.confirm_new_password.value,
                new_email: form.fields.new_email.value,
                new_phone_number: form.fields.new_phone_number.value,
                new_district: form.fields.new_district.value,
                new_sub_district: form.fields.new_sub_district.value
              })

              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 = {
                  new_name: response.data.new_name || [],
                  new_username: response.data.new_username || [],
                  new_password: response.data.new_password || [],
                  confirm_new_password: response.data.confirm_new_password || [],
                  new_email: response.data.new_email || [],
                  new_phone_number: response.data.new_phone_number || [],
                  new_district: response.data.new_district || [],
                  new_sub_district: response.data.new_sub_district || [],
                }

                if (response.data.non_field_errors) {
                  switch (response.data.non_field_errors[0]) {
                    case "Inconsistent password and confirmation":
                      errors.confirm_new_password = [
                        ...errors.confirm_new_password,
                        "Password dan konfirmasinya tidak konsisten"
                      ]
                      break;
                    case "Inconsistent district and sub district value":
                      errors.new_district = [
                        ...errors.new_district,
                        "Kecamatan dan kelurahan tidak konsisten"
                      ]
                      break;
                    default:
                      break;
                  }
                }

                setMessageErrors({
                  new_name: translateError(errors.new_name[0] || ""),
                  new_username: translateError(errors.new_username[0] || "") + " Username hanya boleh terdiri dari karakter alfanumerik atau salah satu dari {@, +, ., _, -}",
                  new_password: translateError(errors.new_password[0] || ""),
                  confirm_new_password: translateError(errors.confirm_new_password[0] || ""),
                  new_email: translateError(errors.new_email[0] || ""),
                  new_phone_number: translateError(errors.new_phone_number[0] || ""),
                  new_district: translateError(errors.new_district[0] || ""),
                  new_sub_district: translateError(errors.new_sub_district[0] || ""),
                })
              } 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;