1
1
import { getUniqueId } from "react-native-device-info" ;
2
+ import { PermissionsAndroid , YellowBox } from "react-native" ;
2
3
import axios from "axios" ;
3
- import RNFS from "react-native-fs" ;
4
- import Share from "react-native-share" ;
4
+ import RNFetchBlob from "rn-fetch-blob" ;
5
5
import moment from "moment" ;
6
6
import {
7
7
clear ,
@@ -16,6 +16,8 @@ import {
16
16
import api from "./API" ;
17
17
import { Permission , Reports } from "./types" ;
18
18
19
+ YellowBox . ignoreWarnings ( [ "Require cycle" ] ) ;
20
+
19
21
/**
20
22
* Coordena operações complexas da aplicação (principalmente aquelas relacionadas
21
23
* ao fluxo online/offline), de forma a simplificar a implementação das telas do app.
@@ -206,29 +208,43 @@ const uploadExecutions = async (): Promise<void> => {
206
208
* - `network`: erro de rede.
207
209
*/
208
210
export const downloadReport = async ( ) : Promise < void > => {
211
+ try {
212
+ await PermissionsAndroid . requestMultiple ( [
213
+ PermissionsAndroid . PERMISSIONS . WRITE_EXTERNAL_STORAGE ,
214
+ PermissionsAndroid . PERMISSIONS . READ_EXTERNAL_STORAGE ,
215
+ ] ) ;
216
+ } catch ( err ) {
217
+ console . warn ( err ) ;
218
+ }
219
+
209
220
await authenticate ( ) ;
210
221
211
222
try {
212
223
const { code, token } = await getAuth ( ) ;
213
224
const url = `${ api . defaults . baseURL } /reports/complete` ;
214
225
const filename = `Relatório dTool - ${ code } .xlsx` ;
215
- const localFile = `${ RNFS . DocumentDirectoryPath } /${ filename } ` ;
216
-
217
- const { promise } = RNFS . downloadFile ( {
218
- fromUrl : url ,
219
- toFile : localFile ,
220
- headers : { Authorization : `Bearer ${ token } ` } ,
221
- } ) ;
222
-
223
- await promise ;
224
-
225
- await Share . open ( {
226
- url : `file://${ localFile } ` ,
227
- title : `Salvar relatório do hospital` ,
228
- failOnCancel : false ,
229
- } ) ;
230
-
231
- await RNFS . unlink ( localFile ) ;
226
+ const localFile = `${ RNFetchBlob . fs . dirs . DownloadDir } /${ filename } ` ;
227
+
228
+ RNFetchBlob . config ( {
229
+ addAndroidDownloads : {
230
+ useDownloadManager : true , // required
231
+ notification : true ,
232
+ path : `file://${ localFile } ` ,
233
+ mime :
234
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
235
+ description : "Relatório DTool." ,
236
+ } ,
237
+ } )
238
+ . fetch ( "GET" , url , {
239
+ Authorization : `Bearer ${ token } ` ,
240
+ } )
241
+ . then ( ( res ) => {
242
+ // when response status code is 200
243
+ console . log ( "The file saved to " , res . path ( ) ) ;
244
+ } )
245
+ . catch ( ( _errorMessage ) => {
246
+ console . log ( _errorMessage ) ;
247
+ } ) ;
232
248
} catch ( error ) {
233
249
// invalid ID
234
250
if ( error . response ?. status === 400 ) {
0 commit comments