mirror of https://github.com/portainer/portainer
fix(time window): show errors for component [EE-6800] (#11317)
Co-authored-by: testa113 <testa113>pull/11332/head
parent
31c5a82749
commit
96b1d36280
|
@ -1,4 +1,5 @@
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
import { FormikErrors } from 'formik';
|
||||||
|
|
||||||
import { Button } from '@@/buttons';
|
import { Button } from '@@/buttons';
|
||||||
import { Alert } from '@@/Alert';
|
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.
|
* The current start and end time values. in 'HH:mm' format (e.g. '00:00') and in UTC timezone.
|
||||||
*/
|
*/
|
||||||
values: EndpointChangeWindow;
|
values: EndpointChangeWindow;
|
||||||
|
errors?: FormikErrors<EndpointChangeWindow>;
|
||||||
initialValues: EndpointChangeWindow;
|
initialValues: EndpointChangeWindow;
|
||||||
onChange: ({
|
onChangeTimeZone: (timeZone: string) => void;
|
||||||
changeWindow,
|
onChangeChangeWindow: (changeWindow: EndpointChangeWindow) => void;
|
||||||
timeZone,
|
|
||||||
}: {
|
|
||||||
changeWindow: EndpointChangeWindow;
|
|
||||||
timeZone?: string;
|
|
||||||
}) => void;
|
|
||||||
isEditMode: boolean;
|
isEditMode: boolean;
|
||||||
setIsEditMode: (isEditMode: boolean) => void;
|
setIsEditMode: (isEditMode: boolean) => void;
|
||||||
timeZone?: string;
|
timeZone?: string;
|
||||||
|
@ -31,8 +28,10 @@ const summaryTimeFormat = 'h:mmA';
|
||||||
|
|
||||||
export function TimeWindowPicker({
|
export function TimeWindowPicker({
|
||||||
values,
|
values,
|
||||||
|
errors,
|
||||||
initialValues,
|
initialValues,
|
||||||
onChange,
|
onChangeTimeZone,
|
||||||
|
onChangeChangeWindow,
|
||||||
isEditMode,
|
isEditMode,
|
||||||
setIsEditMode,
|
setIsEditMode,
|
||||||
timeZone = moment.tz.guess(),
|
timeZone = moment.tz.guess(),
|
||||||
|
@ -43,8 +42,10 @@ export function TimeWindowPicker({
|
||||||
{isEditMode && (
|
{isEditMode && (
|
||||||
<TimeWindowPickerInputGroup
|
<TimeWindowPickerInputGroup
|
||||||
values={values}
|
values={values}
|
||||||
onChange={onChange}
|
onChangeTimeZone={onChangeTimeZone}
|
||||||
|
onChangeChangeWindow={onChangeChangeWindow}
|
||||||
timeZone={timeZone}
|
timeZone={timeZone}
|
||||||
|
errors={errors}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<Alert color="info" className="[&>div]:!text-xs">
|
<Alert color="info" className="[&>div]:!text-xs">
|
||||||
|
@ -83,10 +84,8 @@ export function TimeWindowPicker({
|
||||||
className="!ml-0"
|
className="!ml-0"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setIsEditMode(false);
|
setIsEditMode(false);
|
||||||
onChange({
|
onChangeChangeWindow(initialValues);
|
||||||
changeWindow: initialValues,
|
onChangeTimeZone(initialTimeZone || moment.tz.guess());
|
||||||
timeZone: initialTimeZone,
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
import { FormikErrors } from 'formik';
|
||||||
|
|
||||||
import { Select } from '@@/form-components/ReactSelect';
|
import { Select } from '@@/form-components/ReactSelect';
|
||||||
import { Option } from '@@/form-components/PortainerSelect';
|
import { Option } from '@@/form-components/PortainerSelect';
|
||||||
|
import { FormError } from '@@/form-components/FormError';
|
||||||
|
|
||||||
import { EndpointChangeWindow } from '../../types';
|
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.
|
* The current start and end time values. in 'HH:mm' format (e.g. '00:00') and in UTC timezone.
|
||||||
*/
|
*/
|
||||||
values: EndpointChangeWindow;
|
values: EndpointChangeWindow;
|
||||||
onChange: ({
|
errors?: FormikErrors<EndpointChangeWindow>;
|
||||||
changeWindow,
|
onChangeTimeZone: (timeZone: string) => void;
|
||||||
timeZone,
|
onChangeChangeWindow: (changeWindow: EndpointChangeWindow) => void;
|
||||||
}: {
|
|
||||||
changeWindow: EndpointChangeWindow;
|
|
||||||
timeZone: string;
|
|
||||||
}) => void;
|
|
||||||
timeZone?: string;
|
timeZone?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function TimeWindowPickerInputGroup({
|
export function TimeWindowPickerInputGroup({
|
||||||
values,
|
values,
|
||||||
onChange,
|
errors,
|
||||||
|
onChangeTimeZone,
|
||||||
|
onChangeChangeWindow,
|
||||||
timeZone = moment.tz.guess(),
|
timeZone = moment.tz.guess(),
|
||||||
}: Props) {
|
}: Props) {
|
||||||
// all unique timezones for all countries as options
|
// 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
|
// set the initial timezone to the user's timezone if it is not set
|
||||||
if (!timeZone) {
|
if (!timeZone) {
|
||||||
const newTimeZone = moment.tz.guess();
|
const newTimeZone = moment.tz.guess();
|
||||||
onChange({
|
onChangeChangeWindow({
|
||||||
changeWindow: {
|
...values,
|
||||||
...values,
|
StartTime: timeZoneToUtc(values.StartTime, newTimeZone),
|
||||||
StartTime: timeZoneToUtc(values.StartTime, newTimeZone),
|
EndTime: timeZoneToUtc(values.EndTime, newTimeZone),
|
||||||
EndTime: timeZoneToUtc(values.EndTime, newTimeZone),
|
|
||||||
},
|
|
||||||
timeZone: newTimeZone,
|
|
||||||
});
|
});
|
||||||
|
onChangeTimeZone(newTimeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the option index for react-select to scroll to the current option
|
// find the option index for react-select to scroll to the current option
|
||||||
|
@ -64,63 +62,59 @@ export function TimeWindowPickerInputGroup({
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-wrap items-center gap-x-5">
|
<div className="flex-col">
|
||||||
<div className="flex items-center gap-x-5">
|
<div className="flex flex-wrap items-center gap-x-5">
|
||||||
<TimePickerInput
|
<div className="inline-flex flex-wrap items-center gap-x-5">
|
||||||
utcTime={values.StartTime}
|
<TimePickerInput
|
||||||
timeZone={timeZone}
|
utcTime={values.StartTime}
|
||||||
onChange={(time) =>
|
timeZone={timeZone}
|
||||||
onChange({
|
onChange={(time) =>
|
||||||
changeWindow: {
|
onChangeChangeWindow({
|
||||||
...values,
|
...values,
|
||||||
StartTime: timeZoneToUtc(time, timeZone),
|
StartTime: timeZoneToUtc(time, timeZone),
|
||||||
},
|
})
|
||||||
timeZone,
|
}
|
||||||
})
|
/>
|
||||||
}
|
to
|
||||||
/>
|
<TimePickerInput
|
||||||
to
|
utcTime={values.EndTime}
|
||||||
<TimePickerInput
|
timeZone={timeZone}
|
||||||
utcTime={values.EndTime}
|
onChange={(time) =>
|
||||||
timeZone={timeZone}
|
onChangeChangeWindow({
|
||||||
onChange={(time) =>
|
|
||||||
onChange({
|
|
||||||
changeWindow: {
|
|
||||||
...values,
|
...values,
|
||||||
EndTime: timeZoneToUtc(time, timeZone),
|
EndTime: timeZoneToUtc(time, timeZone),
|
||||||
},
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<Select<Option<string>>
|
||||||
|
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,
|
timeZone,
|
||||||
})
|
newTimeZone.value
|
||||||
}
|
);
|
||||||
/>
|
const updatedEndTime = onTimezoneChangeUpdateUTCTime(
|
||||||
</div>
|
values.EndTime,
|
||||||
<Select<Option<string>>
|
timeZone,
|
||||||
options={timeZoneOptions}
|
newTimeZone.value
|
||||||
value={timeZoneOptions[timeZoneOptionIndex]}
|
);
|
||||||
className="w-72 min-w-fit"
|
onChangeChangeWindow({
|
||||||
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: {
|
|
||||||
...values,
|
...values,
|
||||||
StartTime: updatedStartTime,
|
StartTime: updatedStartTime,
|
||||||
EndTime: updatedEndTime,
|
EndTime: updatedEndTime,
|
||||||
},
|
});
|
||||||
timeZone: newTimeZone.value,
|
onChangeTimeZone(newTimeZone.value);
|
||||||
});
|
}}
|
||||||
}}
|
/>
|
||||||
/>
|
</div>
|
||||||
|
{errors?.StartTime && <FormError>{errors.StartTime}</FormError>}
|
||||||
|
{errors?.EndTime && <FormError>{errors.EndTime}</FormError>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue