From b426e0eb455d039c3372ed5ceb30ee04214e3487 Mon Sep 17 00:00:00 2001 From: proddy Date: Fri, 18 Apr 2025 16:19:07 +0200 Subject: [PATCH] move NTP set time to Settings page --- interface/src/app/settings/NTPSettings.tsx | 123 ++++++++++++++++++++- interface/src/app/status/NTPStatus.tsx | 121 +------------------- interface/src/app/status/Version.tsx | 2 +- mock-api/rest_server.ts | 14 ++- 4 files changed, 135 insertions(+), 125 deletions(-) diff --git a/interface/src/app/settings/NTPSettings.tsx b/interface/src/app/settings/NTPSettings.tsx index cf42b5e19..b86631ac6 100644 --- a/interface/src/app/settings/NTPSettings.tsx +++ b/interface/src/app/settings/NTPSettings.tsx @@ -1,12 +1,27 @@ 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 { Button, Checkbox, MenuItem } from '@mui/material'; +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 { @@ -19,8 +34,8 @@ import { useLayoutTitle } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; -import type { NTPSettingsType } from 'types'; -import { updateValueDirty, useRest } from 'utils'; +import type { NTPSettingsType, Time } from 'types'; +import { formatLocalDateTime, updateValueDirty, useRest } from 'utils'; import { validate } from 'validators'; import { NTP_SETTINGS_VALIDATOR } from 'validators/ntp'; @@ -46,6 +61,17 @@ const NTPSettings = () => { 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, @@ -55,6 +81,78 @@ const NTPSettings = () => { 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 ; @@ -115,6 +213,25 @@ const NTPSettings = () => { {LL.TIME_ZONE()}... {timeZoneSelectItems()} + + + {!data.enabled && !dirtyFlags.length && ( + + + + + + )} + + {renderSetTimeDialog()} + {dirtyFlags && dirtyFlags.length !== 0 && ( - - - - ); - const content = () => { if (!data) { return ; @@ -219,23 +121,6 @@ const NTPStatus = () => { - - {data && !isNtpActive(data) && ( - - - - - - )} - - {renderSetTimeDialog()} ); }; diff --git a/interface/src/app/status/Version.tsx b/interface/src/app/status/Version.tsx index 08a7cfa45..f3e3d1a20 100644 --- a/interface/src/app/status/Version.tsx +++ b/interface/src/app/status/Version.tsx @@ -220,7 +220,7 @@ const Version = () => { color="primary" > - {LL.DOWNLOAD(1)} + {LL.DOWNLOAD(0)} {!downloadOnly && ( diff --git a/mock-api/rest_server.ts b/mock-api/rest_server.ts index 7ed0bfa6b..8a873c385 100644 --- a/mock-api/rest_server.ts +++ b/mock-api/rest_server.ts @@ -414,8 +414,8 @@ let ntp_settings = { tz_label: 'Europe/Amsterdam', tz_format: 'CET-1CEST,M3.5.0,M10.5.0/3' }; -const ntp_status = { - status: 2, +let ntp_status = { + status: 0, // 0 = disabled, 1 = inactive, 2 = active utc_time: '2021-04-01T14:25:42Z', local_time: '2021-04-01T16:25:42', server: 'time.google.com', @@ -4330,7 +4330,15 @@ router router .get(NTP_SETTINGS_ENDPOINT, () => ntp_settings) .get(NTP_STATUS_ENDPOINT, () => ntp_status) - .post(TIME_ENDPOINT, () => status(200)) + .post(TIME_ENDPOINT, async (request: any) => { + const rsp = await request.json(); + const local_time = rsp.local_time; + ntp_status.status = 1; + ntp_status.local_time = local_time; + ntp_status.utc_time = local_time; + console.log('ntp time set to', local_time); + return status(200); + }) .post(NTP_SETTINGS_ENDPOINT, async (request: any) => { ntp_settings = await request.json(); console.log('ntp settings saved', ntp_settings);