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

114 lines
2.9 KiB
JavaScript
Raw Normal View History

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
import { Table, Thead, Tbody, Tr, Th } from '@strapi/design-system/Table';
import { TableLabel } from '@strapi/design-system/Text';
2021-10-14 17:13:12 +02:00
import ConfigDiff from '../ConfigDiff';
2021-03-24 22:03:30 +01:00
import FirstExport from '../FirstExport';
2021-11-13 14:30:16 +01:00
import NoChanges from '../NoChanges';
2021-03-27 16:16:14 +01:00
import ConfigListRow from './ConfigListRow';
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({});
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-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
const closeModal = () => {
setOriginalConfig({});
setNewConfig({});
2021-10-14 17:39:53 +02:00
setCname('');
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-11-13 14:30:16 +01:00
if (!isLoading && isEmpty(diff.diff)) {
return <NoChanges />;
}
return (
<div>
<ConfigDiff
isOpen={openModal}
oldValue={originalConfig}
newValue={newConfig}
onClose={closeModal}
2021-10-14 17:39:53 +02:00
configName={cName}
/>
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>
</div>
);
2021-10-14 17:13:12 +02:00
};
2021-10-14 17:13:12 +02:00
export default ConfigList;