Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ppl-fasilkom-ui
2021
Kelas D
PT Gizi Sehat - Dietela
Dietela Mobile
Commits
939e2a8c
Commit
939e2a8c
authored
May 30, 2021
by
Doan Andreas Nathanael
Browse files
Fix useDownloadFiles hook and integrate with download button
parent
ddd767d0
Changes
8
Hide whitespace changes
Inline
Side-by-side
android/app/src/main/AndroidManifest.xml
View file @
939e2a8c
...
...
@@ -5,6 +5,7 @@
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.DOWNLOAD_WITH_NOTIFICATION"
/>
<application
android:name=
".MainApplication"
...
...
src/__mocks__/rn-fetch-blob.ts
0 → 100644
View file @
939e2a8c
jest
.
mock
(
'
rn-fetch-blob
'
,
()
=>
{
return
{
DocumentDir
:
()
=>
{},
fetch
:
()
=>
{},
base64
:
()
=>
{},
android
:
()
=>
{},
ios
:
()
=>
{},
config
:
()
=>
{},
session
:
()
=>
{},
fs
:
()
=>
{},
wrap
:
()
=>
{},
polyfill
:
()
=>
{},
JSONStream
:
()
=>
{},
};
});
export
{};
src/components/core/ClientList/index.tsx
View file @
939e2a8c
...
...
@@ -10,6 +10,7 @@ import { getAbsoluteUrl } from 'utils/format';
import
{
BigButton
,
Loader
,
EmptyDataPage
}
from
'
components/core
'
;
import
{
Section
}
from
'
components/layout
'
;
import
{
UserRole
}
from
'
services/auth/models
'
;
import
{
FileType
}
from
'
hooks/useDownloadFiles/schema
'
;
interface
Props
{
role
:
string
;
...
...
@@ -26,7 +27,12 @@ const ClientList: FC<Props> = ({
})
=>
{
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
)
=>
{
...
...
@@ -64,6 +70,7 @@ const ClientList: FC<Props> = ({
onPressClientProfile
=
{
()
=>
{
navigation
.
navigate
(
clientProfileRoute
,
{
id
:
client
.
diet_questionnaire_id
,
id_recommendation
:
client
.
diet_recommendation_id
,
role
:
role
,
});
}
}
...
...
src/hooks/useDownloadFiles/index.ts
View file @
939e2a8c
import
{
downloadFile
,
DocumentDirectoryPath
}
from
'
react-native-fs
'
;
import
{
Toast
}
from
'
components/core
'
;
import
{
PermissionsAndroid
}
from
'
react-native
'
;
import
RNFetchBlob
from
'
rn-fetch-blob
'
;
import
{
FileType
}
from
'
./schema
'
;
const
useDownloadFiles
=
(
url
=
''
)
=>
{
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
)
=>
res
.
path
())
.
catch
((
err
)
=>
console
.
log
(
err
));
};
return
{
...
...
src/hooks/useDownloadFiles/schema.ts
0 → 100644
View file @
939e2a8c
export
enum
FileType
{
PDF
=
'
application/pdf
'
,
CSV
=
'
text/csv
'
,
}
src/scenes/profile/ClientDietRecommendationForAdmin/index.tsx
View file @
939e2a8c
...
...
@@ -18,6 +18,7 @@ const ClientDietRecommendationForAdmin: FC = () => {
if
(
isLoading
)
{
return
<
Loader
/>;
}
return
<
ReadOnlyDietRecommendation
data
=
{
data
}
/>;
};
...
...
src/scenes/questionnaire/ReadOnlyDietProfile/index.tsx
View file @
939e2a8c
...
...
@@ -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
,
})
}
/>
...
...
src/scenes/questionnaire/ReadOnlyDietRecommendation/index.tsx
View file @
939e2a8c
...
...
@@ -6,15 +6,17 @@ 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
<
Props
>
=
({
children
,
data
})
=>
{
const
{
download
,
pdfViewUrl
,
fileName
}
=
useDownloadFiles
(
getAbsoluteUrl
(
data
?.
client_plan_meal
),
data
?.
client_plan_meal
,
'
diet recommendation
'
,
FileType
.
PDF
,
);
const
hasValues
=
(
obj
:
DietRecommendationResponse
)
=>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment