strapi-plugin-config-sync/admin/src/components/ConfigList/index.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-03-21 17:55:14 +01:00
import React, { useState, useEffect } from 'react';
import { Table } from '@buffetjs/core';
2021-03-24 18:41:03 +01:00
import { isEmpty } from 'lodash';
import ConfigDiff from '../ConfigDiff';
const headers = [
{
name: 'Config name',
2021-03-20 16:51:34 +01:00
value: 'config_name',
},
{
2021-03-24 18:41:03 +01:00
name: 'Config type',
value: 'config_type',
},
{
name: 'Change',
value: 'change_type',
},
];
2021-03-24 18:41:03 +01:00
const ConfigList = ({ diff, isLoading }) => {
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-21 17:55:14 +01:00
useEffect(() => {
2021-03-24 18:41:03 +01:00
if (isEmpty(diff)) {
return;
}
2021-03-21 17:55:14 +01:00
let formattedRows = [];
2021-03-24 18:41:03 +01:00
Object.keys(diff.fileConfig).map((configName) => {
const type = configName.split('.')[0]; // Grab the first part of the filename.
const name = configName.split(/\.(.+)/)[1].split('.')[0] // Grab the rest of the filename minus the file extension.
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-21 17:55:14 +01:00
change_type: ''
});
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]);
const closeModal = () => {
setOriginalConfig({});
setNewConfig({});
setConfigName('');
setOpenModal(false);
};
return (
<div>
<ConfigDiff
isOpen={openModal}
oldValue={originalConfig}
newValue={newConfig}
onClose={closeModal}
onToggle={closeModal}
configName={configName}
/>
2021-03-20 16:51:34 +01:00
<Table
headers={headers}
2021-03-24 18:41:03 +01:00
onClickRow={(e, { 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-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!"
/>
</div>
);
}
export default ConfigList;