2021-03-21 17:55:14 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-03-20 14:42:15 +01:00
|
|
|
import { Table } from '@buffetjs/core';
|
2021-03-24 18:41:03 +01:00
|
|
|
import { isEmpty } from 'lodash';
|
2021-03-20 14:42:15 +01:00
|
|
|
import ConfigDiff from '../ConfigDiff';
|
2021-03-24 22:03:30 +01:00
|
|
|
import FirstExport from '../FirstExport';
|
2021-03-27 16:16:14 +01:00
|
|
|
import ConfigListRow from './ConfigListRow';
|
2021-03-20 14:42:15 +01:00
|
|
|
|
|
|
|
const headers = [
|
|
|
|
{
|
|
|
|
name: 'Config name',
|
2021-03-20 16:51:34 +01:00
|
|
|
value: 'config_name',
|
2021-03-20 14:42:15 +01:00
|
|
|
},
|
|
|
|
{
|
2021-03-24 18:41:03 +01:00
|
|
|
name: 'Config type',
|
|
|
|
value: 'config_type',
|
2021-03-20 14:42:15 +01:00
|
|
|
},
|
|
|
|
{
|
2021-03-27 16:16:14 +01:00
|
|
|
name: 'State',
|
|
|
|
value: 'state',
|
2021-03-20 14:42:15 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-03-24 18:41:03 +01:00
|
|
|
const ConfigList = ({ diff, isLoading }) => {
|
2021-03-20 14:42:15 +01:00
|
|
|
const [openModal, setOpenModal] = useState(false);
|
|
|
|
const [originalConfig, setOriginalConfig] = useState({});
|
|
|
|
const [newConfig, setNewConfig] = useState({});
|
|
|
|
const [configName, setConfigName] = useState('');
|
2021-03-21 17:55:14 +01:00
|
|
|
const [rows, setRows] = useState([]);
|
2021-03-20 14:42:15 +01:00
|
|
|
|
2021-03-27 16:16:14 +01:00
|
|
|
const getConfigState = (configName) => {
|
|
|
|
if (
|
|
|
|
diff.fileConfig[configName] &&
|
|
|
|
diff.databaseConfig[configName]
|
|
|
|
) {
|
|
|
|
return 'Different'
|
|
|
|
} else if (
|
|
|
|
diff.fileConfig[configName] &&
|
|
|
|
!diff.databaseConfig[configName]
|
|
|
|
) {
|
|
|
|
return 'Only in sync dir'
|
|
|
|
} else if (
|
|
|
|
!diff.fileConfig[configName] &&
|
|
|
|
diff.databaseConfig[configName]
|
|
|
|
) {
|
|
|
|
return 'Only in DB'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-21 17:55:14 +01:00
|
|
|
useEffect(() => {
|
2021-03-24 22:03:30 +01:00
|
|
|
if (isEmpty(diff.diff)) {
|
2021-03-24 19:17:07 +01:00
|
|
|
setRows([]);
|
2021-03-24 18:41:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:55:14 +01:00
|
|
|
let formattedRows = [];
|
2021-03-27 00:54:41 +01:00
|
|
|
Object.keys(diff.diff).map((configName) => {
|
2021-03-24 18:41:03 +01:00
|
|
|
const type = configName.split('.')[0]; // Grab the first part of the filename.
|
2021-03-25 20:57:58 +01:00
|
|
|
const name = configName.split(/\.(.+)/)[1]; // Grab the rest of the filename minus the file extension.
|
2021-03-24 18:41:03 +01:00
|
|
|
|
2021-03-21 17:55:14 +01:00
|
|
|
formattedRows.push({
|
2021-03-24 18:41:03 +01:00
|
|
|
config_name: name,
|
|
|
|
config_type: type,
|
2021-03-27 16:16:14 +01:00
|
|
|
state: getConfigState(configName),
|
|
|
|
onClick: (config_type, config_name) => {
|
|
|
|
setOriginalConfig(diff.fileConfig[`${config_type}.${config_name}`]);
|
|
|
|
setNewConfig(diff.databaseConfig[`${config_type}.${config_name}`]);
|
|
|
|
setConfigName(`${config_type}.${config_name}`);
|
|
|
|
setOpenModal(true);
|
|
|
|
}
|
2021-03-21 17:55:14 +01:00
|
|
|
});
|
2021-03-20 16:51:34 +01:00
|
|
|
});
|
2021-03-24 18:41:03 +01:00
|
|
|
|
2021-03-21 17:55:14 +01:00
|
|
|
setRows(formattedRows);
|
|
|
|
}, [diff]);
|
|
|
|
|
2021-03-20 14:42:15 +01:00
|
|
|
const closeModal = () => {
|
|
|
|
setOriginalConfig({});
|
|
|
|
setNewConfig({});
|
|
|
|
setConfigName('');
|
|
|
|
setOpenModal(false);
|
|
|
|
};
|
|
|
|
|
2021-03-24 22:03:30 +01:00
|
|
|
if (!isLoading && !isEmpty(diff.message)) {
|
|
|
|
return <FirstExport />
|
|
|
|
}
|
|
|
|
|
2021-03-20 14:42:15 +01:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<ConfigDiff
|
|
|
|
isOpen={openModal}
|
|
|
|
oldValue={originalConfig}
|
|
|
|
newValue={newConfig}
|
|
|
|
onClose={closeModal}
|
|
|
|
onToggle={closeModal}
|
|
|
|
configName={configName}
|
|
|
|
/>
|
2021-03-20 16:51:34 +01:00
|
|
|
<Table
|
2021-03-20 14:42:15 +01:00
|
|
|
headers={headers}
|
2021-03-27 16:16:14 +01:00
|
|
|
customRow={ConfigListRow}
|
2021-03-20 16:51:34 +01:00
|
|
|
rows={!isLoading ? rows : []}
|
|
|
|
isLoading={isLoading}
|
2021-03-21 17:55:14 +01:00
|
|
|
tableEmptyText="No config changes. You are up to date!"
|
2021-03-20 14:42:15 +01:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConfigList;
|