2020-03-24 15:19:49 +01:00
|
|
|
import React, { useEffect, useState, useReducer, useRef } from 'react';
|
2020-03-31 17:54:12 +02:00
|
|
|
import axios from 'axios';
|
2020-02-13 15:51:13 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-04-03 14:38:44 +02:00
|
|
|
import { isEqual, isEmpty, get, set } from 'lodash';
|
2020-04-03 15:14:18 +02:00
|
|
|
import {
|
|
|
|
Modal,
|
|
|
|
ModalFooter,
|
|
|
|
PopUpWarning,
|
|
|
|
useGlobalContext,
|
|
|
|
auth,
|
|
|
|
request,
|
|
|
|
} from 'strapi-helper-plugin';
|
2020-02-13 15:51:13 +01:00
|
|
|
import { Button } from '@buffetjs/core';
|
2020-02-19 09:14:11 +01:00
|
|
|
import pluginId from '../../pluginId';
|
2020-04-01 13:25:34 +02:00
|
|
|
import { getFilesToDownload, getTrad, getYupError, urlSchema } from '../../utils';
|
2020-02-20 07:58:59 +01:00
|
|
|
import ModalHeader from '../../components/ModalHeader';
|
2020-03-09 16:49:12 +01:00
|
|
|
import stepper from './stepper';
|
2020-02-18 16:39:03 +01:00
|
|
|
import init from './init';
|
|
|
|
import reducer, { initialState } from './reducer';
|
2020-02-13 15:51:13 +01:00
|
|
|
|
2020-03-26 12:34:10 +01:00
|
|
|
const ModalStepper = ({
|
|
|
|
initialFileToEdit,
|
|
|
|
initialStep,
|
|
|
|
isOpen,
|
|
|
|
onClosed,
|
|
|
|
onDeleteMedia,
|
|
|
|
onToggle,
|
|
|
|
}) => {
|
2020-02-18 16:39:03 +01:00
|
|
|
const { formatMessage } = useGlobalContext();
|
2020-03-24 15:19:49 +01:00
|
|
|
const [isWarningDeleteOpen, setIsWarningDeleteOpen] = useState(false);
|
|
|
|
const [shouldDeleteFile, setShouldDeleteFile] = useState(false);
|
2020-03-25 13:30:58 +01:00
|
|
|
const [isFormDisabled, setIsFormDisabled] = useState(false);
|
2020-04-01 13:25:34 +02:00
|
|
|
const [formErrors, setFormErrors] = useState(null);
|
2020-04-01 18:04:35 +02:00
|
|
|
const [shouldRefetch, setShouldRefetch] = useState(false);
|
2020-03-31 16:28:12 +02:00
|
|
|
const [displayNextButton, setDisplayNextButton] = useState(false);
|
2020-02-18 16:39:03 +01:00
|
|
|
const [reducerState, dispatch] = useReducer(reducer, initialState, init);
|
2020-03-31 16:28:12 +02:00
|
|
|
const { currentStep, fileToEdit, filesToDownload, filesToUpload } = reducerState.toJS();
|
2020-03-25 13:05:23 +01:00
|
|
|
const { Component, components, headerBreadcrumbs, next, prev, withBackButton } = stepper[
|
|
|
|
currentStep
|
|
|
|
];
|
2020-02-19 09:27:06 +01:00
|
|
|
const filesToUploadLength = filesToUpload.length;
|
2020-03-24 11:52:11 +01:00
|
|
|
const toggleRef = useRef(onToggle);
|
|
|
|
const editModalRef = useRef();
|
2020-03-31 17:54:12 +02:00
|
|
|
const downloadFilesRef = useRef();
|
2020-03-24 11:52:11 +01:00
|
|
|
|
2020-02-19 10:12:35 +01:00
|
|
|
useEffect(() => {
|
2020-03-31 17:54:12 +02:00
|
|
|
if (currentStep === 'upload') {
|
|
|
|
// Close the modal
|
|
|
|
if (filesToUploadLength === 0) {
|
|
|
|
// Passing true to the onToggle prop will refetch the data when the modal closes
|
|
|
|
toggleRef.current(true);
|
|
|
|
} else {
|
2020-04-01 08:50:08 +02:00
|
|
|
// Download files from url
|
2020-03-31 17:54:12 +02:00
|
|
|
downloadFilesRef.current();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2020-02-19 10:12:35 +01:00
|
|
|
}, [filesToUploadLength, currentStep]);
|
2020-02-18 18:05:52 +01:00
|
|
|
|
2020-03-24 07:35:16 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (isOpen) {
|
|
|
|
goTo(initialStep);
|
|
|
|
|
|
|
|
if (initialFileToEdit) {
|
|
|
|
dispatch({
|
|
|
|
type: 'INIT_FILE_TO_EDIT',
|
|
|
|
fileToEdit: initialFileToEdit,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Disabling the rule because we just want to let the ability to open the modal
|
|
|
|
// at a specific step then we will let the stepper handle the navigation
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [isOpen]);
|
|
|
|
|
2020-02-18 18:05:52 +01:00
|
|
|
const addFilesToUpload = ({ target: { value } }) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ADD_FILES_TO_UPLOAD',
|
|
|
|
filesToUpload: value,
|
|
|
|
});
|
|
|
|
|
|
|
|
goTo(next);
|
|
|
|
};
|
2020-02-18 16:39:03 +01:00
|
|
|
|
2020-03-31 17:54:12 +02:00
|
|
|
downloadFilesRef.current = async () => {
|
|
|
|
const files = getFilesToDownload(filesToUpload);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
files.map(file => {
|
2020-03-31 18:54:56 +02:00
|
|
|
const { source } = file;
|
|
|
|
|
2020-03-31 17:54:12 +02:00
|
|
|
return axios
|
2020-04-03 07:41:58 +02:00
|
|
|
.get(`${strapi.backendURL}/${pluginId}/proxy?url=${file.fileURL}`, {
|
2020-04-03 15:14:18 +02:00
|
|
|
headers: { Authorization: `Bearer ${auth.getToken()}` },
|
2020-03-31 17:54:12 +02:00
|
|
|
responseType: 'blob',
|
2020-03-31 18:54:56 +02:00
|
|
|
cancelToken: source.token,
|
2020-04-07 14:28:54 +02:00
|
|
|
timeout: 60000,
|
2020-03-31 17:54:12 +02:00
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2020-04-03 11:39:02 +02:00
|
|
|
const fileName = file.fileInfo.name;
|
|
|
|
const createdFile = new File([data], fileName, {
|
2020-03-31 17:54:12 +02:00
|
|
|
type: data.type,
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'FILE_DOWNLOADED',
|
|
|
|
blob: createdFile,
|
|
|
|
originalIndex: file.originalIndex,
|
|
|
|
fileTempId: file.tempId,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error('fetch file error', err);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'SET_FILE_TO_DOWNLOAD_ERROR',
|
|
|
|
originalIndex: file.originalIndex,
|
|
|
|
fileTempId: file.tempId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
// Silent
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-25 13:05:23 +01:00
|
|
|
const handleAbortUpload = () => {
|
|
|
|
const { abortController } = fileToEdit;
|
|
|
|
|
|
|
|
abortController.abort();
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_ABORT_UPLOAD',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-26 12:34:10 +01:00
|
|
|
const handleCancelFileToUpload = fileOriginalIndex => {
|
|
|
|
const fileToCancel = filesToUpload.find(file => file.originalIndex === fileOriginalIndex);
|
2020-03-31 18:54:56 +02:00
|
|
|
const { source } = fileToCancel;
|
2020-02-19 09:14:11 +01:00
|
|
|
|
2020-04-01 08:50:08 +02:00
|
|
|
// Cancel
|
2020-03-31 18:54:56 +02:00
|
|
|
if (source) {
|
2020-04-01 08:50:08 +02:00
|
|
|
// Cancel dowload file upload with axios
|
2020-03-31 18:54:56 +02:00
|
|
|
source.cancel('Operation canceled by the user.');
|
|
|
|
} else {
|
2020-04-01 08:50:08 +02:00
|
|
|
// Cancel upload with fetch
|
2020-03-31 18:54:56 +02:00
|
|
|
fileToCancel.abortController.abort();
|
|
|
|
}
|
2020-02-19 09:14:11 +01:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'REMOVE_FILE_TO_UPLOAD',
|
2020-03-26 12:34:10 +01:00
|
|
|
fileIndex: fileOriginalIndex,
|
2020-02-19 09:14:11 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-05 10:56:05 +01:00
|
|
|
const handleChange = ({ target: { name, value } }) => {
|
2020-03-31 16:28:12 +02:00
|
|
|
let val = value;
|
|
|
|
let type = 'ON_CHANGE';
|
|
|
|
|
|
|
|
if (name === 'url') {
|
2020-04-01 13:25:34 +02:00
|
|
|
setFormErrors(null);
|
|
|
|
|
2020-03-31 16:28:12 +02:00
|
|
|
val = value.split('\n');
|
|
|
|
type = 'ON_CHANGE_URLS_TO_DOWNLOAD';
|
|
|
|
}
|
|
|
|
|
2020-03-05 10:56:05 +01:00
|
|
|
dispatch({
|
2020-03-31 16:28:12 +02:00
|
|
|
type,
|
2020-03-05 10:56:05 +01:00
|
|
|
keys: name,
|
2020-03-31 16:28:12 +02:00
|
|
|
value: val,
|
2020-03-05 10:56:05 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-24 15:19:49 +01:00
|
|
|
const handleConfirmDeleteFile = () => {
|
|
|
|
setShouldDeleteFile(true);
|
2020-03-24 16:11:03 +01:00
|
|
|
toggleModalWarning();
|
2020-03-24 15:19:49 +01:00
|
|
|
};
|
|
|
|
|
2020-04-01 13:25:34 +02:00
|
|
|
const handleClickNextButton = async () => {
|
|
|
|
try {
|
|
|
|
await urlSchema.validate(
|
|
|
|
{ filesToDownload: filesToDownload.filter(url => !isEmpty(url)) },
|
|
|
|
{ abortEarly: false }
|
|
|
|
);
|
|
|
|
|
|
|
|
setFormErrors(null);
|
|
|
|
// Navigate to next step
|
|
|
|
dispatch({
|
|
|
|
type: 'ADD_URLS_TO_FILES_TO_UPLOAD',
|
|
|
|
nextStep: next,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
const formattedErrors = getYupError(err);
|
|
|
|
|
|
|
|
setFormErrors(formattedErrors.filesToDownload);
|
|
|
|
}
|
2020-03-31 16:28:12 +02:00
|
|
|
};
|
|
|
|
|
2020-03-24 15:19:49 +01:00
|
|
|
const handleClickDeleteFile = async () => {
|
|
|
|
toggleModalWarning();
|
|
|
|
};
|
|
|
|
|
2020-02-25 14:35:48 +01:00
|
|
|
const handleClickDeleteFileToUpload = fileIndex => {
|
|
|
|
dispatch({
|
|
|
|
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
|
|
fileIndex,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (currentStep === 'edit-new') {
|
|
|
|
dispatch({
|
|
|
|
type: 'RESET_FILE_TO_EDIT',
|
|
|
|
});
|
|
|
|
|
|
|
|
goNext();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-24 15:19:49 +01:00
|
|
|
const handleClose = () => {
|
2020-03-24 07:35:16 +01:00
|
|
|
onClosed();
|
2020-03-25 13:30:58 +01:00
|
|
|
setIsFormDisabled(false);
|
2020-03-31 16:28:12 +02:00
|
|
|
setDisplayNextButton(false);
|
2020-04-01 13:25:34 +02:00
|
|
|
setFormErrors(null);
|
2020-04-01 18:04:35 +02:00
|
|
|
setShouldRefetch(false);
|
2020-03-24 07:35:16 +01:00
|
|
|
|
2020-02-18 16:39:03 +01:00
|
|
|
dispatch({
|
|
|
|
type: 'RESET_PROPS',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-24 15:19:49 +01:00
|
|
|
const handleCloseModalWarning = async () => {
|
|
|
|
if (shouldDeleteFile) {
|
|
|
|
const { id } = fileToEdit;
|
|
|
|
|
2020-03-26 12:34:10 +01:00
|
|
|
onDeleteMedia(id);
|
2020-03-24 15:19:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-20 16:50:00 +01:00
|
|
|
const handleGoToEditNewFile = fileIndex => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SET_FILE_TO_EDIT',
|
|
|
|
fileIndex,
|
|
|
|
});
|
|
|
|
|
|
|
|
goTo('edit-new');
|
|
|
|
};
|
|
|
|
|
2020-02-19 11:21:25 +01:00
|
|
|
const handleGoToAddBrowseFiles = () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'CLEAN_FILES_ERROR',
|
|
|
|
});
|
|
|
|
|
|
|
|
goBack();
|
|
|
|
};
|
|
|
|
|
2020-02-24 15:45:00 +01:00
|
|
|
const handleSetCropResult = blob => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SET_CROP_RESULT',
|
|
|
|
blob,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSubmitEditNewFile = e => {
|
|
|
|
e.preventDefault();
|
2020-02-24 15:09:07 +01:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_SUBMIT_EDIT_NEW_FILE',
|
|
|
|
});
|
2020-02-24 15:45:00 +01:00
|
|
|
|
|
|
|
goNext();
|
2020-02-24 15:09:07 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 13:05:23 +01:00
|
|
|
const handleSubmitEditExistingFile = async (
|
|
|
|
e,
|
|
|
|
shouldDuplicateMedia = false,
|
|
|
|
file = fileToEdit.file
|
|
|
|
) => {
|
2020-03-24 10:48:21 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_SUBMIT_EDIT_EXISTING_FILE',
|
|
|
|
});
|
|
|
|
|
|
|
|
const headers = {};
|
|
|
|
const formData = new FormData();
|
|
|
|
|
2020-03-24 16:11:03 +01:00
|
|
|
// If the file has been cropped we need to add it to the formData
|
|
|
|
// otherwise we just don't send it
|
2020-03-25 13:05:23 +01:00
|
|
|
const didCropFile = file instanceof File;
|
|
|
|
const { abortController, id, fileInfo } = fileToEdit;
|
2020-03-24 16:11:03 +01:00
|
|
|
const requestURL = shouldDuplicateMedia ? `/${pluginId}` : `/${pluginId}?id=${id}`;
|
2020-03-24 10:48:21 +01:00
|
|
|
|
|
|
|
if (didCropFile) {
|
|
|
|
formData.append('files', file);
|
2020-03-24 16:11:03 +01:00
|
|
|
}
|
2020-03-24 10:48:21 +01:00
|
|
|
|
2020-03-24 16:11:03 +01:00
|
|
|
formData.append('fileInfo', JSON.stringify(fileInfo));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await request(
|
|
|
|
requestURL,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
body: formData,
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
// Close the modal and refetch data
|
|
|
|
toggleRef.current(true);
|
|
|
|
} catch (err) {
|
2020-04-07 11:18:26 +02:00
|
|
|
console.error(err);
|
2020-04-07 13:23:08 +02:00
|
|
|
const status = get(err, 'response.status', get(err, 'status', null));
|
|
|
|
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
|
2020-04-02 21:53:25 +02:00
|
|
|
const errorMessage = get(
|
|
|
|
err,
|
|
|
|
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
|
2020-04-07 13:23:08 +02:00
|
|
|
get(err, ['response', 'payload', 'message'], statusText)
|
2020-04-02 21:53:25 +02:00
|
|
|
);
|
|
|
|
|
2020-04-07 11:18:26 +02:00
|
|
|
if (status) {
|
2020-04-03 10:05:24 +02:00
|
|
|
dispatch({
|
|
|
|
type: 'SET_FILE_TO_EDIT_ERROR',
|
|
|
|
errorMessage,
|
|
|
|
});
|
|
|
|
}
|
2020-03-24 10:48:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-24 11:52:11 +01:00
|
|
|
const handleReplaceMedia = () => {
|
|
|
|
editModalRef.current.click();
|
|
|
|
};
|
|
|
|
|
2020-02-24 16:22:17 +01:00
|
|
|
const handleToggle = () => {
|
|
|
|
if (filesToUploadLength > 0) {
|
|
|
|
// eslint-disable-next-line no-alert
|
2020-03-28 13:35:56 +01:00
|
|
|
const confirm = window.confirm(
|
|
|
|
formatMessage({ id: getTrad('window.confirm.close-modal.files') })
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 21:37:28 +02:00
|
|
|
if (!isEqual(initialFileToEdit, fileToEdit) && currentStep === 'edit') {
|
2020-03-28 13:35:56 +01:00
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
const confirm = window.confirm(
|
|
|
|
formatMessage({ id: getTrad('window.confirm.close-modal.file') })
|
|
|
|
);
|
2020-02-24 16:22:17 +01:00
|
|
|
|
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:04:35 +02:00
|
|
|
onToggle(shouldRefetch);
|
2020-02-24 16:22:17 +01:00
|
|
|
};
|
|
|
|
|
2020-02-19 09:14:11 +01:00
|
|
|
const handleUploadFiles = async () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SET_FILES_UPLOADING_STATE',
|
|
|
|
});
|
|
|
|
|
2020-02-19 13:40:36 +01:00
|
|
|
const requests = filesToUpload.map(
|
2020-04-03 14:38:44 +02:00
|
|
|
async ({ file, fileInfo, originalName, originalIndex, abortController }) => {
|
2020-02-19 13:40:36 +01:00
|
|
|
const formData = new FormData();
|
|
|
|
const headers = {};
|
2020-04-03 14:38:44 +02:00
|
|
|
|
|
|
|
if (originalName === fileInfo.name) {
|
|
|
|
set(fileInfo, 'name', null);
|
|
|
|
}
|
|
|
|
|
2020-02-19 13:40:36 +01:00
|
|
|
formData.append('files', file);
|
2020-03-05 10:56:05 +01:00
|
|
|
formData.append('fileInfo', JSON.stringify(fileInfo));
|
2020-02-19 13:40:36 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await request(
|
|
|
|
`/${pluginId}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
body: formData,
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2020-04-01 18:04:35 +02:00
|
|
|
setShouldRefetch(true);
|
|
|
|
|
2020-02-19 13:40:36 +01:00
|
|
|
dispatch({
|
|
|
|
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
|
|
fileIndex: originalIndex,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2020-04-07 11:18:26 +02:00
|
|
|
console.error(err);
|
2020-04-07 13:23:08 +02:00
|
|
|
const status = get(err, 'response.status', get(err, 'status', null));
|
|
|
|
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
|
2020-02-19 13:40:36 +01:00
|
|
|
const errorMessage = get(
|
|
|
|
err,
|
|
|
|
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
|
2020-04-07 13:23:08 +02:00
|
|
|
get(err, ['response', 'payload', 'message'], statusText)
|
2020-02-19 13:40:36 +01:00
|
|
|
);
|
|
|
|
|
2020-04-07 11:18:26 +02:00
|
|
|
if (status) {
|
2020-03-26 12:34:10 +01:00
|
|
|
dispatch({
|
|
|
|
type: 'SET_FILE_ERROR',
|
|
|
|
fileIndex: originalIndex,
|
|
|
|
errorMessage,
|
|
|
|
});
|
|
|
|
}
|
2020-02-19 10:12:35 +01:00
|
|
|
}
|
2020-02-19 13:40:36 +01:00
|
|
|
}
|
|
|
|
);
|
2020-02-19 09:14:11 +01:00
|
|
|
|
2020-02-19 13:40:36 +01:00
|
|
|
await Promise.all(requests);
|
2020-02-19 09:14:11 +01:00
|
|
|
};
|
|
|
|
|
2020-02-18 18:05:52 +01:00
|
|
|
const goBack = () => {
|
|
|
|
goTo(prev);
|
|
|
|
};
|
|
|
|
|
2020-02-19 09:14:11 +01:00
|
|
|
const goNext = () => {
|
|
|
|
if (next === null) {
|
|
|
|
onToggle();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
goTo(next);
|
|
|
|
};
|
|
|
|
|
2020-02-18 18:05:52 +01:00
|
|
|
const goTo = to => {
|
|
|
|
dispatch({
|
|
|
|
type: 'GO_TO',
|
|
|
|
to,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-24 15:19:49 +01:00
|
|
|
const toggleModalWarning = () => {
|
|
|
|
setIsWarningDeleteOpen(prev => !prev);
|
|
|
|
};
|
|
|
|
|
2020-03-31 16:28:12 +02:00
|
|
|
const shouldDisplayNextButton = currentStep === 'browse' && displayNextButton;
|
2020-04-06 16:32:01 +02:00
|
|
|
const isFinishButtonDisabled = filesToUpload.some(file => file.isDownloading || file.isUploading);
|
2020-04-07 10:57:59 +02:00
|
|
|
const areButtonsDisabledOnEditExistingFile =
|
|
|
|
currentStep === 'edit' && fileToEdit.isUploading === true;
|
2020-03-31 16:28:12 +02:00
|
|
|
|
2020-02-13 15:51:13 +01:00
|
|
|
return (
|
2020-03-24 15:19:49 +01:00
|
|
|
<>
|
|
|
|
<Modal isOpen={isOpen} onToggle={handleToggle} onClosed={handleClose}>
|
|
|
|
{/* header title */}
|
|
|
|
<ModalHeader
|
|
|
|
goBack={goBack}
|
2020-03-24 15:45:01 +01:00
|
|
|
headerBreadcrumbs={headerBreadcrumbs}
|
2020-02-24 15:09:07 +01:00
|
|
|
withBackButton={withBackButton}
|
2020-02-18 18:05:52 +01:00
|
|
|
/>
|
2020-03-24 15:45:01 +01:00
|
|
|
|
2020-03-24 15:19:49 +01:00
|
|
|
{/* body of the modal */}
|
|
|
|
{Component && (
|
|
|
|
<Component
|
2020-03-25 13:05:23 +01:00
|
|
|
onAbortUpload={handleAbortUpload}
|
2020-03-24 15:19:49 +01:00
|
|
|
addFilesToUpload={addFilesToUpload}
|
|
|
|
fileToEdit={fileToEdit}
|
2020-03-31 16:28:12 +02:00
|
|
|
filesToDownload={filesToDownload}
|
2020-03-24 15:19:49 +01:00
|
|
|
filesToUpload={filesToUpload}
|
2020-04-01 13:25:34 +02:00
|
|
|
formErrors={formErrors}
|
2020-03-25 13:05:23 +01:00
|
|
|
components={components}
|
2020-03-25 13:30:58 +01:00
|
|
|
isEditingUploadedFile={currentStep === 'edit'}
|
|
|
|
isFormDisabled={isFormDisabled}
|
2020-03-24 15:19:49 +01:00
|
|
|
onChange={handleChange}
|
|
|
|
onClickCancelUpload={handleCancelFileToUpload}
|
|
|
|
onClickDeleteFileToUpload={
|
2020-03-26 12:34:10 +01:00
|
|
|
currentStep === 'edit' ? handleClickDeleteFile : handleClickDeleteFileToUpload
|
2020-03-24 15:19:49 +01:00
|
|
|
}
|
|
|
|
onClickEditNewFile={handleGoToEditNewFile}
|
|
|
|
onGoToAddBrowseFiles={handleGoToAddBrowseFiles}
|
|
|
|
onSubmitEdit={
|
2020-03-26 12:34:10 +01:00
|
|
|
currentStep === 'edit' ? handleSubmitEditExistingFile : handleSubmitEditNewFile
|
2020-03-24 15:19:49 +01:00
|
|
|
}
|
|
|
|
onToggle={handleToggle}
|
2020-03-25 13:30:58 +01:00
|
|
|
toggleDisableForm={setIsFormDisabled}
|
2020-03-24 15:19:49 +01:00
|
|
|
ref={currentStep === 'edit' ? editModalRef : null}
|
|
|
|
setCropResult={handleSetCropResult}
|
2020-03-31 16:28:12 +02:00
|
|
|
setShouldDisplayNextButton={setDisplayNextButton}
|
2020-03-24 15:19:49 +01:00
|
|
|
withBackButton={withBackButton}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<ModalFooter>
|
|
|
|
<section>
|
|
|
|
<Button type="button" color="cancel" onClick={handleToggle}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
2020-02-19 09:27:06 +01:00
|
|
|
</Button>
|
2020-03-31 16:28:12 +02:00
|
|
|
{shouldDisplayNextButton && (
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="primary"
|
|
|
|
onClick={handleClickNextButton}
|
|
|
|
disabled={isEmpty(filesToDownload)}
|
|
|
|
>
|
2020-04-01 08:50:08 +02:00
|
|
|
{formatMessage({ id: getTrad('button.next') })}
|
2020-03-31 16:28:12 +02:00
|
|
|
</Button>
|
|
|
|
)}
|
2020-03-24 15:19:49 +01:00
|
|
|
{currentStep === 'upload' && (
|
2020-03-31 17:54:12 +02:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="success"
|
|
|
|
onClick={handleUploadFiles}
|
|
|
|
disabled={isFinishButtonDisabled}
|
|
|
|
>
|
2020-03-24 15:19:49 +01:00
|
|
|
{formatMessage(
|
|
|
|
{
|
|
|
|
id: getTrad(
|
|
|
|
`modal.upload-list.footer.button.${
|
|
|
|
filesToUploadLength > 1 ? 'plural' : 'singular'
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{ number: filesToUploadLength }
|
|
|
|
)}
|
2020-03-24 11:52:11 +01:00
|
|
|
</Button>
|
2020-03-24 15:19:49 +01:00
|
|
|
)}
|
|
|
|
{currentStep === 'edit-new' && (
|
|
|
|
<Button color="success" type="button" onClick={handleSubmitEditNewFile}>
|
2020-03-24 11:52:11 +01:00
|
|
|
{formatMessage({ id: 'form.button.finish' })}
|
|
|
|
</Button>
|
2020-03-24 15:19:49 +01:00
|
|
|
)}
|
|
|
|
{currentStep === 'edit' && (
|
|
|
|
<div style={{ margin: 'auto 0' }}>
|
|
|
|
<Button
|
2020-04-07 10:57:59 +02:00
|
|
|
disabled={isFormDisabled || areButtonsDisabledOnEditExistingFile}
|
2020-03-24 15:19:49 +01:00
|
|
|
color="primary"
|
|
|
|
onClick={handleReplaceMedia}
|
|
|
|
style={{ marginRight: 10 }}
|
|
|
|
>
|
|
|
|
Replace media
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
2020-04-07 10:57:59 +02:00
|
|
|
disabled={isFormDisabled || areButtonsDisabledOnEditExistingFile}
|
2020-03-24 15:19:49 +01:00
|
|
|
color="success"
|
|
|
|
type="button"
|
|
|
|
onClick={handleSubmitEditExistingFile}
|
|
|
|
>
|
|
|
|
{formatMessage({ id: 'form.button.finish' })}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</section>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
<PopUpWarning
|
|
|
|
onClosed={handleCloseModalWarning}
|
|
|
|
isOpen={isWarningDeleteOpen}
|
|
|
|
toggleModal={toggleModalWarning}
|
|
|
|
popUpWarningType="danger"
|
|
|
|
onConfirm={handleConfirmDeleteFile}
|
|
|
|
/>
|
|
|
|
</>
|
2020-02-13 15:51:13 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
ModalStepper.defaultProps = {
|
2020-03-24 07:35:16 +01:00
|
|
|
initialFileToEdit: null,
|
|
|
|
initialStep: 'browse',
|
|
|
|
onClosed: () => {},
|
2020-03-26 12:34:10 +01:00
|
|
|
onDeleteMedia: () => {},
|
2020-02-13 15:51:13 +01:00
|
|
|
onToggle: () => {},
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
ModalStepper.propTypes = {
|
2020-03-24 07:35:16 +01:00
|
|
|
initialFileToEdit: PropTypes.object,
|
|
|
|
initialStep: PropTypes.string,
|
2020-02-13 15:51:13 +01:00
|
|
|
isOpen: PropTypes.bool.isRequired,
|
2020-03-24 07:35:16 +01:00
|
|
|
onClosed: PropTypes.func,
|
2020-03-26 12:34:10 +01:00
|
|
|
onDeleteMedia: PropTypes.func,
|
2020-02-13 15:51:13 +01:00
|
|
|
onToggle: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
export default ModalStepper;
|