From ad5a17ac3428af92c868b00fdd7a9f8e340a19a4 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Mon, 20 Nov 2023 10:24:54 +0200 Subject: [PATCH] feat(edge/updates): schedule time changes [EE-5975] (#10458) --- app/portainer/filters/filters.js | 12 ++++++------ .../ListView/columns/scheduled-time.tsx | 7 +++++++ .../common/ScheduledTimeField.tsx | 16 +++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/portainer/filters/filters.js b/app/portainer/filters/filters.js index c6af4a9e7..5e75f9d7b 100644 --- a/app/portainer/filters/filters.js +++ b/app/portainer/filters/filters.js @@ -41,16 +41,16 @@ export function isoDateFromTimestamp(timestamp) { return moment.unix(timestamp).format(TIME_FORMAT); } -export function isoDate(date) { - return moment(date).format(TIME_FORMAT); +export function isoDate(date, format = TIME_FORMAT) { + return moment(date).format(format); } -export function parseIsoDate(date) { - return moment(date, TIME_FORMAT).toDate(); +export function parseIsoDate(date, format = TIME_FORMAT) { + return moment(date, format).toDate(); } -export function formatDate(date, strFormat = 'YYYY-MM-DD HH:mm:ss Z') { - return moment(date, strFormat).format(TIME_FORMAT); +export function formatDate(date, strFormat = 'YYYY-MM-DD HH:mm:ss Z', outFormat = TIME_FORMAT) { + return moment(date, strFormat).format(outFormat); } export function getPairKey(pair, separator) { diff --git a/app/react/portainer/environments/update-schedules/ListView/columns/scheduled-time.tsx b/app/react/portainer/environments/update-schedules/ListView/columns/scheduled-time.tsx index 653d230c3..0bfbe181e 100644 --- a/app/react/portainer/environments/update-schedules/ListView/columns/scheduled-time.tsx +++ b/app/react/portainer/environments/update-schedules/ListView/columns/scheduled-time.tsx @@ -1,5 +1,12 @@ +import { formatDate } from '@/portainer/filters/filters'; + +import { FORMAT } from '../../common/ScheduledTimeField'; + import { columnHelper } from './helper'; export const scheduledTime = columnHelper.accessor('scheduledTime', { header: 'Scheduled Time & Date', + // make sure the value has the right format + cell: ({ getValue }) => formatDate(getValue(), FORMAT, FORMAT), + id: 'time', }); diff --git a/app/react/portainer/environments/update-schedules/common/ScheduledTimeField.tsx b/app/react/portainer/environments/update-schedules/common/ScheduledTimeField.tsx index cb4a80b04..cacb17cb9 100644 --- a/app/react/portainer/environments/update-schedules/common/ScheduledTimeField.tsx +++ b/app/react/portainer/environments/update-schedules/common/ScheduledTimeField.tsx @@ -20,11 +20,13 @@ interface Props { disabled?: boolean; } +export const FORMAT = 'YYYY-MM-DD HH:mm'; + export function ScheduledTimeField({ disabled }: Props) { const [{ name, value }, { error }, { setValue }] = useField('scheduledTime'); - const dateValue = useMemo(() => parseIsoDate(value), [value]); + const dateValue = useMemo(() => parseIsoDate(value, FORMAT), [value]); if (!value) { return null; @@ -35,12 +37,12 @@ export function ScheduledTimeField({ disabled }: Props) { {!disabled ? ( { const dateToSave = date || new Date(Date.now() + 24 * 60 * 60 * 1000); - setValue(isoDate(dateToSave.valueOf())); + setValue(isoDate(dateToSave.valueOf(), FORMAT)); }} name={name} value={dateValue} @@ -72,16 +74,16 @@ export function timeValidation() { ) .test( 'validDate', - `Scheduled time must be bigger then ${isoDate( - Date.now() - 24 * 60 * 60 * 1000 - )}`, + `Scheduled time must be bigger then ${ + (isoDate(Date.now() - 24 * 60 * 60 * 1000), FORMAT) + }`, (value) => parseIsoDate(value).valueOf() > Date.now() - 24 * 60 * 60 * 1000 ); } export function defaultValue() { - return isoDate(Date.now() + 24 * 60 * 60 * 1000); + return isoDate(Date.now(), FORMAT); } function isValidDate(date: Date) {