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

Don't allow duplicates of a file #1241

Closed
Gkanawati opened this issue Sep 21, 2022 · 2 comments
Closed

Don't allow duplicates of a file #1241

Gkanawati opened this issue Sep 21, 2022 · 2 comments

Comments

@Gkanawati
Copy link

Describe the solution you'd like
There should be a way to make sure users can't upload 2 of the same files.

Describe alternatives you've considered
I've tried to do this outside of the dropzone, but there doesn't seem to a way.

Teachability, Documentation, Adoption, Migration Strategy
It could just be a prop passed in called noDuplicates

My solution its not perfect, im looking for better solutions for this problem:

const { getRootProps, getInputProps } = useDropzone({
    accept: 'image/*',
    onDrop: (acceptedFile) => {
      const newFiles = acceptedFile.map(file => {
        return Object.assign(file, {
          preview: URL.createObjectURL(file)
        })
      })
      setFieldValue('files', [
        ...files,
        ...newFiles
      ])

      files.map(file => {
        newFiles.forEach(newFile => {
          if (newFile.name == file.name) {
            setToast({
              open: true,
              severity: 'error',
              text: 'Não é possível adicionar imagens duplicadas!'
            })

            const filteredArray = files.filter((item, index) => files.indexOf(item) == index)
            setFieldValue('files', filteredArray)
          }
        })
      })
    }
  })

  const handleRemoveFile = (fileName) => {
    const newFiles = files.filter(item => item.name !== fileName)
    setFieldValue('files', newFiles)
  }
@Gkanawati Gkanawati changed the title [ENHANCEMENT] Don't allow duplicates of a file Don't allow duplicates of a file Sep 21, 2022
@phuongwd
Copy link

phuongwd commented Nov 2, 2022

@Gkanawati Just curious, why do you not use the Custom validation?

@Gkanawati
Copy link
Author

@Gkanawati Just curious, why do you not use the Custom validation?

There's what i was looking for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants