Fakultas Ilmu Komputer UI

index.tsx 2.18 KB
Newer Older
1
2
import React, { FC } from 'react';
import { ScrollView, View } from 'react-native';
3
import { Text, Button } from 'react-native-elements';
4
import Pdf from 'react-native-pdf';
5

6
import { InfoCard, EmptyDataPage } from 'components/core';
7
import { layoutStyles } from 'styles';
8
import { useDownloadFiles } from 'hooks';
9
10
11
import { DietRecommendationResponse } from 'services/dietRecommendation/models';
import { FileType } from 'hooks/useDownloadFiles/schema';
import { API_BASE_URL } from 'env';
12
13

import { styles } from './styles';
14
import { Props } from './types';
15

16
const ReadOnlyDietRecommendation: FC<Props> = ({ children, data }) => {
17
  const url = API_BASE_URL + data?.client_plan_meal;
18
  const { download, fileName } = useDownloadFiles(url, '', FileType.PDF);
19

20
21
22
23
24
25
26
27
28
  const hasValues = (obj: DietRecommendationResponse) =>
    [obj.client_plan_meal, obj.nutritional_advice, obj.lifestyle_advice].reduce(
      (acc, item) => acc || Boolean(item),
      false,
    );

  if (!data || !hasValues(data)) {
    return (
      <View style={styles.noRecom}>
29
        <EmptyDataPage text="Silahkan tunggu rekomendasi dari nutrisionis dalam 24 jam" />
30
31
32
        {children}
      </View>
    );
33
  }
34
35
36
  return (
    <ScrollView contentContainerStyle={layoutStyles}>
      <Text style={styles.header}>Saran Gizi</Text>
37
      <InfoCard content={data.nutritional_advice || '-'} />
38
      <Text style={[styles.header, styles.spacing]}>Saran Gaya Hidup</Text>
39
      <InfoCard content={data.lifestyle_advice || '-'} />
40
41
42
      <View style={styles.spacing}>
        <Text style={styles.header}>Rencana Menu dan Porsi Makan</Text>
      </View>
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      {data?.client_plan_meal ? (
        <View>
          <Pdf source={{ uri: url, cache: true }} style={styles.pdfView} />
          <Button
            title={`Unduh ${fileName}`}
            type="outline"
            icon={{
              name: 'file-download',
            }}
            iconRight
            buttonStyle={styles.buttonStyle}
            titleStyle={styles.titleStyle}
            onPress={download}
          />
        </View>
      ) : (
        <InfoCard content="Belum ada nih, ditunggu yaa" />
      )}
61
62
63
64
65
      {children}
    </ScrollView>
  );
};

66
export default ReadOnlyDietRecommendation;