2020-03-23 16:24:26 +01:00
|
|
|
import React, { useEffect, memo } from 'react';
|
2020-03-09 16:49:12 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Modal, ModalFooter, useGlobalContext } from 'strapi-helper-plugin';
|
|
|
|
import { Button } from '@buffetjs/core';
|
2020-03-23 16:24:26 +01:00
|
|
|
import ModalHeader from '../../components/ModalHeader';
|
2020-03-09 16:49:12 +01:00
|
|
|
import stepper from './stepper';
|
|
|
|
import getTrad from '../../utils/getTrad';
|
|
|
|
import useModalContext from '../../hooks/useModalContext';
|
|
|
|
|
2020-03-25 01:04:49 +01:00
|
|
|
const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
|
2020-03-09 16:49:12 +01:00
|
|
|
const { formatMessage } = useGlobalContext();
|
|
|
|
const {
|
2020-03-24 00:11:16 +01:00
|
|
|
addFilesToUpload,
|
2020-03-09 16:49:12 +01:00
|
|
|
currentStep,
|
2020-03-24 00:11:16 +01:00
|
|
|
fetchMediaLib,
|
2020-03-09 16:49:12 +01:00
|
|
|
filesToUpload,
|
2020-03-24 00:11:16 +01:00
|
|
|
fileToEdit,
|
|
|
|
goTo,
|
2020-03-09 16:49:12 +01:00
|
|
|
handleCancelFileToUpload,
|
|
|
|
handleCleanFilesError,
|
2020-03-24 00:11:16 +01:00
|
|
|
handleClose,
|
|
|
|
handleFileToEditChange,
|
2020-03-09 16:49:12 +01:00
|
|
|
handleGoToEditNewFile,
|
|
|
|
handleRemoveFileToUpload,
|
|
|
|
handleResetFileToEdit,
|
2020-03-24 00:11:16 +01:00
|
|
|
handleSetCropResult,
|
|
|
|
handleUploadFiles,
|
|
|
|
selectedFiles,
|
2020-03-09 16:49:12 +01:00
|
|
|
} = useModalContext();
|
|
|
|
const { Component, headerBreadcrumbs, next, prev, withBackButton, HeaderComponent } = stepper[
|
|
|
|
currentStep
|
|
|
|
];
|
|
|
|
const filesToUploadLength = filesToUpload.length;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (currentStep === 'upload' && filesToUploadLength === 0) {
|
|
|
|
// Go to the modal list view when file uploading is over
|
|
|
|
fetchMediaLib();
|
|
|
|
goTo('list');
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [filesToUploadLength, currentStep]);
|
|
|
|
|
|
|
|
const addFilesToUploadList = ({ target: { value } }) => {
|
|
|
|
addFilesToUpload({ target: { value } });
|
|
|
|
|
2020-03-23 16:24:26 +01:00
|
|
|
goNext();
|
2020-03-09 16:49:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleClickDeleteFileToUpload = fileIndex => {
|
|
|
|
handleRemoveFileToUpload(fileIndex);
|
|
|
|
|
|
|
|
if (currentStep === 'edit-new') {
|
|
|
|
handleResetFileToEdit();
|
|
|
|
|
|
|
|
goNext();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleGoToAddBrowseFiles = () => {
|
|
|
|
handleCleanFilesError();
|
|
|
|
|
|
|
|
goBack();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSubmitEditNewFile = e => {
|
|
|
|
e.preventDefault();
|
2020-03-25 01:04:49 +01:00
|
|
|
goNext();
|
|
|
|
};
|
2020-03-23 16:24:26 +01:00
|
|
|
|
2020-03-25 01:04:49 +01:00
|
|
|
const handleSubmit = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
onInputMediaChange(selectedFiles);
|
2020-03-09 16:49:12 +01:00
|
|
|
goNext();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleToggle = () => {
|
|
|
|
if (filesToUploadLength > 0 || selectedFiles.length > 0) {
|
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
const confirm = window.confirm(formatMessage({ id: getTrad('window.confirm.close-modal') }));
|
|
|
|
|
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggle();
|
|
|
|
};
|
|
|
|
|
|
|
|
const goBack = () => {
|
|
|
|
goTo(prev);
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: when back button needed
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const goNext = () => {
|
|
|
|
if (next === null) {
|
|
|
|
onToggle();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
goTo(next);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal isOpen={isOpen} onToggle={handleToggle} onClosed={handleClose}>
|
|
|
|
{/* header title */}
|
|
|
|
<ModalHeader
|
|
|
|
goBack={goBack}
|
|
|
|
HeaderComponent={HeaderComponent}
|
|
|
|
headerBreadcrumbs={headerBreadcrumbs}
|
|
|
|
withBackButton={withBackButton}
|
|
|
|
/>
|
|
|
|
{/* body of the modal */}
|
|
|
|
{Component && (
|
|
|
|
<Component
|
|
|
|
addFilesToUpload={addFilesToUploadList}
|
|
|
|
fileToEdit={fileToEdit}
|
|
|
|
filesToUpload={filesToUpload}
|
|
|
|
onChange={handleFileToEditChange}
|
|
|
|
onClickCancelUpload={handleCancelFileToUpload}
|
|
|
|
onClickDeleteFileToUpload={handleClickDeleteFileToUpload}
|
|
|
|
onClickEditNewFile={handleGoToEditNewFile}
|
|
|
|
onGoToAddBrowseFiles={handleGoToAddBrowseFiles}
|
|
|
|
onSubmitEditNewFile={handleSubmitEditNewFile}
|
|
|
|
onToggle={handleToggle}
|
|
|
|
setCropResult={handleSetCropResult}
|
|
|
|
withBackButton={withBackButton}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<ModalFooter>
|
|
|
|
<section>
|
|
|
|
<Button type="button" color="cancel" onClick={handleToggle}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
</Button>
|
|
|
|
{currentStep === 'upload' ? (
|
|
|
|
<Button type="button" color="success" onClick={handleUploadFiles}>
|
|
|
|
{formatMessage(
|
|
|
|
{
|
|
|
|
id: getTrad(
|
|
|
|
`modal.upload-list.footer.button.${
|
|
|
|
filesToUploadLength > 1 ? 'plural' : 'singular'
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{ number: filesToUploadLength }
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
) : (
|
2020-03-25 01:04:49 +01:00
|
|
|
<Button color="success" type="button" onClick={handleSubmit}>
|
2020-03-09 16:49:12 +01:00
|
|
|
{formatMessage({ id: 'form.button.finish' })}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</section>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-23 16:24:26 +01:00
|
|
|
InputModalStepper.defaultProps = {
|
2020-03-09 16:49:12 +01:00
|
|
|
onToggle: () => {},
|
|
|
|
};
|
|
|
|
|
2020-03-23 16:24:26 +01:00
|
|
|
InputModalStepper.propTypes = {
|
2020-03-09 16:49:12 +01:00
|
|
|
isOpen: PropTypes.bool.isRequired,
|
2020-03-25 01:04:49 +01:00
|
|
|
onInputMediaChange: PropTypes.func.isRequired,
|
2020-03-09 16:49:12 +01:00
|
|
|
onToggle: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2020-03-23 16:24:26 +01:00
|
|
|
export default memo(InputModalStepper);
|