2020-02-18 16:39:03 +01:00
|
|
|
import React, { useReducer } 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-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];
|
|
|
|
|
|
|
|
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 09:14:11 +01:00
|
|
|
const handleUploadFiles = async () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SET_FILES_UPLOADING_STATE',
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const requests = filesToUpload.map(({ file, abortController }) => {
|
|
|
|
const formData = new FormData();
|
|
|
|
const headers = {};
|
|
|
|
formData.append('files', file);
|
|
|
|
|
|
|
|
return request(
|
|
|
|
`/${pluginId}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
body: formData,
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(requests);
|
|
|
|
|
|
|
|
goNext();
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-18 18:05:52 +01:00
|
|
|
// FIXME: when back button needed
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
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-02-13 15:51:13 +01:00
|
|
|
return (
|
2020-02-17 08:56:28 +01:00
|
|
|
<Modal
|
|
|
|
isOpen={isOpen}
|
|
|
|
onToggle={onToggle}
|
|
|
|
// TODO: reset to initialState
|
2020-02-18 16:39:03 +01:00
|
|
|
onClosed={handleClosed}
|
2020-02-17 08:56:28 +01:00
|
|
|
>
|
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-18 18:37:14 +01:00
|
|
|
onGoToAddBrowseFiles={goBack}
|
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:14:11 +01:00
|
|
|
<Button type="button" color="success" onClick={handleUploadFiles}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
</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;
|