mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
142 lines
3.4 KiB
TypeScript
142 lines
3.4 KiB
TypeScript
import { useState } from 'react';
|
|
import type { FC } from 'react';
|
|
|
|
import CancelIcon from '@mui/icons-material/Cancel';
|
|
import WarningIcon from '@mui/icons-material/Warning';
|
|
import { Button, Checkbox } from '@mui/material';
|
|
|
|
import * as SystemApi from 'api/system';
|
|
|
|
import type { ValidateFieldsError } from 'async-validator';
|
|
import {
|
|
BlockFormControlLabel,
|
|
BlockNavigation,
|
|
ButtonRow,
|
|
FormLoader,
|
|
SectionContent,
|
|
ValidatedPasswordField,
|
|
ValidatedTextField,
|
|
useLayoutTitle
|
|
} from 'components';
|
|
import { useI18nContext } from 'i18n/i18n-react';
|
|
import type { OTASettingsType } from 'types';
|
|
import { numberValue, updateValueDirty, useRest } from 'utils';
|
|
import { validate } from 'validators';
|
|
import { OTA_SETTINGS_VALIDATOR } from 'validators/system';
|
|
|
|
const OTASettings: FC = () => {
|
|
const {
|
|
loadData,
|
|
saveData,
|
|
saving,
|
|
updateDataValue,
|
|
data,
|
|
origData,
|
|
dirtyFlags,
|
|
setDirtyFlags,
|
|
blocker,
|
|
errorMessage
|
|
} = useRest<OTASettingsType>({
|
|
read: SystemApi.readOTASettings,
|
|
update: SystemApi.updateOTASettings
|
|
});
|
|
|
|
const { LL } = useI18nContext();
|
|
|
|
const updateFormValue = updateValueDirty(
|
|
origData,
|
|
dirtyFlags,
|
|
setDirtyFlags,
|
|
updateDataValue
|
|
);
|
|
|
|
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
|
|
|
const content = () => {
|
|
if (!data) {
|
|
return <FormLoader onRetry={loadData} errorMessage={errorMessage} />;
|
|
}
|
|
|
|
const validateAndSubmit = async () => {
|
|
try {
|
|
setFieldErrors(undefined);
|
|
await validate(OTA_SETTINGS_VALIDATOR, data);
|
|
await saveData();
|
|
} catch (error) {
|
|
setFieldErrors(error as ValidateFieldsError);
|
|
}
|
|
};
|
|
|
|
useLayoutTitle('OTA');
|
|
|
|
return (
|
|
<>
|
|
<BlockFormControlLabel
|
|
control={
|
|
<Checkbox
|
|
name="enabled"
|
|
checked={data.enabled}
|
|
onChange={updateFormValue}
|
|
/>
|
|
}
|
|
label={LL.ENABLE_OTA()}
|
|
/>
|
|
<ValidatedTextField
|
|
fieldErrors={fieldErrors}
|
|
name="port"
|
|
label="Port"
|
|
fullWidth
|
|
variant="outlined"
|
|
value={numberValue(data.port)}
|
|
type="number"
|
|
onChange={updateFormValue}
|
|
margin="normal"
|
|
/>
|
|
<ValidatedPasswordField
|
|
fieldErrors={fieldErrors}
|
|
name="password"
|
|
label={LL.PASSWORD()}
|
|
fullWidth
|
|
variant="outlined"
|
|
value={data.password}
|
|
onChange={updateFormValue}
|
|
margin="normal"
|
|
/>
|
|
{dirtyFlags && dirtyFlags.length !== 0 && (
|
|
<ButtonRow>
|
|
<Button
|
|
startIcon={<CancelIcon />}
|
|
disabled={saving}
|
|
variant="outlined"
|
|
color="primary"
|
|
type="submit"
|
|
onClick={loadData}
|
|
>
|
|
{LL.CANCEL()}
|
|
</Button>
|
|
<Button
|
|
startIcon={<WarningIcon color="warning" />}
|
|
disabled={saving}
|
|
variant="contained"
|
|
color="info"
|
|
type="submit"
|
|
onClick={validateAndSubmit}
|
|
>
|
|
{LL.APPLY_CHANGES(dirtyFlags.length)}
|
|
</Button>
|
|
</ButtonRow>
|
|
)}
|
|
</>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<SectionContent>
|
|
{blocker ? <BlockNavigation blocker={blocker} /> : null}
|
|
{content()}
|
|
</SectionContent>
|
|
);
|
|
};
|
|
|
|
export default OTASettings;
|