From 5f21ac9fc56919dc61aaba495bbc330547855d35 Mon Sep 17 00:00:00 2001 From: Boaz Poolman Date: Sat, 20 Mar 2021 14:42:15 +0100 Subject: [PATCH] List all config changes & show diff on click --- admin/src/components/ConfigDiff/index.js | 49 ++++++++++++++++++ admin/src/components/ConfigList/index.js | 66 ++++++++++++++++++++++++ admin/src/containers/App/index.js | 8 +-- admin/src/containers/ConfigPage/index.js | 13 ++--- admin/src/helpers/getObjectDiff.js | 22 ++++++++ package.json | 1 - 6 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 admin/src/components/ConfigDiff/index.js create mode 100644 admin/src/components/ConfigList/index.js create mode 100644 admin/src/helpers/getObjectDiff.js diff --git a/admin/src/components/ConfigDiff/index.js b/admin/src/components/ConfigDiff/index.js new file mode 100644 index 0000000..05f0f50 --- /dev/null +++ b/admin/src/components/ConfigDiff/index.js @@ -0,0 +1,49 @@ +import React from 'react'; +import ReactDiffViewer, { DiffMethod } from 'react-diff-viewer'; +import { AttributeIcon } from '@buffetjs/core'; +import { + HeaderModal, + HeaderModalTitle, + Modal, + ModalBody, + ModalFooter, +} from 'strapi-helper-plugin'; + +const ConfigDiff = ({ isOpen, onClose, onToggle, oldValue, newValue, configName }) => { + return ( + + +
+ + Config changes for {configName} +
+
+ +
+
+ +
+
+
+ +
+ +
+
+
+ ); +} + +export default ConfigDiff; \ No newline at end of file diff --git a/admin/src/components/ConfigList/index.js b/admin/src/components/ConfigList/index.js new file mode 100644 index 0000000..4d6064b --- /dev/null +++ b/admin/src/components/ConfigList/index.js @@ -0,0 +1,66 @@ +import React, { useState } from 'react'; +import { Table } from '@buffetjs/core'; +import difference from '../../helpers/getObjectDiff'; +import ConfigDiff from '../ConfigDiff'; + +const headers = [ + { + name: 'Id', + value: 'id', + }, + { + name: 'Config name', + value: 'name', + }, + { + name: 'Database table', + value: 'lastname', + }, + { + name: 'Change', + value: 'change_type', + }, +]; + +const ConfigList = ({ fileConfig, databaseConfig }) => { + const diff = difference(fileConfig.toJS(), databaseConfig.toJS()); + const [openModal, setOpenModal] = useState(false); + const [originalConfig, setOriginalConfig] = useState({}); + const [newConfig, setNewConfig] = useState({}); + const [configName, setConfigName] = useState(''); + let rows = []; + + Object.keys(diff).map((config) => rows.push({ name: config })); + + const closeModal = () => { + setOriginalConfig({}); + setNewConfig({}); + setConfigName(''); + setOpenModal(false); + }; + + return ( +
+ + { + setOriginalConfig(fileConfig.get(data.name)); + setNewConfig(databaseConfig.get(data.name)); + setConfigName(data.name); + setOpenModal(true); + }} + rows={rows} + /> + + ); +} + +export default ConfigList; \ No newline at end of file diff --git a/admin/src/containers/App/index.js b/admin/src/containers/App/index.js index 7ff148f..a268bf4 100644 --- a/admin/src/containers/App/index.js +++ b/admin/src/containers/App/index.js @@ -6,21 +6,15 @@ */ import React from 'react'; -import { Switch, Route } from 'react-router-dom'; -import { NotFound } from 'strapi-helper-plugin'; import { Provider } from 'react-redux'; import { store } from "../../helpers/configureStore"; -import pluginId from '../../helpers/pluginId'; import ConfigPage from '../ConfigPage'; const App = () => { return ( - - - - + ); }; diff --git a/admin/src/containers/ConfigPage/index.js b/admin/src/containers/ConfigPage/index.js index ef7e273..bd10e8f 100644 --- a/admin/src/containers/ConfigPage/index.js +++ b/admin/src/containers/ConfigPage/index.js @@ -1,9 +1,9 @@ import React, { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import ReactDiffViewer from 'react-diff-viewer'; import { Map } from 'immutable'; import { getAllDatabaseConfig, getAllFileConfig } from '../../state/actions/Config'; +import ConfigList from '../../components/ConfigList'; const ConfigPage = () => { const dispatch = useDispatch(); @@ -15,17 +15,14 @@ const ConfigPage = () => { dispatch(getAllFileConfig()); }, []); - if (!fileConfig || !databaseConfig) { + if (fileConfig.size === 0 || databaseConfig.size === 0) { return null; } return ( - +
+ +
); } diff --git a/admin/src/helpers/getObjectDiff.js b/admin/src/helpers/getObjectDiff.js new file mode 100644 index 0000000..d693c1a --- /dev/null +++ b/admin/src/helpers/getObjectDiff.js @@ -0,0 +1,22 @@ +import { transform, isEqual, isArray, isObject } from 'lodash'; + +/** + * Find difference between two objects + * @param {object} origObj - Source object to compare newObj against + * @param {object} newObj - New object with potential changes + * @return {object} differences + */ +const difference = (origObj, newObj) => { + function changes(newObj, origObj) { + let arrayIndexCounter = 0 + return transform(newObj, function (result, value, key) { + if (!isEqual(value, origObj[key])) { + let resultKey = isArray(origObj) ? arrayIndexCounter++ : key + result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value + } + }) + } + return changes(newObj, origObj) +} + +export default difference; \ No newline at end of file diff --git a/package.json b/package.json index 631ac35..b1fa504 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "react-diff-viewer": "^3.1.1", "react-dom": "^17.0.1", "react-redux": "^7.2.2", - "react-router-dom": "^5.2.0", "redux": "^4.0.5", "redux-immutable": "^4.0.0", "redux-logger": "^3.0.6",