Merge pull request #5702 from strapi/media-lib/add-back-button

Add backbutton in upload
This commit is contained in:
virginieky 2020-04-03 18:15:19 +02:00 committed by GitHub
commit c24c2f9adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 3 deletions

View File

@ -24,11 +24,15 @@ const ModalHeader = ({ goBack, headerBreadcrumbs, withBackButton, HeaderComponen
})) }))
: null; : null;
const handleClick = () => {
goBack('backButton');
};
return ( return (
<Wrapper> <Wrapper>
<ModalSection> <ModalSection>
<HeaderModalTitle> <HeaderModalTitle>
{withBackButton && <BackButton onClick={goBack} type="button" />} {withBackButton && <BackButton onClick={handleClick} type="button" />}
{HeaderComponent && <HeaderComponent />} {HeaderComponent && <HeaderComponent />}
{translatedHeaders && {translatedHeaders &&
translatedHeaders.map(({ key, element }, index) => { translatedHeaders.map(({ key, element }, index) => {

View File

@ -26,6 +26,7 @@ const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
handleAbortUpload, handleAbortUpload,
handleCancelFileToUpload, handleCancelFileToUpload,
handleCleanFilesError, handleCleanFilesError,
handleClearFilesToUploadAndDownload,
handleClickNextButton, handleClickNextButton,
handleClose, handleClose,
handleEditExistingFile, handleEditExistingFile,
@ -46,6 +47,7 @@ const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
toggleModalWarning, toggleModalWarning,
} = useModalContext(); } = useModalContext();
const { const {
backButtonDestination,
Component, Component,
components, components,
headerBreadcrumbs, headerBreadcrumbs,
@ -80,7 +82,39 @@ const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
goNext(); goNext();
}; };
const goBack = () => { const goBack = (elementName = null) => {
const hasFilesToUpload = !isEmpty(filesToUpload);
// Redirect the user to the list modal from the upload one
if (elementName === 'backButton' && backButtonDestination && currentStep === 'upload') {
if (hasFilesToUpload) {
// eslint-disable-next-line no-alert
const confirm = window.confirm(
formatMessage({ id: getTrad('window.confirm.close-modal.files') })
);
if (!confirm) {
return;
}
}
goTo(backButtonDestination);
handleClearFilesToUploadAndDownload();
return;
}
if (
elementName === 'backButton' &&
backButtonDestination &&
currentStep === 'browse' &&
hasFilesToUpload
) {
goTo(backButtonDestination);
return;
}
goTo(prev); goTo(prev);
}; };

View File

@ -19,12 +19,16 @@ const stepper = {
prev: 'list', prev: 'list',
next: 'upload', next: 'upload',
withBackButton: true, withBackButton: true,
backButtonDestination: 'upload',
}, },
upload: { upload: {
Component: UploadList, Component: UploadList,
headerBreadcrumbs: [getTrad('modal.header.select-files')], headerBreadcrumbs: [getTrad('modal.header.select-files')],
next: null, next: null,
prev: 'browse', prev: 'browse',
withBackButton: true,
// Exception in order to not update the entire code
backButtonDestination: 'list',
}, },
'edit-new': { 'edit-new': {
Component: EditForm, Component: EditForm,

View File

@ -340,6 +340,12 @@ const InputModalStepperProvider = ({
}); });
}; };
const handleClearFilesToUploadAndDownload = () => {
dispatch({
type: 'CLEAR_FILES_TO_UPLOAD_AND_DOWNLOAD',
});
};
const handleSetFileToEditError = errorMessage => { const handleSetFileToEditError = errorMessage => {
dispatch({ dispatch({
type: 'SET_FILE_TO_EDIT_ERROR', type: 'SET_FILE_TO_EDIT_ERROR',
@ -427,6 +433,7 @@ const InputModalStepperProvider = ({
handleCancelFileToUpload, handleCancelFileToUpload,
handleClickNextButton, handleClickNextButton,
handleCleanFilesError, handleCleanFilesError,
handleClearFilesToUploadAndDownload,
handleClose, handleClose,
handleEditExistingFile, handleEditExistingFile,
handleFileSelection, handleFileSelection,

View File

@ -43,6 +43,12 @@ const reducer = (state, action) =>
break; break;
} }
case 'CLEAR_FILES_TO_UPLOAD_AND_DOWNLOAD': {
draftState.filesToUpload = [];
draftState.filesToDownload = [];
break;
}
case 'FILE_DOWNLOADED': { case 'FILE_DOWNLOADED': {
const index = state.filesToUpload.findIndex(file => file.tempId === action.fileTempId); const index = state.filesToUpload.findIndex(file => file.tempId === action.fileTempId);

View File

@ -464,6 +464,25 @@ describe('UPLOAD | containers | ModalStepper | reducer', () => {
}); });
}); });
describe('CLEAR_FILES_TO_UPLOAD_AND_DOWNLOAD', () => {
it('should empty the filesToDownload and filesToUpload arrays', () => {
const state = {
filesToDownload: ['1', '2'],
filesToUpload: ['3', '4'],
};
const action = {
type: 'CLEAR_FILES_TO_UPLOAD_AND_DOWNLOAD',
};
const expected = {
filesToDownload: [],
filesToUpload: [],
};
expect(reducer(state, action)).toEqual(expected);
});
});
describe('FILE_DOWLOADED', () => { describe('FILE_DOWLOADED', () => {
it('should update the corresponding file', () => { it('should update the corresponding file', () => {
const state = { const state = {

View File

@ -8,8 +8,9 @@ const stepper = {
browse: { browse: {
Component: UploadForm, Component: UploadForm,
headerBreadcrumbs: [getTrad('modal.header.browse')], headerBreadcrumbs: [getTrad('modal.header.browse')],
prev: null, prev: 'upload',
next: 'upload', next: 'upload',
withBackButton: true,
}, },
upload: { upload: {
Component: UploadList, Component: UploadList,