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

114 lines
3.1 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 { NoContent } from '@strapi/helper-plugin';
import AddIcon from '@strapi/icons/AddIcon';
import { VisuallyHidden } from '@strapi/parts/VisuallyHidden';
import { Table, Thead, Tbody, Tr, Th, TFooter } from '@strapi/parts/Table';
import { TableLabel } from '@strapi/parts/Text';
import { Button } from '@strapi/parts/Button';
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-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-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-10-14 17:13:12 +02: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-10-14 17:13:12 +02:00
const closeModal = () => {
setOriginalConfig({});
setNewConfig({});
setConfigName('');
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
}
return (
<div>
<ConfigDiff
isOpen={openModal}
oldValue={originalConfig}
newValue={newConfig}
onClose={closeModal}
onToggle={closeModal}
configName={configName}
/>
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;