diff --git a/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPicker.tsx b/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPicker.tsx index fce1258fc..3d3b97759 100644 --- a/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPicker.tsx +++ b/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPicker.tsx @@ -1,4 +1,5 @@ import moment from 'moment'; +import { FormikErrors } from 'formik'; import { Button } from '@@/buttons'; import { Alert } from '@@/Alert'; @@ -13,14 +14,10 @@ type Props = { * The current start and end time values. in 'HH:mm' format (e.g. '00:00') and in UTC timezone. */ values: EndpointChangeWindow; + errors?: FormikErrors; initialValues: EndpointChangeWindow; - onChange: ({ - changeWindow, - timeZone, - }: { - changeWindow: EndpointChangeWindow; - timeZone?: string; - }) => void; + onChangeTimeZone: (timeZone: string) => void; + onChangeChangeWindow: (changeWindow: EndpointChangeWindow) => void; isEditMode: boolean; setIsEditMode: (isEditMode: boolean) => void; timeZone?: string; @@ -31,8 +28,10 @@ const summaryTimeFormat = 'h:mmA'; export function TimeWindowPicker({ values, + errors, initialValues, - onChange, + onChangeTimeZone, + onChangeChangeWindow, isEditMode, setIsEditMode, timeZone = moment.tz.guess(), @@ -43,8 +42,10 @@ export function TimeWindowPicker({ {isEditMode && ( )} @@ -83,10 +84,8 @@ export function TimeWindowPicker({ className="!ml-0" onClick={() => { setIsEditMode(false); - onChange({ - changeWindow: initialValues, - timeZone: initialTimeZone, - }); + onChangeChangeWindow(initialValues); + onChangeTimeZone(initialTimeZone || moment.tz.guess()); }} > Cancel diff --git a/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPickerInputGroup.tsx b/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPickerInputGroup.tsx index 5f8eb5b6c..dd394518f 100644 --- a/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPickerInputGroup.tsx +++ b/app/react/portainer/environments/common/TimeWindowPicker/TimeWindowPickerInputGroup.tsx @@ -1,8 +1,10 @@ import moment from 'moment'; import { useMemo } from 'react'; +import { FormikErrors } from 'formik'; import { Select } from '@@/form-components/ReactSelect'; import { Option } from '@@/form-components/PortainerSelect'; +import { FormError } from '@@/form-components/FormError'; import { EndpointChangeWindow } from '../../types'; @@ -14,19 +16,17 @@ type Props = { * The current start and end time values. in 'HH:mm' format (e.g. '00:00') and in UTC timezone. */ values: EndpointChangeWindow; - onChange: ({ - changeWindow, - timeZone, - }: { - changeWindow: EndpointChangeWindow; - timeZone: string; - }) => void; + errors?: FormikErrors; + onChangeTimeZone: (timeZone: string) => void; + onChangeChangeWindow: (changeWindow: EndpointChangeWindow) => void; timeZone?: string; }; export function TimeWindowPickerInputGroup({ values, - onChange, + errors, + onChangeTimeZone, + onChangeChangeWindow, timeZone = moment.tz.guess(), }: Props) { // all unique timezones for all countries as options @@ -47,14 +47,12 @@ export function TimeWindowPickerInputGroup({ // set the initial timezone to the user's timezone if it is not set if (!timeZone) { const newTimeZone = moment.tz.guess(); - onChange({ - changeWindow: { - ...values, - StartTime: timeZoneToUtc(values.StartTime, newTimeZone), - EndTime: timeZoneToUtc(values.EndTime, newTimeZone), - }, - timeZone: newTimeZone, + onChangeChangeWindow({ + ...values, + StartTime: timeZoneToUtc(values.StartTime, newTimeZone), + EndTime: timeZoneToUtc(values.EndTime, newTimeZone), }); + onChangeTimeZone(newTimeZone); } // find the option index for react-select to scroll to the current option @@ -64,63 +62,59 @@ export function TimeWindowPickerInputGroup({ ); return ( -
-
- - onChange({ - changeWindow: { +
+
+
+ + onChangeChangeWindow({ ...values, StartTime: timeZoneToUtc(time, timeZone), - }, - timeZone, - }) - } - /> - to - - onChange({ - changeWindow: { + }) + } + /> + to + + onChangeChangeWindow({ ...values, EndTime: timeZoneToUtc(time, timeZone), - }, + }) + } + /> +
+ > + options={timeZoneOptions} + value={timeZoneOptions[timeZoneOptionIndex]} + className="basis-[fit-content] flex-1 min-w-fit max-w-xs" + onChange={(newTimeZone) => { + if (!newTimeZone) return; + // update the utc time so that the local time displayed remains the same + const updatedStartTime = onTimezoneChangeUpdateUTCTime( + values.StartTime, timeZone, - }) - } - /> -
- > - options={timeZoneOptions} - value={timeZoneOptions[timeZoneOptionIndex]} - className="w-72 min-w-fit" - onChange={(newTimeZone) => { - if (!newTimeZone) return; - // update the utc time so that the local time displayed remains the same - const updatedStartTime = onTimezoneChangeUpdateUTCTime( - values.StartTime, - timeZone, - newTimeZone.value - ); - const updatedEndTime = onTimezoneChangeUpdateUTCTime( - values.EndTime, - timeZone, - newTimeZone.value - ); - onChange({ - changeWindow: { + newTimeZone.value + ); + const updatedEndTime = onTimezoneChangeUpdateUTCTime( + values.EndTime, + timeZone, + newTimeZone.value + ); + onChangeChangeWindow({ ...values, StartTime: updatedStartTime, EndTime: updatedEndTime, - }, - timeZone: newTimeZone.value, - }); - }} - /> + }); + onChangeTimeZone(newTimeZone.value); + }} + /> +
+ {errors?.StartTime && {errors.StartTime}} + {errors?.EndTime && {errors.EndTime}}
); }