feat: Partial export/import in admin

pull/28/head
Boaz Poolman 2021-11-20 19:06:01 +01:00
parent 187a090866
commit 17c68c2c36
8 changed files with 111 additions and 29 deletions

View File

@ -1,16 +1,18 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { useDispatch } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { isEmpty } from 'lodash'; import { isEmpty } from 'lodash';
import { Button } from '@strapi/design-system/Button'; import { Button } from '@strapi/design-system/Button';
import { Map } from 'immutable';
import ConfirmModal from '../ConfirmModal'; import ConfirmModal from '../ConfirmModal';
import { exportAllConfig, importAllConfig } from '../../state/actions/Config'; import { exportAllConfig, importAllConfig } from '../../state/actions/Config';
const ActionButtons = ({ diff }) => { const ActionButtons = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const [modalIsOpen, setModalIsOpen] = useState(false); const [modalIsOpen, setModalIsOpen] = useState(false);
const [actionType, setActionType] = useState(''); const [actionType, setActionType] = useState('');
const partialDiff = useSelector((state) => state.getIn(['config', 'partialDiff'], Map({}))).toJS();
const closeModal = () => { const closeModal = () => {
setActionType(''); setActionType('');
@ -24,16 +26,16 @@ const ActionButtons = ({ diff }) => {
return ( return (
<ActionButtonsStyling> <ActionButtonsStyling>
<Button disabled={isEmpty(diff.diff)} onClick={() => openModal('import')}>Import</Button> <Button disabled={isEmpty(partialDiff)} onClick={() => openModal('import')}>Import</Button>
<Button disabled={isEmpty(diff.diff)} onClick={() => openModal('export')}>Export</Button> <Button disabled={isEmpty(partialDiff)} onClick={() => openModal('export')}>Export</Button>
{!isEmpty(diff.diff) && ( {!isEmpty(partialDiff) && (
<h4 style={{ display: 'inline' }}>{Object.keys(diff.diff).length} {Object.keys(diff.diff).length === 1 ? "config change" : "config changes"}</h4> <h4 style={{ display: 'inline' }}>{Object.keys(partialDiff).length} {Object.keys(partialDiff).length === 1 ? "config change" : "config changes"}</h4>
)} )}
<ConfirmModal <ConfirmModal
isOpen={modalIsOpen} isOpen={modalIsOpen}
onClose={closeModal} onClose={closeModal}
type={actionType} type={actionType}
onSubmit={() => actionType === 'import' ? dispatch(importAllConfig()) : dispatch(exportAllConfig())} onSubmit={() => actionType === 'import' ? dispatch(importAllConfig(partialDiff)) : dispatch(exportAllConfig(partialDiff))}
/> />
</ActionButtonsStyling> </ActionButtonsStyling>
); );

View File

@ -1,7 +1,8 @@
import React from 'react'; import React from 'react';
import { Tr, Td } from '@strapi/design-system/Table'; import { Tr, Td } from '@strapi/design-system/Table';
import { BaseCheckbox } from '@strapi/design-system/BaseCheckbox';
const CustomRow = ({ row }) => { const CustomRow = ({ row, checked, updateValue }) => {
const { configName, configType, state, onClick } = row; const { configName, configType, state, onClick } = row;
const stateStyle = (stateStr) => { const stateStyle = (stateStr) => {
@ -34,9 +35,20 @@ const CustomRow = ({ row }) => {
return ( return (
<Tr <Tr
onClick={() => onClick(configType, configName)} onClick={(e) => {
if (e.target.type !== 'checkbox') {
onClick(configType, configName);
}
}}
style={{ cursor: 'pointer' }} style={{ cursor: 'pointer' }}
> >
<Td>
<BaseCheckbox
aria-label={`Select ${configName}`}
value={checked}
onValueChange={updateValue}
/>
</Td>
<Td> <Td>
<p>{configName}</p> <p>{configName}</p>
</Td> </Td>

View File

@ -1,13 +1,16 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { isEmpty } from 'lodash'; import { isEmpty } from 'lodash';
import { useDispatch } from 'react-redux';
import { Table, Thead, Tbody, Tr, Th } from '@strapi/design-system/Table'; import { Table, Thead, Tbody, Tr, Th } from '@strapi/design-system/Table';
import { TableLabel } from '@strapi/design-system/Text'; import { TableLabel } from '@strapi/design-system/Text';
import { BaseCheckbox } from '@strapi/design-system/BaseCheckbox';
import ConfigDiff from '../ConfigDiff'; import ConfigDiff from '../ConfigDiff';
import FirstExport from '../FirstExport'; import FirstExport from '../FirstExport';
import NoChanges from '../NoChanges'; import NoChanges from '../NoChanges';
import ConfigListRow from './ConfigListRow'; import ConfigListRow from './ConfigListRow';
import { setConfigPartialDiffInState } from '../../state/actions/Config';
const ConfigList = ({ diff, isLoading }) => { const ConfigList = ({ diff, isLoading }) => {
const [openModal, setOpenModal] = useState(false); const [openModal, setOpenModal] = useState(false);
@ -15,6 +18,8 @@ const ConfigList = ({ diff, isLoading }) => {
const [newConfig, setNewConfig] = useState({}); const [newConfig, setNewConfig] = useState({});
const [cName, setCname] = useState(''); const [cName, setCname] = useState('');
const [rows, setRows] = useState([]); const [rows, setRows] = useState([]);
const [checkedItems, setCheckedItems] = useState([]);
const dispatch = useDispatch();
const getConfigState = (configName) => { const getConfigState = (configName) => {
if ( if (
@ -46,6 +51,8 @@ const ConfigList = ({ diff, isLoading }) => {
const type = name.split('.')[0]; // Grab the first part of the filename. const type = name.split('.')[0]; // Grab the first part of the filename.
const formattedName = name.split(/\.(.+)/)[1]; // Grab the rest of the filename minus the file extension. const formattedName = name.split(/\.(.+)/)[1]; // Grab the rest of the filename minus the file extension.
setCheckedItems(checkedItems.concat(true));
formattedRows.push({ formattedRows.push({
configName: formattedName, configName: formattedName,
configType: type, configType: type,
@ -62,6 +69,14 @@ const ConfigList = ({ diff, isLoading }) => {
setRows(formattedRows); setRows(formattedRows);
}, [diff]); }, [diff]);
useEffect(() => {
const newPartialDiff = [];
checkedItems.map((item, index) => {
if (item && rows[index]) newPartialDiff.push(`${rows[index].configType}.${rows[index].configName}`);
});
dispatch(setConfigPartialDiffInState(newPartialDiff));
}, [checkedItems]);
const closeModal = () => { const closeModal = () => {
setOriginalConfig({}); setOriginalConfig({});
setNewConfig({}); setNewConfig({});
@ -77,6 +92,9 @@ const ConfigList = ({ diff, isLoading }) => {
return <NoChanges />; return <NoChanges />;
} }
const allChecked = checkedItems && checkedItems.every(Boolean);
const isIndeterminate = checkedItems.some(Boolean) && !allChecked;
return ( return (
<div> <div>
<ConfigDiff <ConfigDiff
@ -89,6 +107,14 @@ const ConfigList = ({ diff, isLoading }) => {
<Table colCount={4} rowCount={rows.length + 1}> <Table colCount={4} rowCount={rows.length + 1}>
<Thead> <Thead>
<Tr> <Tr>
<Th>
<BaseCheckbox
aria-label="Select all entries"
indeterminate={isIndeterminate}
onValueChange={(value) => setCheckedItems(checkedItems.map(() => value))}
value={allChecked}
/>
</Th>
<Th> <Th>
<TableLabel>Config name</TableLabel> <TableLabel>Config name</TableLabel>
</Th> </Th>
@ -101,8 +127,16 @@ const ConfigList = ({ diff, isLoading }) => {
</Tr> </Tr>
</Thead> </Thead>
<Tbody> <Tbody>
{rows.map((row) => ( {rows.map((row, index) => (
<ConfigListRow key={row.configName} row={row} /> <ConfigListRow
key={row.configName}
row={row}
checked={checkedItems[index]}
updateValue={() => {
checkedItems[index] = !checkedItems[index];
setCheckedItems([...checkedItems]);
}}
/>
))} ))}
</Tbody> </Tbody>
</Table> </Table>

View File

@ -18,7 +18,7 @@ const ConfigPage = () => {
return ( return (
<Box paddingLeft={8} paddingRight={8}> <Box paddingLeft={8} paddingRight={8}>
<ActionButtons diff={configDiff.toJS()} /> <ActionButtons />
<ConfigList isLoading={isLoading} diff={configDiff.toJS()} /> <ConfigList isLoading={isLoading} diff={configDiff.toJS()} />
</Box> </Box>
); );

View File

@ -29,13 +29,22 @@ export function setConfigDiffInState(config) {
}; };
} }
export function exportAllConfig() { export const SET_CONFIG_PARTIAL_DIFF_IN_STATE = 'SET_CONFIG_PARTIAL_DIFF_IN_STATE';
export function setConfigPartialDiffInState(config) {
return {
type: SET_CONFIG_PARTIAL_DIFF_IN_STATE,
config,
};
}
export function exportAllConfig(partialDiff) {
return async function(dispatch, getState, toggleNotification) { return async function(dispatch, getState, toggleNotification) {
dispatch(setLoadingState(true)); dispatch(setLoadingState(true));
try { try {
const { message } = await request('/config-sync/export', { method: 'GET' }); const { message } = await request('/config-sync/export', {
dispatch(setConfigDiffInState(Map({}))); method: 'POST',
body: partialDiff,
});
toggleNotification({ type: 'success', message }); toggleNotification({ type: 'success', message });
dispatch(setLoadingState(false)); dispatch(setLoadingState(false));
} catch (err) { } catch (err) {
@ -45,13 +54,14 @@ export function exportAllConfig() {
}; };
} }
export function importAllConfig() { export function importAllConfig(partialDiff) {
return async function(dispatch, getState, toggleNotification) { return async function(dispatch, getState, toggleNotification) {
dispatch(setLoadingState(true)); dispatch(setLoadingState(true));
try { try {
const { message } = await request('/config-sync/import', { method: 'GET' }); const { message } = await request('/config-sync/import', {
dispatch(setConfigDiffInState(Map({}))); method: 'POST',
body: partialDiff,
});
toggleNotification({ type: 'success', message }); toggleNotification({ type: 'success', message });
dispatch(setLoadingState(false)); dispatch(setLoadingState(false));
} catch (err) { } catch (err) {

View File

@ -4,11 +4,12 @@
* *
*/ */
import { fromJS, Map } from 'immutable'; import { fromJS, Map, List } from 'immutable';
import { SET_CONFIG_DIFF_IN_STATE, SET_LOADING_STATE } from '../../actions/Config'; import { SET_CONFIG_DIFF_IN_STATE, SET_CONFIG_PARTIAL_DIFF_IN_STATE, SET_LOADING_STATE } from '../../actions/Config';
const initialState = fromJS({ const initialState = fromJS({
configDiff: Map({}), configDiff: Map({}),
partialDiff: List([]),
isLoading: false, isLoading: false,
}); });
@ -16,10 +17,13 @@ export default function configReducer(state = initialState, action) {
switch (action.type) { switch (action.type) {
case SET_CONFIG_DIFF_IN_STATE: case SET_CONFIG_DIFF_IN_STATE:
return state return state
.update('configDiff', () => fromJS(action.config)) .update('configDiff', () => fromJS(action.config));
case SET_CONFIG_PARTIAL_DIFF_IN_STATE:
return state
.update('partialDiff', () => fromJS(action.config));
case SET_LOADING_STATE: case SET_LOADING_STATE:
return state return state
.update('isLoading', () => fromJS(action.value)) .update('isLoading', () => fromJS(action.value));
default: default:
return state; return state;
} }

View File

@ -14,7 +14,17 @@ module.exports = {
* @returns {void} * @returns {void}
*/ */
exportAll: async (ctx) => { exportAll: async (ctx) => {
await strapi.plugin('config-sync').service('main').exportAllConfig(); if (!ctx.request.body) {
ctx.send({
message: 'No config was specified for the export endpoint.',
});
return;
}
await Promise.all(ctx.request.body.map(async (configName) => {
await strapi.plugin('config-sync').service('main').exportSingleConfig(configName);
}));
ctx.send({ ctx.send({
message: `Config was successfully exported to ${strapi.config.get('plugin.config-sync.destination')}.`, message: `Config was successfully exported to ${strapi.config.get('plugin.config-sync.destination')}.`,
@ -37,7 +47,17 @@ module.exports = {
return; return;
} }
await strapi.plugin('config-sync').service('main').importAllConfig(); if (!ctx.request.body) {
ctx.send({
message: 'No config was specified for the export endpoint.',
});
return;
}
await Promise.all(ctx.request.body.map(async (configName) => {
await strapi.plugin('config-sync').service('main').importSingleConfig(configName);
}));
ctx.send({ ctx.send({
message: 'Config was successfully imported.', message: 'Config was successfully imported.',

View File

@ -4,7 +4,7 @@ module.exports = {
type: 'admin', type: 'admin',
routes: [ routes: [
{ {
method: "GET", method: "POST",
path: "/export", path: "/export",
handler: "config.exportAll", handler: "config.exportAll",
config: { config: {
@ -12,7 +12,7 @@ module.exports = {
}, },
}, },
{ {
method: "GET", method: "POST",
path: "/import", path: "/import",
handler: "config.importAll", handler: "config.importAll",
config: { config: {