Fakultas Ilmu Komputer UI

index.tsx 2.69 KB
Newer Older
1
import React, { FC, useContext } from 'react';
2
import { useForm, useSignupEffect } from 'hooks';
3
import { ScrollView } from 'react-native-gesture-handler';
Wulan Mantiri's avatar
Wulan Mantiri committed
4
import { useNavigation, useRoute } from '@react-navigation/native';
5

6
import { BigButton, Link, Toast } from 'components/core';
7
import { Section } from 'components/layout';
8
import { TextField } from 'components/form';
9
import { GoogleLoginButton } from '../components';
10
11
12
13

import { fieldValidation, initialValues, textField } from './schema';
import { generateValidationSchema } from 'utils/form';
import { UserContext } from 'provider';
14
import * as ROUTES from 'constants/routes';
15
16
17
18
19
20
21
22

import { layoutStyles } from 'styles';

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

const ManualRegistrationPage: FC = () => {
  const { signup, loginWithGoogle, isLoading } = useContext(UserContext);
23
  const routes = useRoute();
24
  const navigation = useNavigation();
25

26
27
28
29
  const loginRoute = routes.params?.from_choose_plan
    ? ROUTES.login
    : ROUTES.loginChoosePlan;

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 {
    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',
        });
      }
    },
  });

56
57
  const signupWithGoogle = () => loginWithGoogle(false);

58
  useSignupEffect();
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  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>
79
        <GoogleLoginButton onPress={signupWithGoogle} isLoading={isLoading} />
80
      </Section>
81
82
83
      <Section>
        <Link
          title="Sudah punya akun? Login disini"
84
          onPress={() => navigation.navigate(loginRoute)}
85
86
        />
      </Section>
87
88
89
90
91
    </ScrollView>
  );
};

export default ManualRegistrationPage;