Fakultas Ilmu Komputer UI

index.tsx 2.7 KB
Newer Older
1
import React, { FC, useContext } from 'react';
2
3
4
import { ScrollView, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';

5
import { BigButton, Toast } from 'components/core';
6
7
8
9
10
11
import * as ROUTES from 'constants/routes';
import { useForm } from 'hooks';
import { layoutStyles } from 'styles';

import { generateValidationSchema } from 'utils/form';
import { Props } from './types';
12
import { updateDietQuestionnaireApi } from 'services/dietQuestionnaire';
13
import { UserContext } from 'provider';
14
15
16
17
18

const QuestionnaireWrapper: FC<Props> = ({
  Questionnaire,
  initialValues,
  fieldValidations = [],
19
20
  convertPayload,
  route,
21
22
}) => {
  const navigation = useNavigation();
23
  const dietQuestionnaireResponse = route.params;
24
  const { user, setUser } = useContext(UserContext);
25
26
27
28
29
30
31
32
33

  const getInitialValues = () => {
    let defaultValues: typeof initialValues = {};
    Object.entries(initialValues).forEach(([k, value]) => {
      const key = k as keyof typeof initialValues;
      defaultValues[key] = dietQuestionnaireResponse[key] || value;
    });
    return defaultValues;
  };
34
35
36
37
38
39

  const {
    getTextInputProps,
    getFormFieldProps,
    handleSubmit,
    isSubmitting,
40
    values: formValues,
41
  } = useForm({
42
    initialValues: getInitialValues(),
43
44
    validationSchema: generateValidationSchema(fieldValidations),
    onSubmit: async (values) => {
45
46
47
48
49
50
      const payload = convertPayload ? convertPayload(values) : values;
      const response = await updateDietQuestionnaireApi(
        dietQuestionnaireResponse.id,
        payload,
      );
      if (response.success && response.data) {
51
52
53
54
55
56
        if (response.data.finished_steps.length === 6) {
          setUser({
            ...user,
            is_finished_onboarding: true,
          });
        }
57
58
59
60
61
62
63
64
65
66
67
        navigation.reset({
          index: 0,
          routes: [{ name: ROUTES.extendedQuestionnaire }],
        });
      } else {
        Toast.show({
          type: 'error',
          text1: 'Gagal menyimpan data',
          text2: 'Terjadi kesalahan pada sisi kami. Silakan coba lagi',
        });
      }
68
69
70
    },
  });

71
72
73
74
75
76
  const isValid =
    formValues.step === 1
      ? formValues.agree_to_all_statements_consent === 1 &&
        formValues.personal_data_consent === 1
      : true;

77
78
79
80
81
82
  return (
    <ScrollView contentContainerStyle={layoutStyles}>
      <Questionnaire
        getTextInputProps={getTextInputProps}
        getFormFieldProps={getFormFieldProps}
      />
83
84
85
86
87
88
      <BigButton
        title="simpan"
        onPress={handleSubmit}
        loading={isSubmitting}
        disabled={!isValid}
      />
89
90
91
92
93
94
95
96
97
98
99
    </ScrollView>
  );
};

export const styles = StyleSheet.create({
  spacing: {
    marginBottom: 24,
  },
});

export default QuestionnaireWrapper;