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, MenuItem } from '@mui/material'; import * as NTPApi from 'api/ntp'; import { updateState } from 'alova'; import type { ValidateFieldsError } from 'async-validator'; import { BlockFormControlLabel, BlockNavigation, ButtonRow, FormLoader, SectionContent, ValidatedTextField } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import type { NTPSettingsType } from 'types'; import { updateValueDirty, useRest } from 'utils'; import { validate } from 'validators'; import { NTP_SETTINGS_VALIDATOR } from 'validators/ntp'; import { TIME_ZONES, selectedTimeZone, timeZoneSelectItems } from './TZ'; const NTPSettings: FC = () => { const { loadData, saving, data, updateDataValue, origData, dirtyFlags, setDirtyFlags, blocker, saveData, errorMessage } = useRest({ read: NTPApi.readNTPSettings, update: NTPApi.updateNTPSettings }); const { LL } = useI18nContext(); const updateFormValue = updateValueDirty( origData, dirtyFlags, setDirtyFlags, updateDataValue ); const [fieldErrors, setFieldErrors] = useState(); const content = () => { if (!data) { return ; } const validateAndSubmit = async () => { try { setFieldErrors(undefined); await validate(NTP_SETTINGS_VALIDATOR, data); await saveData(); } catch (error) { setFieldErrors(error as ValidateFieldsError); } }; const changeTimeZone = (event: React.ChangeEvent) => { updateFormValue(event); updateState('ntpSettings', (settings: NTPSettingsType) => ({ ...settings, tz_label: event.target.value, tz_format: TIME_ZONES[event.target.value] })); }; return ( <> } label={LL.ENABLE_NTP()} /> {LL.TIME_ZONE()}... {timeZoneSelectItems()} {dirtyFlags && dirtyFlags.length !== 0 && ( )} ); }; return ( {blocker ? : null} {content()} ); }; export default NTPSettings;