Fakultas Ilmu Komputer UI

index.tsx 3.56 KB
Newer Older
1
import React, { FC, useState } from 'react';
2
import { View, StyleSheet } from 'react-native';
3
4
import { layoutStyles } from 'styles';
import { pages } from './pages';
5
6
import { WizardContainer, Toast } from 'components/core';
import { useForm } from 'hooks';
7
8
9
import { dietReportCommentInitialValues, fieldValidations } from './schema';
import { generateValidationSchema } from 'utils/form';
import { DietReportPage } from './components';
10
import { useRoute, useNavigation } from '@react-navigation/native';
11
import { createNutritionistCommentApi } from 'services/progress';
12
import {
13
14
15
  NutritionistCommentRequest,
  UserReportResponse,
} from 'services/progress/models';
16
17
18
19
20
21
22
23
24
import * as ROUTES from 'constants/routes';
import {
  dietReportTextFields,
  dietReportSelectFields,
} from 'constants/weeklyReport';

interface ParamsDietReport {
  id: number;
}
25
26

const DietReportForNutritionist: FC = () => {
27
28
  const navigation = useNavigation();
  const route = useRoute();
29
  const data = route.params as UserReportResponse;
30

31
32
33
34
35
36
37
  const [activeSlide, setActiveSlide] = useState(1);

  const {
    getTextInputProps,
    handleSubmit,
    isSubmitting,
    isFieldError,
38
    values: formValues,
39
  } = useForm({
40
41
42
    initialValues: dietReportCommentInitialValues,
    validationSchema: generateValidationSchema(fieldValidations),
    onSubmit: async (values) => {
43
      const payload: NutritionistCommentRequest = {
44
        weekly_report: data.id,
45
46
47
        ...values,
      };
      const response = await createNutritionistCommentApi(payload);
48
      if (response.success) {
49
50
51
52
53
54
55
56
57
58
59
60
61
        Toast.show({
          type: 'success',
          text1: 'Sukses membuat komen',
          text2: 'Komen anda terhadap laporan mingguan klien sudah tersimpan.',
        });
        navigation.navigate(ROUTES.clientListForNutritionist);
      } else {
        Toast.show({
          type: 'error',
          text1: 'Gagal membuat komen',
          text2: 'Komen anda terhadap laporan mingguan klien gagal tersimpan.',
        });
      }
62
63
64
    },
  });

65
  const userReport = data;
66
67

  const isCurrentPageError = (): boolean => {
68
69
70
71
72
73
74
    if (activeSlide === 1) {
      return [
        formValues.height,
        formValues.weight,
        formValues.waist_size,
      ].reduce((acc: boolean, item) => acc || item === '', false);
    }
75
76
77
78
    if (activeSlide === 3) {
      return isFieldError('average_consumption');
    }
    const fields = [
79
80
      ...dietReportTextFields[`dietReportPage${activeSlide}`],
      ...dietReportSelectFields[`dietReportPage${activeSlide}`],
81
    ];
82
83
84
    return fields.reduce(
      (acc: boolean, item) => acc || isFieldError(item.name),
      false,
85
86
87
    );
  };

88
89
  return (
    <View style={[layoutStyles, styles.reportContainer]}>
90
91
92
93
94
95
96
97
98
99
100
101
102
103
      <WizardContainer
        currentStep={activeSlide}
        setCurrentStep={setActiveSlide}
        onFinish={handleSubmit}
        isLoading={isSubmitting}
        isNextDisabled={isCurrentPageError()}
        components={pages.map((page, idx) => (
          <DietReportPage
            key={idx}
            content={page.pageContent(userReport, getTextInputProps)}
            pageName={page.pageName}
            getTextInputProps={getTextInputProps}
          />
        ))}
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
      />
    </View>
  );
};

const styles = StyleSheet.create({
  flexContainer: {
    flex: 1,
  },
  reportContainer: {
    position: 'relative',
    flex: 1,
    paddingBottom: 20,
  },
  searchContainer: {
    backgroundColor: 'white',
    borderWidth: 1,
    borderRadius: 5,
    marginBottom: 20,
    justifyContent: 'center',
    height: 58,
  },
  backgroundWhite: { backgroundColor: 'white' },
});

export default DietReportForNutritionist;