Show correct diff in frontend

pull/5/head
Boaz Poolman 2021-03-27 00:54:41 +01:00
parent b6f1f354ea
commit f692fa5aaa
4 changed files with 16 additions and 5 deletions

View File

@ -23,8 +23,8 @@ const ActionButtons = ({ diff }) => {
return (
<ActionButtonsStyling>
<Button disabled={isEmpty(diff.fileConfig)} color="primary" label="Import" onClick={() => openModal('import')} />
<Button disabled={isEmpty(diff.fileConfig)} color="primary" label="Export" onClick={() => openModal('export')} />
<Button disabled={isEmpty(diff.diff)} color="primary" label="Import" onClick={() => openModal('import')} />
<Button disabled={isEmpty(diff.diff)} color="primary" label="Export" onClick={() => openModal('export')} />
{!isEmpty(diff.diff) && (
<h4 style={{ display: 'inline' }}>{Object.keys(diff.diff).length} {Object.keys(diff.diff).length === 1 ? "config change" : "config changes"}</h4>
)}

View File

@ -33,7 +33,7 @@ const ConfigList = ({ diff, isLoading }) => {
}
let formattedRows = [];
Object.keys(diff.fileConfig).map((configName) => {
Object.keys(diff.diff).map((configName) => {
const type = configName.split('.')[0]; // Grab the first part of the filename.
const name = configName.split(/\.(.+)/)[1]; // Grab the rest of the filename minus the file extension.

View File

@ -70,7 +70,8 @@ module.exports = {
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles();
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase();
const diff = difference(fileConfig, databaseConfig);
const diff = difference(databaseConfig, fileConfig);
formattedDiff.diff = diff;
Object.keys(diff).map((changedConfigName) => {

View File

@ -10,13 +10,23 @@ const { transform, isEqual, isArray, isObject } = require('lodash');
const difference = (origObj, newObj) => {
function changes(newObj, origObj) {
let arrayIndexCounter = 0
return transform(newObj, function (result, value, key) {
const newObjChange = 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
}
});
const origObjChange = transform(origObj, function (result, value, key) {
if (!isEqual(value, newObj[key])) {
let resultKey = isArray(newObj) ? arrayIndexCounter++ : key
result[resultKey] = (isObject(value) && isObject(newObj[key])) ? changes(value, newObj[key]) : value
}
})
return Object.assign(newObjChange, origObjChange);
}
return changes(newObj, origObj)
}