From 001c4285e78e89018a13cbe09f2256dbc98a5e58 Mon Sep 17 00:00:00 2001 From: doanandreas Date: Sun, 30 May 2021 16:23:15 +0700 Subject: [PATCH 1/4] [GREEN] fixed use download file hook --- android/app/src/main/AndroidManifest.xml | 1 + src/hooks/useDownloadFiles/index.ts | 57 ++++++++++-------------- src/hooks/useDownloadFiles/schema.ts | 4 ++ 3 files changed, 28 insertions(+), 34 deletions(-) create mode 100644 src/hooks/useDownloadFiles/schema.ts diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 4741a0d..1aa2c8c 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + { - const fileName = url.split('/').pop(); +const useDownloadFiles = ( + url = '', + title = '', + fileType: FileType, + fileTitle?: string, +) => { + const fileName = fileTitle ? fileTitle : url.split('/').pop(); const extension = fileName?.split('.').pop()?.toUpperCase() || '-'; const askWritePermission = async () => @@ -17,36 +22,20 @@ const useDownloadFiles = (url = '') => { return; } - const path = `${DocumentDirectoryPath}/${fileName}`; - const response = await downloadFile({ - fromUrl: url, - toFile: path, - }); - return response.promise - .then((res) => { - if (res.statusCode === 200 && res.bytesWritten > 0) { - Toast.show({ - type: 'success', - text1: `${extension} berhasil diunduh.`, - text2: `Silakan lihat file ${fileName} dalam penyimpanan HP Anda.`, - }); - return { - success: true, - data: `file://${path}`, - }; - } else { - Toast.show({ - type: 'error', - text1: `${extension} gagal diunduh.`, - text2: 'Terjadi kesalahan pada sisi kami.', - }); - return { - success: false, - error: res, - }; - } - }) - .catch(console.log); + const dirs = RNFetchBlob.fs.dirs; + RNFetchBlob.config({ + addAndroidDownloads: { + useDownloadManager: true, + notification: true, + mime: fileType, + title: 'Downloading ' + title + '...', + mediaScannable: true, + path: dirs.DownloadDir + `/${fileName}`, + }, + }) + .fetch('GET', url) + .then((res) => console.log(res.path())) + .catch((err) => console.log(err)); }; return { diff --git a/src/hooks/useDownloadFiles/schema.ts b/src/hooks/useDownloadFiles/schema.ts new file mode 100644 index 0000000..2af83b9 --- /dev/null +++ b/src/hooks/useDownloadFiles/schema.ts @@ -0,0 +1,4 @@ +export enum FileType { + PDF = 'application/pdf', + CSV = 'text/csv', +} -- GitLab From d3c7b77a136d9de8a9c2b5f51e1ee7db8186f8c9 Mon Sep 17 00:00:00 2001 From: doanandreas Date: Sun, 30 May 2021 16:23:36 +0700 Subject: [PATCH 2/4] [GREEN] integrated use download hook with download buttons --- src/scenes/admin/ClientListAdmin/index.tsx | 9 ++++++++- .../profile/ClientDietRecommendationForAdmin/index.tsx | 3 +++ src/scenes/questionnaire/ReadOnlyDietProfile/index.tsx | 7 ++++--- .../questionnaire/ReadOnlyDietRecommendation/index.tsx | 8 ++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/scenes/admin/ClientListAdmin/index.tsx b/src/scenes/admin/ClientListAdmin/index.tsx index a2642d6..219031b 100644 --- a/src/scenes/admin/ClientListAdmin/index.tsx +++ b/src/scenes/admin/ClientListAdmin/index.tsx @@ -14,11 +14,17 @@ import { Dimensions, StyleSheet, View } from 'react-native'; import { Section } from 'components/layout'; import { getAbsoluteUrl } from 'utils/format'; import { UserRole } from 'services/auth/models'; +import { FileType } from 'hooks/useDownloadFiles/schema'; const ClientListAdmin: FC = () => { const navigation = useNavigation(); const { isLoading, data: clients = [] } = useApi(retrieveClientListApi); - const { download } = useDownloadFiles(getAbsoluteUrl('exportcsv')); + const { download } = useDownloadFiles( + getAbsoluteUrl('/exportcsv'), + 'client list CSV', + FileType.CSV, + 'export.csv', + ); const [searchedText, setSearchedText] = useState(''); const updateSearch = (search: string) => { @@ -53,6 +59,7 @@ const ClientListAdmin: FC = () => { onPressClientProfile={() => { navigation.navigate(ROUTES.clientProfileAdmin, { id: client.diet_questionnaire_id, + id_recommendation: client.diet_recommendation_id, role: UserRole.ADMIN, }); }} diff --git a/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx b/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx index 05c80fd..8fe1a2b 100644 --- a/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx +++ b/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx @@ -18,6 +18,9 @@ const ClientDietRecommendationForAdmin: FC = () => { if (isLoading) { return ; } + + console.log(data); + return ; }; diff --git a/src/scenes/questionnaire/ReadOnlyDietProfile/index.tsx b/src/scenes/questionnaire/ReadOnlyDietProfile/index.tsx index 4c696ee..4f78bfa 100644 --- a/src/scenes/questionnaire/ReadOnlyDietProfile/index.tsx +++ b/src/scenes/questionnaire/ReadOnlyDietProfile/index.tsx @@ -21,6 +21,7 @@ import { UserRole } from 'services/auth/models'; interface ParamsDietProfile { id: number; + id_recommendation: number; role: string; } @@ -28,7 +29,7 @@ const ReadOnlyDietProfile: FC = () => { const navigation = useNavigation(); const [activeSlide, setActiveSlide] = useState(0); const route = useRoute(); - const { id, role } = route.params as ParamsDietProfile; + const { id, id_recommendation, role } = route.params as ParamsDietProfile; const { isLoading, data } = useApi(() => retrieveDietQuestionnaireByIdApi(id), ); @@ -64,7 +65,7 @@ const ReadOnlyDietProfile: FC = () => { onPress={() => navigation.navigate(ROUTES.profileDietRecommendation, { name: dataDQR.user.name, - id: id, + id: id_recommendation, }) } /> @@ -74,7 +75,7 @@ const ReadOnlyDietProfile: FC = () => { onPress={() => navigation.navigate(ROUTES.clientDietRecommendation, { name: dataDQR.user.name, - id: id, + id: id_recommendation, }) } /> diff --git a/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx b/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx index 3909f87..fe34aea 100644 --- a/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx +++ b/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx @@ -6,17 +6,21 @@ import { WebView } from 'react-native-webview'; import { InfoCard, Loader, EmptyDataPage } from 'components/core'; import { layoutStyles } from 'styles'; import { useDownloadFiles } from 'hooks'; -import { getAbsoluteUrl } from 'utils/format'; import { styles } from './styles'; import { Props } from './types'; import { DietRecommendationResponse } from 'services/dietRecommendation/models'; +import { FileType } from 'hooks/useDownloadFiles/schema'; const ReadOnlyDietRecommendation: FC = ({ children, data }) => { const { download, pdfViewUrl, fileName } = useDownloadFiles( - getAbsoluteUrl(data?.client_plan_meal), + data?.client_plan_meal, + 'diet recommendation', + FileType.PDF, ); + console.log(data); + const hasValues = (obj: DietRecommendationResponse) => [obj.client_plan_meal, obj.nutritional_advice, obj.lifestyle_advice].reduce( (acc, item) => acc || Boolean(item), -- GitLab From bbd938903b8358badf554e49ebd908fc698772bd Mon Sep 17 00:00:00 2001 From: doanandreas Date: Sun, 30 May 2021 16:33:23 +0700 Subject: [PATCH 3/4] [GREEN] delete unnecesarry console.log --- src/hooks/useDownloadFiles/index.ts | 2 +- src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx | 2 -- src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/hooks/useDownloadFiles/index.ts b/src/hooks/useDownloadFiles/index.ts index fe7503b..0b887c2 100644 --- a/src/hooks/useDownloadFiles/index.ts +++ b/src/hooks/useDownloadFiles/index.ts @@ -34,7 +34,7 @@ const useDownloadFiles = ( }, }) .fetch('GET', url) - .then((res) => console.log(res.path())) + .then((res) => res.path()) .catch((err) => console.log(err)); }; diff --git a/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx b/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx index 8fe1a2b..f339ea9 100644 --- a/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx +++ b/src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx @@ -19,8 +19,6 @@ const ClientDietRecommendationForAdmin: FC = () => { return ; } - console.log(data); - return ; }; diff --git a/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx b/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx index fe34aea..2c70c5d 100644 --- a/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx +++ b/src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx @@ -19,8 +19,6 @@ const ReadOnlyDietRecommendation: FC = ({ children, data }) => { FileType.PDF, ); - console.log(data); - const hasValues = (obj: DietRecommendationResponse) => [obj.client_plan_meal, obj.nutritional_advice, obj.lifestyle_advice].reduce( (acc, item) => acc || Boolean(item), -- GitLab From e9379f0e7b11b29d8d3a4bdbe7f5410e38abbeaa Mon Sep 17 00:00:00 2001 From: doanandreas Date: Sun, 30 May 2021 16:33:37 +0700 Subject: [PATCH 4/4] [GREEN] added mocks for rn fetch blob --- src/__mocks__/rn-fetch-blob.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/__mocks__/rn-fetch-blob.ts diff --git a/src/__mocks__/rn-fetch-blob.ts b/src/__mocks__/rn-fetch-blob.ts new file mode 100644 index 0000000..dfaae72 --- /dev/null +++ b/src/__mocks__/rn-fetch-blob.ts @@ -0,0 +1,17 @@ +jest.mock('rn-fetch-blob', () => { + return { + DocumentDir: () => {}, + fetch: () => {}, + base64: () => {}, + android: () => {}, + ios: () => {}, + config: () => {}, + session: () => {}, + fs: () => {}, + wrap: () => {}, + polyfill: () => {}, + JSONStream: () => {}, + }; +}); + +export {}; -- GitLab