Remove fileInfo.name when unchanged

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-04-03 14:38:44 +02:00
parent 500c12f7c7
commit c69b6e8d4e
7 changed files with 60 additions and 30 deletions

View File

@ -1,7 +1,7 @@
import React, { useReducer, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { request, generateSearchFromFilters, useGlobalContext } from 'strapi-helper-plugin';
import { get, isEmpty } from 'lodash';
import { clone, get, isEmpty, set } from 'lodash';
import axios from 'axios';
import pluginId from '../../pluginId';
import {
@ -72,9 +72,9 @@ const InputModalStepperProvider = ({
return axios
.get(file.fileURL, {
headers: new Headers({ Origin: window.location.origin, mode: 'cors' }),
responseType: 'blob',
cancelToken: source.token,
timeout: 30000,
})
.then(({ data }) => {
const createdFile = new File([data], file.fileURL, {
@ -352,11 +352,19 @@ const InputModalStepperProvider = ({
});
const requests = filesToUpload.map(
async ({ file, fileInfo, originalIndex, abortController }) => {
async ({ file, fileInfo, originalIndex, originalName, abortController }) => {
const formData = new FormData();
const headers = {};
const infos = clone(fileInfo);
if (originalName === infos.name) {
set(infos, 'name', null);
}
console.log(infos);
formData.append('files', file);
formData.append('fileInfo', JSON.stringify(fileInfo));
formData.append('fileInfo', JSON.stringify(infos));
try {
const uploadedFile = await request(

View File

@ -220,7 +220,7 @@ const reducer = (state, action) =>
...draftState.filesToUpload[index],
isDownloading: false,
hasError: true,
errorMessage: draftState.filesToUpload[index].fileURL,
errorMessage: draftState.filesToUpload[index].fileOriginalURL,
};
break;

View File

@ -1,7 +1,7 @@
import React, { useEffect, useState, useReducer, useRef } from 'react';
import axios from 'axios';
import PropTypes from 'prop-types';
import { isEqual, isEmpty, get } from 'lodash';
import { isEqual, isEmpty, get, set } from 'lodash';
import { Modal, ModalFooter, PopUpWarning, useGlobalContext, request } from 'strapi-helper-plugin';
import { Button } from '@buffetjs/core';
import pluginId from '../../pluginId';
@ -85,10 +85,9 @@ const ModalStepper = ({
return axios
.get(file.fileURL, {
headers: new Headers({ Origin: window.location.origin, mode: 'cors' }),
responseType: 'blob',
cancelToken: source.token,
// Should we add a timeout?
timeout: 30000,
})
.then(({ data }) => {
const fileName = file.fileInfo.name;
@ -356,9 +355,14 @@ const ModalStepper = ({
});
const requests = filesToUpload.map(
async ({ file, fileInfo, originalIndex, abortController }) => {
async ({ file, fileInfo, originalName, originalIndex, abortController }) => {
const formData = new FormData();
const headers = {};
if (originalName === fileInfo.name) {
set(fileInfo, 'name', null);
}
formData.append('files', file);
formData.append('fileInfo', JSON.stringify(fileInfo));

View File

@ -100,7 +100,7 @@ const reducer = (state, action) => {
return file
.update('isDownloading', () => false)
.update('hasError', () => true)
.update('errorMessage', () => file.get('fileURL'));
.update('errorMessage', () => file.get('fileOriginalURL'));
}
return file;

View File

@ -18,6 +18,9 @@ const createNewFilesToDownloadArray = (filesURLArray, alreadyUploadedFiles) => {
return acc;
}
try {
const url = new URL(current);
const name = decodeURIComponent(url.pathname.substring(url.pathname.lastIndexOf('/') + 1));
const CancelToken = axios.CancelToken;
const abortController = new AbortController();
const source = CancelToken.source();
@ -29,15 +32,20 @@ const createNewFilesToDownloadArray = (filesURLArray, alreadyUploadedFiles) => {
fileInfo: {
alternativeText: '',
caption: '',
name: current.substring(current.lastIndexOf('/') + 1),
name,
},
fileURL: current,
fileURL: url,
fileOriginalURL: current,
originalName: name,
hasError: false,
errorMessage: null,
isUploading: false,
isDownloading: true,
tempId: max + index,
});
} catch (err) {
// invalid url
}
return acc;
}, []);

View File

@ -13,6 +13,7 @@ const createNewFilesToUploadArray = filesObject => {
},
hasError: false,
errorMessage: null,
originalName: currentFile.name,
isUploading: false,
tempId: null,
});

View File

@ -13,7 +13,16 @@ const urlSchema = yup.object().shape({
message: '${path}',
test(values) {
const filtered = values.filter(val => {
return !val.startsWith('http');
// return !val.startsWith('http');
try {
// eslint-disable-next-line no-new
new URL(val);
return false;
} catch (err) {
// invalid url
return true;
}
});
const filteredLength = filtered.length;