Fakultas Ilmu Komputer UI

index.tsx 2.24 KB
Newer Older
1
2
3
4
5
import React, { FC, useContext } from 'react';
import { useAuthEffect, useForm } from 'hooks';
import { ScrollView } from 'react-native-gesture-handler';

import { BigButton, Toast } from 'components/core';
6
import { Section } from 'components/layout';
7
import { TextField } from 'components/form';
8
import { GoogleLoginButton } from '../components';
9
10
11
12
13
14
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

import { fieldValidation, initialValues, textField } from './schema';
import { generateValidationSchema } from 'utils/form';
import { UserContext } from 'provider';

import { layoutStyles } from 'styles';

const isPasswordField = (name: string) =>
  name === 'password1' || name === 'password2';

const ManualRegistrationPage: FC = () => {
  const { signup, loginWithGoogle, isLoading } = useContext(UserContext);

  const {
    getTextInputProps,
    handleSubmit,
    isSubmitting,
    setFieldError,
  } = useForm({
    initialValues,
    validationSchema: generateValidationSchema(fieldValidation),
    onSubmit: async (values) => {
      const response = await signup(values);

      if (!response.success) {
        setFieldError('name', response.error.name);
        setFieldError('email', response.error.email);
        setFieldError('password1', response.error.password1);
        setFieldError('password2', response.error.password2);

        Toast.show({
          type: 'error',
          text1: 'Gagal registrasi akun',
          text2: 'Terjadi kesalahan registrasi. Silakan coba lagi',
        });
      }
    },
  });

48
49
  const signupWithGoogle = () => loginWithGoogle(false);

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  useAuthEffect();

  return (
    <ScrollView contentContainerStyle={layoutStyles}>
      {textField.map((fieldProps, i) => (
        <TextField
          key={`field${i}`}
          label={fieldProps.label}
          required={fieldProps.required}
          placeholder={fieldProps.placeholder}
          {...getTextInputProps(fieldProps.name)}
          secureTextEntry={isPasswordField(fieldProps.name)}
        />
      ))}
      <BigButton
        title="daftarkan akun"
        onPress={handleSubmit}
        loading={isSubmitting}
        testID="submitButton"
      />
      <Section>
71
        <GoogleLoginButton onPress={signupWithGoogle} isLoading={isLoading} />
72
73
74
75
76
77
      </Section>
    </ScrollView>
  );
};

export default ManualRegistrationPage;