@@ -12,9 +12,6 @@ import * as FileSystem from "expo-file-system";
1212import { MULTIPART_FILE_UPLOAD_SIZE } from "../../../common/constants" ;
1313import * as Sentry from "@sentry/react-native" ;
1414import { Buffer } from "buffer" ;
15- import useStore from "../../store/store" ;
16- import { AttachmentProgressStatusEnum } from "../../store/attachment-upload-state/attachment-upload-slice" ;
17-
1815// export const handleChunkUpload = async (
1916// filePath: string,
2017// uploadUrls: Record<string, string>,
@@ -54,18 +51,7 @@ import { AttachmentProgressStatusEnum } from "../../store/attachment-upload-stat
5451// return etags;
5552// };
5653
57- export const uploadAttachmentMutationFn = async (
58- payload : AddAttachmentStartAPIPayload ,
59- setProgress : ( fn : ( prev : Record < string , any > ) => Record < string , any > ) => void ,
60- state : any ,
61- ) => {
62- setProgress ( ( state ) => ( {
63- ...state ,
64- [ payload . id ] : {
65- progress : 0 ,
66- status : AttachmentProgressStatusEnum . STARTING ,
67- } ,
68- } ) ) ;
54+ export const uploadAttachmentMutationFn = async ( payload : AddAttachmentStartAPIPayload ) => {
6955 const start = await addAttachmentMultipartStart ( payload ) ;
7056 try {
7157 let etags : Record < number , string > = { } ;
@@ -78,18 +64,7 @@ export const uploadAttachmentMutationFn = async (
7864 encoding : FileSystem . EncodingType . Base64 ,
7965 } ) ;
8066 const buffer = Buffer . from ( chunk , "base64" ) ;
81- const progress = Math . round ( ( ( index + 1 ) / urls . length ) * 100 * 10 ) / 10 ;
82-
83- setProgress ( ( state ) => ( {
84- ...state ,
85- [ payload . id ] : {
86- progress : progress ,
87- status :
88- progress === 100
89- ? AttachmentProgressStatusEnum . COMPLETED
90- : AttachmentProgressStatusEnum . INPROGRESS ,
91- } ,
92- } ) ) ;
67+ // const progress = Math.round(((index + 1) / urls.length) * 100 * 10) / 10;
9368
9469 const data = await uploadS3Chunk ( url , buffer ) ;
9570
@@ -102,14 +77,6 @@ export const uploadAttachmentMutationFn = async (
10277 id : payload . id ,
10378 } ) ;
10479
105- setProgress ( ( state ) => ( {
106- ...state ,
107- [ payload . id ] : {
108- progress : 100 ,
109- status : AttachmentProgressStatusEnum . COMPLETED ,
110- } ,
111- } ) ) ;
112-
11380 return completed ;
11481 } catch ( err ) {
11582 Sentry . captureMessage ( "Upload failed, aborting!" ) ;
@@ -120,32 +87,24 @@ export const uploadAttachmentMutationFn = async (
12087 uploadId : start . uploadId ,
12188 electionRoundId : payload . electionRoundId ,
12289 } ) ;
123- setProgress ( ( state ) => ( {
124- ...state ,
125- [ payload . id ] : {
126- progress : 0 ,
127- status : AttachmentProgressStatusEnum . ABORTED ,
128- } ,
129- } ) ) ;
90+
13091 return aborted ;
13192 }
13293} ;
13394
13495export type UploadAttachmentProgress = {
13596 progress : number ;
136- status : AttachmentProgressStatusEnum ;
13797} ;
13898
13999export const useUploadAttachmentMutation = ( scopeId : string ) => {
140100 const queryClient = useQueryClient ( ) ;
141- const { progresses : state , setProgresses } = useStore ( ) ;
101+
142102 return useMutation ( {
143103 mutationKey : AttachmentsKeys . addAttachmentMutation ( ) ,
144104 scope : {
145105 id : scopeId ,
146106 } ,
147- mutationFn : ( payload : AddAttachmentStartAPIPayload ) =>
148- uploadAttachmentMutationFn ( payload , setProgresses , state ) ,
107+ mutationFn : ( payload : AddAttachmentStartAPIPayload ) => uploadAttachmentMutationFn ( payload ) ,
149108 onMutate : async ( payload : AddAttachmentStartAPIPayload ) => {
150109 const attachmentsQK = AttachmentsKeys . attachments (
151110 payload . electionRoundId ,
@@ -176,6 +135,7 @@ export const useUploadAttachmentMutation = (scopeId: string) => {
176135 return { previousData, attachmentsQK } ;
177136 } ,
178137 onError : ( err , payload , context ) => {
138+ console . log ( "onError" , err ) ;
179139 const attachmentsQK = AttachmentsKeys . attachments (
180140 payload . electionRoundId ,
181141 payload . pollingStationId ,
@@ -184,12 +144,6 @@ export const useUploadAttachmentMutation = (scopeId: string) => {
184144 queryClient . setQueryData ( attachmentsQK , context ?. previousData ) ;
185145 } ,
186146 onSettled : ( _data , _err , variables ) => {
187- setProgresses ( ( state ) => {
188- const { [ variables . id ] : toDelete , ...rest } = state ;
189- return {
190- ...rest ,
191- } ;
192- } ) ;
193147 return queryClient . invalidateQueries ( {
194148 queryKey : AttachmentsKeys . attachments (
195149 variables . electionRoundId ,
0 commit comments