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;
const handleClick = () => {
goBack('backButton');
};
return (
<Wrapper>
<ModalSection>
<HeaderModalTitle>
{withBackButton && <BackButton onClick={goBack} type="button" />}
{withBackButton && <BackButton onClick={handleClick} type="button" />}
{HeaderComponent && <HeaderComponent />}
{translatedHeaders &&
translatedHeaders.map(({ key, element }, index) => {

View File

@ -26,6 +26,7 @@ const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
handleAbortUpload,
handleCancelFileToUpload,
handleCleanFilesError,
handleClearFilesToUploadAndDownload,
handleClickNextButton,
handleClose,
handleEditExistingFile,
@ -46,6 +47,7 @@ const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
toggleModalWarning,
} = useModalContext();
const {
backButtonDestination,
Component,
components,
headerBreadcrumbs,
@ -80,7 +82,39 @@ const InputModalStepper = ({ isOpen, onToggle, onInputMediaChange }) => {
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);
};

View File

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

View File

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

View File

@ -43,6 +43,12 @@ const reducer = (state, action) =>
break;
}
case 'CLEAR_FILES_TO_UPLOAD_AND_DOWNLOAD': {
draftState.filesToUpload = [];
draftState.filesToDownload = [];
break;
}
case 'FILE_DOWNLOADED': {
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', () => {
it('should update the corresponding file', () => {
const state = {

View File

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