2021-03-21 17:55:14 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-03-24 18:41:03 +01:00
|
|
|
import { isEmpty } from 'lodash';
|
2021-10-14 17:13:12 +02:00
|
|
|
|
2021-10-14 17:39:53 +02:00
|
|
|
import { Table, Thead, Tbody, Tr, Th } from '@strapi/parts/Table';
|
2021-10-14 17:13:12 +02:00
|
|
|
import { TableLabel } from '@strapi/parts/Text';
|
|
|
|
|
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
|
|
|
|
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({});
|
2021-10-14 17:39:53 +02:00
|
|
|
const [cName, setCname] = 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 (
|
2021-10-14 17:39:53 +02:00
|
|
|
diff.fileConfig[configName]
|
|
|
|
&& diff.databaseConfig[configName]
|
2021-03-27 16:16:14 +01:00
|
|
|
) {
|
2021-10-14 17:39:53 +02:00
|
|
|
return 'Different';
|
2021-03-27 16:16:14 +01:00
|
|
|
} else if (
|
2021-10-14 17:39:53 +02:00
|
|
|
diff.fileConfig[configName]
|
|
|
|
&& !diff.databaseConfig[configName]
|
2021-03-27 16:16:14 +01:00
|
|
|
) {
|
2021-10-14 17:39:53 +02:00
|
|
|
return 'Only in sync dir';
|
2021-03-27 16:16:14 +01:00
|
|
|
} else if (
|
2021-10-14 17:39:53 +02:00
|
|
|
!diff.fileConfig[configName]
|
|
|
|
&& diff.databaseConfig[configName]
|
2021-03-27 16:16:14 +01:00
|
|
|
) {
|
2021-10-14 17:39:53 +02:00
|
|
|
return 'Only in DB';
|
2021-03-27 16:16:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-10-14 17:39:53 +02:00
|
|
|
const formattedRows = [];
|
|
|
|
Object.keys(diff.diff).map((name) => {
|
|
|
|
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.
|
2021-03-24 18:41:03 +01:00
|
|
|
|
2021-10-14 17:13:12 +02:00
|
|
|
formattedRows.push({
|
2021-10-14 17:39:53 +02:00
|
|
|
configName: formattedName,
|
|
|
|
configType: type,
|
|
|
|
state: getConfigState(name),
|
|
|
|
onClick: (configType, configName) => {
|
|
|
|
setOriginalConfig(diff.fileConfig[`${configType}.${configName}`]);
|
|
|
|
setNewConfig(diff.databaseConfig[`${configType}.${configName}`]);
|
|
|
|
setCname(`${configType}.${configName}`);
|
2021-03-27 16:16:14 +01:00
|
|
|
setOpenModal(true);
|
2021-10-14 17:39:53 +02:00
|
|
|
},
|
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-10-14 17:13:12 +02:00
|
|
|
|
2021-03-20 14:42:15 +01:00
|
|
|
const closeModal = () => {
|
|
|
|
setOriginalConfig({});
|
|
|
|
setNewConfig({});
|
2021-10-14 17:39:53 +02:00
|
|
|
setCname('');
|
2021-03-20 14:42:15 +01:00
|
|
|
setOpenModal(false);
|
|
|
|
};
|
|
|
|
|
2021-03-24 22:03:30 +01:00
|
|
|
if (!isLoading && !isEmpty(diff.message)) {
|
2021-10-14 17:13:12 +02:00
|
|
|
return <FirstExport />;
|
2021-03-24 22:03:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 14:42:15 +01:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<ConfigDiff
|
|
|
|
isOpen={openModal}
|
|
|
|
oldValue={originalConfig}
|
|
|
|
newValue={newConfig}
|
|
|
|
onClose={closeModal}
|
2021-10-14 17:39:53 +02:00
|
|
|
configName={cName}
|
2021-03-20 14:42:15 +01:00
|
|
|
/>
|
2021-10-14 17:13:12 +02:00
|
|
|
<Table colCount={4} rowCount={rows.length + 1}>
|
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>Config name</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>Config type</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>State</TableLabel>
|
|
|
|
</Th>
|
|
|
|
</Tr>
|
|
|
|
</Thead>
|
|
|
|
<Tbody>
|
|
|
|
{rows.map((row) => (
|
2021-10-14 17:33:21 +02:00
|
|
|
<ConfigListRow key={row.name} row={row} />
|
2021-10-14 17:13:12 +02:00
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
2021-03-20 14:42:15 +01:00
|
|
|
</div>
|
|
|
|
);
|
2021-10-14 17:13:12 +02:00
|
|
|
};
|
2021-03-20 14:42:15 +01:00
|
|
|
|
2021-10-14 17:13:12 +02:00
|
|
|
export default ConfigList;
|