mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-06-13 19:36:26 +03:00
have to save before executing
This commit is contained in:
@@ -46,6 +46,7 @@ const CommandsDialog = ({
|
|||||||
validator
|
validator
|
||||||
}: CommandsDialogProps) => {
|
}: CommandsDialogProps) => {
|
||||||
const { LL } = useI18nContext();
|
const { LL } = useI18nContext();
|
||||||
|
const [hasChanges, setHasChanges] = useState<boolean>(false);
|
||||||
const [editItem, setEditItem] = useState<CommandItem>(selectedItem);
|
const [editItem, setEditItem] = useState<CommandItem>(selectedItem);
|
||||||
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
||||||
|
|
||||||
@@ -62,6 +63,17 @@ const CommandsDialog = ({
|
|||||||
}
|
}
|
||||||
}, [open, selectedItem]);
|
}, [open, selectedItem]);
|
||||||
|
|
||||||
|
const hasChanged = (ci: CommandItem) =>
|
||||||
|
ci.id !== ci.o_id ||
|
||||||
|
(ci.name || '') !== (ci.o_name || '') ||
|
||||||
|
ci.cmd !== ci.o_cmd ||
|
||||||
|
ci.value !== ci.o_value ||
|
||||||
|
ci.deleted !== ci.o_deleted;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setHasChanges(hasChanged(editItem));
|
||||||
|
}, [editItem]);
|
||||||
|
|
||||||
const handleSave = async (itemToSave: CommandItem) => {
|
const handleSave = async (itemToSave: CommandItem) => {
|
||||||
try {
|
try {
|
||||||
setFieldErrors(undefined);
|
setFieldErrors(undefined);
|
||||||
@@ -69,6 +81,8 @@ const CommandsDialog = ({
|
|||||||
onSave(itemToSave);
|
onSave(itemToSave);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setFieldErrors((error as ValidationError).fieldErrors);
|
setFieldErrors((error as ValidationError).fieldErrors);
|
||||||
|
} finally {
|
||||||
|
setHasChanges(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -162,15 +176,19 @@ const CommandsDialog = ({
|
|||||||
>
|
>
|
||||||
{LL.CANCEL()}
|
{LL.CANCEL()}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
|
||||||
startIcon={creating ? <AddIcon /> : <DoneIcon />}
|
{hasChanges && (
|
||||||
variant="outlined"
|
<Button
|
||||||
onClick={save}
|
startIcon={creating ? <AddIcon /> : <DoneIcon />}
|
||||||
color="primary"
|
variant="outlined"
|
||||||
>
|
onClick={save}
|
||||||
{creating ? LL.ADD(0) : LL.UPDATE()}
|
color="primary"
|
||||||
</Button>
|
>
|
||||||
{!creating && editItem.cmd !== '' && (
|
{creating ? LL.ADD(0) : LL.UPDATE()}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!creating && !hasChanges && editItem.cmd !== '' && (
|
||||||
<Button
|
<Button
|
||||||
startIcon={<PlayArrowIcon />}
|
startIcon={<PlayArrowIcon />}
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
|
|||||||
Reference in New Issue
Block a user