import { useState } from 'react'; import { toast } from 'react-toastify'; import AccessTimeIcon from '@mui/icons-material/AccessTime'; import CancelIcon from '@mui/icons-material/Cancel'; import WarningIcon from '@mui/icons-material/Warning'; import { Box, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, TextField, Typography } from '@mui/material'; import * as NTPApi from 'api/ntp'; import { readNTPSettings } from 'api/ntp'; import { dialogStyle } from 'CustomTheme'; import { useRequest } from 'alova/client'; import { updateState } from 'alova/client'; import type { ValidateFieldsError } from 'async-validator'; import { BlockFormControlLabel, BlockNavigation, ButtonRow, FormLoader, SectionContent, ValidatedTextField, useLayoutTitle } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import type { NTPSettingsType, Time } from 'types'; import { formatLocalDateTime, updateValueDirty, useRest } from 'utils'; import { validate } from 'validators'; import { NTP_SETTINGS_VALIDATOR } from 'validators/ntp'; import { TIME_ZONES, selectedTimeZone, timeZoneSelectItems } from './TZ'; const NTPSettings = () => { const { loadData, saving, data, updateDataValue, origData, dirtyFlags, setDirtyFlags, blocker, saveData, errorMessage } = useRest({ read: NTPApi.readNTPSettings, update: NTPApi.updateNTPSettings }); const { LL } = useI18nContext(); useLayoutTitle('NTP'); const [localTime, setLocalTime] = useState(''); const [settingTime, setSettingTime] = useState(false); const [processing, setProcessing] = useState(false); const { send: updateTime } = useRequest( (local_time: Time) => NTPApi.updateTime(local_time), { immediate: false } ); const updateFormValue = updateValueDirty( origData, dirtyFlags, setDirtyFlags, updateDataValue ); const [fieldErrors, setFieldErrors] = useState(); const updateLocalTime = (event: React.ChangeEvent) => setLocalTime(event.target.value); const openSetTime = () => { setLocalTime(formatLocalDateTime(new Date())); setSettingTime(true); }; const configureTime = async () => { setProcessing(true); await updateTime({ local_time: formatLocalDateTime(new Date(localTime)) }) .then(async () => { toast.success(LL.TIME_SET()); setSettingTime(false); await loadData(); }) .catch(() => { toast.error(LL.PROBLEM_UPDATING()); }) .finally(() => { setProcessing(false); }); }; const renderSetTimeDialog = () => ( setSettingTime(false)} > {LL.SET_TIME(1)} {LL.SET_TIME_TEXT()} ); 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) => { void updateState(readNTPSettings(), (settings: NTPSettingsType) => ({ ...settings, tz_label: event.target.value, tz_format: TIME_ZONES[event.target.value] })); updateFormValue(event); }; return ( <> } label={LL.ENABLE_NTP()} /> {LL.TIME_ZONE()}... {timeZoneSelectItems()} {!data.enabled && !dirtyFlags.length && ( )} {renderSetTimeDialog()} {dirtyFlags && dirtyFlags.length !== 0 && ( )} ); }; return ( {blocker ? : null} {content()} ); }; export default NTPSettings;