fix customizations Warn user in WebUI of unsaved changes #911

This commit is contained in:
proddy
2023-01-19 15:51:27 +01:00
parent 665c33d1a9
commit 9fe6b9d0e5
2 changed files with 11 additions and 5 deletions

View File

@@ -148,14 +148,14 @@ const SettingsCustomization: FC = () => {
}
}, [LL]);
const setInitialMask = (data: DeviceEntity[]) => {
const setOriginalSettings = (data: DeviceEntity[]) => {
setDeviceEntities(data.map((de) => ({ ...de, o_m: de.m, o_cn: de.cn, o_mi: de.mi, o_ma: de.ma })));
};
const fetchDeviceEntities = async (unique_id: number) => {
try {
const new_deviceEntities = (await EMSESP.readDeviceEntities({ id: unique_id })).data;
setInitialMask(new_deviceEntities);
setOriginalSettings(new_deviceEntities);
} catch (error) {
setErrorMessage(extractErrorMessage(error, LL.PROBLEM_LOADING()));
}
@@ -249,13 +249,17 @@ const SettingsCustomization: FC = () => {
}
};
function hasEntityChanged(de: DeviceEntity) {
return (de?.cn || '') !== (de?.o_cn || '') || de.m !== de.o_m || de.ma !== de.o_ma || de.mi !== de.o_mi;
}
const getChanges = () => {
if (!deviceEntities || selectedDevice === -1) {
return [];
}
return deviceEntities
.filter((de) => de.m !== de.o_m || de.cn !== de.o_cn || de.ma !== de.o_ma || de.mi !== de.o_mi)
.filter((de) => hasEntityChanged(de))
.map(
(new_de) =>
new_de.m.toString(16).padStart(2, '0') +
@@ -291,7 +295,7 @@ const SettingsCustomization: FC = () => {
} catch (error) {
enqueueSnackbar(extractErrorMessage(error, LL.PROBLEM_UPDATING()), { variant: 'error' });
}
setInitialMask(deviceEntities);
setOriginalSettings(deviceEntities);
}
};

View File

@@ -354,7 +354,7 @@ settings = {
bool_format: 1,
bool_dashboard: 1,
enum_format: 1,
fahrenheit: false
fahrenheit: false,
}
const emsesp_devices = {
@@ -969,6 +969,7 @@ function updateMask(entity, de, dd) {
// see if the custom name has changed
const old_custom_name = dd.data[dd_objIndex].cn
console.log('comparing old ' + old_custom_name + ' with new ' + new_custom_name)
if (old_custom_name !== new_custom_name) {
changed = true
new_fullname = new_custom_name
@@ -984,6 +985,7 @@ function updateMask(entity, de, dd) {
de[de_objIndex].m = current_mask
de[de_objIndex].cn = new_fullname
dd.data[dd_objIndex].id = current_mask.toString(16).padStart(2, '0') + new_fullname
dd.data[dd_objIndex].cn = new_fullname
}
console.log('new dd:')