Add cancel fetch file

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-03-31 18:54:56 +02:00
parent 2f6cec21e3
commit 43ab0edeb8
2 changed files with 15 additions and 3 deletions

View File

@ -78,10 +78,13 @@ const ModalStepper = ({
try {
await Promise.all(
files.map(file => {
const { source } = file;
return axios
.get(file.fileURL, {
headers: new Headers({ Origin: window.location.origin, mode: 'cors' }),
responseType: 'blob',
cancelToken: source.token,
})
.then(({ data }) => {
const createdFile = new File([data], file.fileURL, {
@ -123,9 +126,14 @@ const ModalStepper = ({
const handleCancelFileToUpload = fileOriginalIndex => {
const fileToCancel = filesToUpload.find(file => file.originalIndex === fileOriginalIndex);
const { source } = fileToCancel;
// Cancel upload
fileToCancel.abortController.abort();
if (source) {
source.cancel('Operation canceled by the user.');
} else {
// Cancel upload
fileToCancel.abortController.abort();
}
dispatch({
type: 'REMOVE_FILE_TO_UPLOAD',

View File

@ -1,4 +1,4 @@
// import axios from 'axios';
import axios from 'axios';
import { isEmpty } from 'lodash';
const getTempsIds = alreadyUploadedFiles => {
@ -18,11 +18,15 @@ const createNewFilesToDownloadArray = (filesURLArray, alreadyUploadedFiles) => {
return acc;
}
const CancelToken = axios.CancelToken;
// TODO change with axios cancel request
const abortController = new AbortController();
const source = CancelToken.source();
acc.push({
abortController,
source,
file: null,
fileInfo: {
alternativeText: '',