Fakultas Ilmu Komputer UI

index.tsx 2.21 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
18
19
20
  const url = API_BASE_URL + data?.client_plan_meal;
  const { download, fileName } = useDownloadFiles(
    url,
    'rencana makan',
21
    FileType.PDF,
22
  );
23

24
25
26
27
28
29
30
31
32
  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}>
33
        <EmptyDataPage text="Silahkan tunggu rekomendasi dari nutrisionis dalam 24 jam" />
34
35
36
        {children}
      </View>
    );
37
  }
38
39
40
  return (
    <ScrollView contentContainerStyle={layoutStyles}>
      <Text style={styles.header}>Saran Gizi</Text>
41
      <InfoCard content={data.nutritional_advice || '-'} />
42
      <Text style={[styles.header, styles.spacing]}>Saran Gaya Hidup</Text>
43
      <InfoCard content={data.lifestyle_advice || '-'} />
44
45
46
      <View style={styles.spacing}>
        <Text style={styles.header}>Rencana Menu dan Porsi Makan</Text>
      </View>
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
      {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" />
      )}
65
66
67
68
69
      {children}
    </ScrollView>
  );
};

70
export default ReadOnlyDietRecommendation;