Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] State update in onremovefile event handler triggers file re-upload #239

Open
2 tasks done
IvoLeist opened this issue Dec 11, 2023 · 1 comment
Open
2 tasks done
Labels
bug Something isn't working

Comments

@IvoLeist
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Have you updated React FilePond, FilePond, and all plugins?

  • I have updated FilePond and its plugins

Describe the bug

Hello I am sorry for the double post, but I realized that the issue first described here: pqina/filepond#950 is probably a React specific.

Here a short summary:
I realized some odd behaviour which only happens on the live environment
https://convert-pheno.cnag.cat/, a React application built with Vite and served by nginx

The code snippet below supposed to remove a file from the uploaded files state, causes an unwanted re-triggering of the file upload.

onremovefile={(_, file) => {
   setFilesUploadFinished(false);
   const fileName = file.filename;

   // only update state if the file processing was finished (status 2 = IDLE)
   // for reference see
   // github.com/pqina/filepond-docs/blob/master/content/patterns/API/filepond-object.md#filestatus-enum
   // if below is commented out it is working on the live environment
       if (
             file.status === 2 &&
             file.getMetadata("processingAborted") !== true
       ) {
        setUploadedFiles((prev) => {
          const prevCopy = { ...prev };
          delete prevCopy[fileName];
          return prevCopy;
        });
     }
}}

Reproduction

I am not sure how to reproduce it since I guess you would need a server to show it. But if you are motivated you could fork our repo and then run the server/client with docker-compose up.

Here is a gif of the unexpected behaviour
file-deletion-reupload-loop

Environment

- Device: Any
- OS:Any
- Broser: Any
- React version: 18.2
@eywolfe
Copy link

eywolfe commented Mar 19, 2024

I ran into this same issue, though I see it locally as well. As a workaround I had my remove function immediately call load() to remove the file from Filepond state then handled the actual server deletion in onremovefile. This isn't ideal because the user won't see if the deletion fails, but at least it doesn't re-upload the file anymore!

Something like:

<FilePond
      files={files}
      server={{
        ...,
        remove: (_source, load) => {
          console.log("removing file", _source)
          // Called when a file saved in our database is deleted by the user
          // Immediately remove the file from state by calling load()
          // Rely on onremovefile to call provided update function
          load()
        },
      }}
      onremovefile={(_error, removedFile) => {
        console.log("onremovefile", removedFile.source)
        // Called after remove when a saved file is deleted
        // files state has already been updated
        if (typeof removedFile?.source === "string") {
          removeSourceFromExistingFiles(removedFile.source)
        }
      }}
/>

and removeSourceFromExistingFiles is something like this (I'm using react hook form to manage my file state):

const removeSourceFromExistingFiles = (source) => {
  const deletedId = initialFileMap[source]?.id
  setValue(
    "existingFileIds",
    watch("existingFileIds").filter((id) => id !== deletedId),
  )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants