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

168 lines
4.8 KiB
React
Raw Normal View History

2021-03-21 17:55:14 +01:00
import React, { useState, useEffect } from 'react';
2023-10-15 03:53:57 +02:00
import { useIntl } from 'react-intl';
2021-03-24 18:41:03 +01:00
import { isEmpty } from 'lodash';
2021-11-20 19:06:01 +01:00
import { useDispatch } from 'react-redux';
2021-10-14 17:13:12 +02:00
import {
Table,
Thead,
Tbody,
Tr,
Th,
Typography,
BaseCheckbox,
Loader,
} from '@strapi/design-system';
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-11-20 19:06:01 +01:00
import { setConfigPartialDiffInState } from '../../state/actions/Config';
2023-10-11 20:46:51 +02:00
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-11-20 19:06:01 +01:00
const [checkedItems, setCheckedItems] = useState([]);
const dispatch = useDispatch();
2023-10-11 20:46:51 +02:00
const { formatMessage } = useIntl();
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
) {
2023-10-11 20:46:51 +02:00
return formatMessage({ id: 'config-sync.ConfigList.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
) {
2023-10-11 20:46:51 +02:00
return formatMessage({ id: 'config-sync.ConfigList.OnlyDir' });
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
) {
2023-10-11 20:46:51 +02:00
return formatMessage({ id: 'config-sync.ConfigList.OnlyDB' });
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 = [];
2021-11-20 19:51:03 +01:00
const newCheckedItems = [];
2021-10-14 17:39:53 +02:00
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-11-20 19:51:03 +01:00
newCheckedItems.push(true);
2021-11-20 19:06:01 +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-11-20 19:51:03 +01:00
setCheckedItems(newCheckedItems);
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-11-20 19:06:01 +01:00
useEffect(() => {
const newPartialDiff = [];
checkedItems.map((item, index) => {
if (item && rows[index]) newPartialDiff.push(`${rows[index].configType}.${rows[index].configName}`);
});
dispatch(setConfigPartialDiffInState(newPartialDiff));
}, [checkedItems]);
const closeModal = () => {
setOriginalConfig({});
setNewConfig({});
2021-10-14 17:39:53 +02:00
setCname('');
setOpenModal(false);
};
2021-11-20 20:29:33 +01:00
if (isLoading) {
return (
<div style={{ textAlign: 'center', marginTop: 40 }}>
2023-10-11 20:46:51 +02:00
<Loader>{formatMessage({ id: 'config-sync.ConfigList.Loading' })}</Loader>
2021-11-20 20:29:33 +01:00
</div>
);
}
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 />;
}
2021-11-20 19:06:01 +01:00
const allChecked = checkedItems && checkedItems.every(Boolean);
const isIndeterminate = checkedItems.some(Boolean) && !allChecked;
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>
2021-11-20 19:06:01 +01:00
<Th>
<BaseCheckbox
2023-10-11 20:46:51 +02:00
aria-label={formatMessage({ id: 'config-sync.ConfigList.SelectAll' })}
2021-11-20 19:06:01 +01:00
indeterminate={isIndeterminate}
onValueChange={(value) => setCheckedItems(checkedItems.map(() => value))}
value={allChecked}
/>
</Th>
2021-10-14 17:13:12 +02:00
<Th>
2023-10-11 20:46:51 +02:00
<Typography variant="sigma">{formatMessage({ id: 'config-sync.ConfigList.ConfigName' })}</Typography>
2021-10-14 17:13:12 +02:00
</Th>
<Th>
2023-10-11 20:46:51 +02:00
<Typography variant="sigma">{formatMessage({ id: 'config-sync.ConfigList.ConfigType' })}</Typography>
2021-10-14 17:13:12 +02:00
</Th>
<Th>
2023-10-11 20:46:51 +02:00
<Typography variant="sigma">{formatMessage({ id: 'config-sync.ConfigList.State' })}</Typography>
2021-10-14 17:13:12 +02:00
</Th>
</Tr>
</Thead>
<Tbody>
2021-11-20 19:06:01 +01:00
{rows.map((row, index) => (
<ConfigListRow
key={row.configName}
row={row}
checked={checkedItems[index]}
updateValue={() => {
checkedItems[index] = !checkedItems[index];
setCheckedItems([...checkedItems]);
}}
/>
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;