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';
|
|
|
|
import {
|
|
|
|
HeaderModal,
|
|
|
|
HeaderModalTitle,
|
|
|
|
Modal,
|
|
|
|
ModalFooter,
|
2020-02-18 16:39:03 +01:00
|
|
|
useGlobalContext,
|
2020-02-13 15:51:13 +01:00
|
|
|
} from 'strapi-helper-plugin';
|
|
|
|
import { Button } from '@buffetjs/core';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
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
|
|
|
|
|
|
|
const handleClosed = () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'RESET_PROPS',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-18 18:05:52 +01:00
|
|
|
// FIXME: when back button needed
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const goBack = () => {
|
|
|
|
goTo(prev);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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-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;
|