2020-02-19 10:12:35 +01:00
|
|
|
import React, { useEffect, useReducer, useRef } from 'react';
|
2020-02-13 15:51:13 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-02-19 09:14:11 +01:00
|
|
|
import { get } from 'lodash';
|
2020-02-13 15:51:13 +01:00
|
|
|
import {
|
|
|
|
HeaderModal,
|
|
|
|
HeaderModalTitle,
|
|
|
|
Modal,
|
|
|
|
ModalFooter,
|
2020-02-18 16:39:03 +01:00
|
|
|
useGlobalContext,
|
2020-02-19 09:14:11 +01:00
|
|
|
request,
|
2020-02-13 15:51:13 +01:00
|
|
|
} from 'strapi-helper-plugin';
|
|
|
|
import { Button } from '@buffetjs/core';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-02-19 09:14:11 +01:00
|
|
|
import pluginId from '../../pluginId';
|
2020-02-18 16:39:03 +01:00
|
|
|
import stepper from './utils/stepper';
|
|
|
|
import init from './init';
|
|
|
|
import reducer, { initialState } from './reducer';
|
2020-02-19 09:27:06 +01:00
|
|
|
import getTrad from '../../utils/getTrad';
|
2020-02-13 15:51:13 +01:00
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
const ModalStepper = ({ isOpen, onToggle }) => {
|
2020-02-18 16:39:03 +01:00
|
|
|
const { formatMessage } = useGlobalContext();
|
|
|
|
const [reducerState, dispatch] = useReducer(reducer, initialState, init);
|
2020-02-18 18:05:52 +01:00
|
|
|
const { currentStep, filesToUpload } = reducerState.toJS();
|
|
|
|
const { Component, headerTradId, next, prev } = stepper[currentStep];
|
2020-02-19 09:27:06 +01:00
|
|
|
const filesToUploadLength = filesToUpload.length;
|
2020-02-19 10:12:35 +01:00
|
|
|
const toggleRef = useRef();
|
|
|
|
toggleRef.current = onToggle;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (currentStep === 'upload' && filesToUploadLength === 0) {
|
|
|
|
// Close modal when file uploading is over
|
|
|
|
toggleRef.current();
|
|
|
|
}
|
|
|
|
}, [filesToUploadLength, currentStep]);
|
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-02-19 09:14:11 +01:00
|
|
|
const handleCancelFileToUpload = fileIndex => {
|
|
|
|
const fileToCancel = get(filesToUpload, fileIndex, {});
|
|
|
|
|
|
|
|
// Cancel upload
|
|
|
|
fileToCancel.abortController.abort();
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
|
|
fileIndex,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-18 16:39:03 +01:00
|
|
|
const handleClosed = () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'RESET_PROPS',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-19 11:21:25 +01:00
|
|
|
const handleGoToAddBrowseFiles = () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'CLEAN_FILES_ERROR',
|
|
|
|
});
|
|
|
|
|
|
|
|
goBack();
|
|
|
|
};
|
|
|
|
|
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(
|
|
|
|
async ({ file, originalIndex, abortController }) => {
|
|
|
|
const formData = new FormData();
|
|
|
|
const headers = {};
|
|
|
|
formData.append('files', file);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await request(
|
|
|
|
`/${pluginId}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
body: formData,
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
|
|
fileIndex: originalIndex,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
const errorMessage = get(
|
|
|
|
err,
|
|
|
|
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
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 10:12:35 +01:00
|
|
|
// FIXME: when back button needed
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
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-02-13 15:51:13 +01:00
|
|
|
return (
|
2020-02-19 13:40:36 +01:00
|
|
|
<Modal isOpen={isOpen} onToggle={onToggle} onClosed={handleClosed}>
|
2020-02-18 16:39:03 +01:00
|
|
|
{/* header title */}
|
2020-02-13 15:51:13 +01:00
|
|
|
<HeaderModal>
|
|
|
|
<section>
|
|
|
|
<HeaderModalTitle>
|
2020-02-18 18:05:52 +01:00
|
|
|
<FormattedMessage id={headerTradId} />
|
2020-02-13 15:51:13 +01:00
|
|
|
</HeaderModalTitle>
|
|
|
|
</section>
|
2020-02-18 16:39:03 +01:00
|
|
|
</HeaderModal>
|
2020-02-18 18:05:52 +01:00
|
|
|
{/* body of the modal */}
|
|
|
|
{Component && (
|
|
|
|
<Component
|
|
|
|
addFilesToUpload={addFilesToUpload}
|
|
|
|
filesToUpload={filesToUpload}
|
2020-02-19 09:14:11 +01:00
|
|
|
onClickCancelUpload={handleCancelFileToUpload}
|
2020-02-19 11:21:25 +01:00
|
|
|
onGoToAddBrowseFiles={handleGoToAddBrowseFiles}
|
2020-02-18 18:05:52 +01:00
|
|
|
/>
|
|
|
|
)}
|
2020-02-18 16:39:03 +01:00
|
|
|
|
|
|
|
<ModalFooter>
|
2020-02-13 15:51:13 +01:00
|
|
|
<section>
|
2020-02-18 16:39:03 +01:00
|
|
|
<Button type="button" color="cancel" onClick={onToggle}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
</Button>
|
2020-02-19 09:27:06 +01:00
|
|
|
{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-02-13 15:51:13 +01:00
|
|
|
</section>
|
2020-02-18 16:39:03 +01:00
|
|
|
</ModalFooter>
|
2020-02-13 15:51:13 +01:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
ModalStepper.defaultProps = {
|
2020-02-13 15:51:13 +01:00
|
|
|
onToggle: () => {},
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
ModalStepper.propTypes = {
|
2020-02-13 15:51:13 +01:00
|
|
|
isOpen: PropTypes.bool.isRequired,
|
|
|
|
onToggle: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2020-02-18 15:35:50 +01:00
|
|
|
export default ModalStepper;
|