2021-03-20 16:51:34 +01:00
|
|
|
import React, { useState } from 'react';
|
2021-03-20 15:07:10 +01:00
|
|
|
import styled from 'styled-components';
|
2021-11-20 19:06:01 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-03-21 17:55:14 +01:00
|
|
|
import { isEmpty } from 'lodash';
|
2021-10-27 13:28:06 +02:00
|
|
|
import { Button } from '@strapi/design-system/Button';
|
2021-11-20 19:06:01 +01:00
|
|
|
import { Map } from 'immutable';
|
2021-11-20 19:51:03 +01:00
|
|
|
import { useNotification } from '@strapi/helper-plugin';
|
2021-10-14 17:13:12 +02:00
|
|
|
|
2021-03-20 16:51:34 +01:00
|
|
|
import ConfirmModal from '../ConfirmModal';
|
|
|
|
import { exportAllConfig, importAllConfig } from '../../state/actions/Config';
|
|
|
|
|
2021-11-20 19:06:01 +01:00
|
|
|
const ActionButtons = () => {
|
2021-03-20 16:51:34 +01:00
|
|
|
const dispatch = useDispatch();
|
2021-11-20 19:51:03 +01:00
|
|
|
const toggleNotification = useNotification();
|
2021-03-20 16:51:34 +01:00
|
|
|
const [modalIsOpen, setModalIsOpen] = useState(false);
|
|
|
|
const [actionType, setActionType] = useState('');
|
2021-11-20 19:06:01 +01:00
|
|
|
const partialDiff = useSelector((state) => state.getIn(['config', 'partialDiff'], Map({}))).toJS();
|
2021-03-20 16:51:34 +01:00
|
|
|
|
|
|
|
const closeModal = () => {
|
|
|
|
setActionType('');
|
|
|
|
setModalIsOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const openModal = (type) => {
|
|
|
|
setActionType(type);
|
|
|
|
setModalIsOpen(true);
|
|
|
|
};
|
2021-03-20 15:07:10 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ActionButtonsStyling>
|
2021-11-20 19:06:01 +01:00
|
|
|
<Button disabled={isEmpty(partialDiff)} onClick={() => openModal('import')}>Import</Button>
|
|
|
|
<Button disabled={isEmpty(partialDiff)} onClick={() => openModal('export')}>Export</Button>
|
|
|
|
{!isEmpty(partialDiff) && (
|
|
|
|
<h4 style={{ display: 'inline' }}>{Object.keys(partialDiff).length} {Object.keys(partialDiff).length === 1 ? "config change" : "config changes"}</h4>
|
2021-03-26 19:53:18 +01:00
|
|
|
)}
|
2021-03-20 16:51:34 +01:00
|
|
|
<ConfirmModal
|
|
|
|
isOpen={modalIsOpen}
|
|
|
|
onClose={closeModal}
|
2021-10-14 17:13:12 +02:00
|
|
|
type={actionType}
|
2021-11-20 19:51:03 +01:00
|
|
|
onSubmit={() => actionType === 'import' ? dispatch(importAllConfig(partialDiff, toggleNotification)) : dispatch(exportAllConfig(partialDiff, toggleNotification))}
|
2021-03-20 16:51:34 +01:00
|
|
|
/>
|
2021-03-20 15:07:10 +01:00
|
|
|
</ActionButtonsStyling>
|
|
|
|
);
|
2021-10-14 17:13:12 +02:00
|
|
|
};
|
2021-03-20 15:07:10 +01:00
|
|
|
|
|
|
|
const ActionButtonsStyling = styled.div`
|
|
|
|
padding: 10px 0 20px 0;
|
2021-11-13 14:15:31 +01:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2021-03-20 15:07:10 +01:00
|
|
|
|
|
|
|
> button {
|
|
|
|
margin-right: 10px;
|
|
|
|
}
|
|
|
|
`;
|
2021-10-14 17:13:12 +02:00
|
|
|
|
|
|
|
export default ActionButtons;
|