KarimMokhtar / react-drag-drop-files

Light and simple Reactjs drag and drop files library to use with very flexible options to change, so you put whatever the design you want for your drop-area. Users can drag and drop or even select the file anywhere in the window.
MIT License
248 stars 91 forks source link

Show "Uploading" message, and setUploaded manually #125

Open bangbaew opened 1 year ago

bangbaew commented 1 year ago

My app does upload file to the backend after dragging the files to to component, I want it to show an "uploading" message first, and I want to be able to use setUploaded in my app after finish uploading to the backend.

my app

const handleUpload = (files: FileList) => {

  setUploaded(false) //<-- Show "Uploading" message

  for (let i = 0; i < files.length; i++) {
    const f = files.item(i)!
    const formData = new FormData();
    formData.append("file", f);

    axios.post(getConfigClient() + '/document/upload', formData, {
      headers: {
        "Content-Type": "multipart/form-data",
      }
    })
      .then(async _ => {
        const newDocList: AxiosResponse<Document[]> = await axios.get(getConfigClient() + `/document`, { headers: { 'Cache-Control': 'no-cache', ...getAuthHeader() }, params: { parentId: currentFolderId } })
        setUploaded(true) // <-- Show "Uploaded" message when all files completed uploading

        setDocList(newDocList)
        setFile(f);
      })
      .catch(e => {
        console.log(e)
        alert(e.response?.data)
      })
  }
};