From 98bc68682f57646319bab783a997090fb71ae985 Mon Sep 17 00:00:00 2001 From: Pavel Denisjuk Date: Wed, 25 Sep 2024 13:13:54 +0200 Subject: [PATCH] fix(app-headless-cms): ensure time value contains seconds (#4293) --- .../dateTime/DateTimeWithTimezone.tsx | 44 ++++++++----------- .../dateTime/DateTimeWithoutTimezone.tsx | 31 ++++++------- .../plugins/fieldRenderers/dateTime/utils.tsx | 16 +++++++ 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/DateTimeWithTimezone.tsx b/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/DateTimeWithTimezone.tsx index 3c1ab4350f4..20a91a5a6c1 100644 --- a/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/DateTimeWithTimezone.tsx +++ b/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/DateTimeWithTimezone.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React from "react"; import { Input } from "./Input"; import { Select } from "./Select"; import { Grid, Cell } from "@webiny/ui/Grid"; @@ -8,7 +8,9 @@ import { DEFAULT_TIMEZONE, getCurrentDate, getCurrentLocalTime, - getCurrentTimeZone + getCurrentTimeZone, + getHHmmss, + getHHmm } from "./utils"; import { CmsModelField } from "~/types"; import { BindComponentRenderProp } from "@webiny/form"; @@ -37,11 +39,11 @@ const parseDateTime = (value?: string): Pick & { rest: string } = }; }; -const parseTime = (value?: string): Pick => { +const parseTime = (value?: string, defaultTimeZone?: string): Pick => { if (!value) { return { time: "", - timezone: "" + timezone: defaultTimeZone || "" }; } const sign = value.includes("+") ? "+" : "-"; @@ -62,20 +64,15 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith const defaultTimeZone = getCurrentTimeZone() || DEFAULT_TIMEZONE; // "2020-05-18T09:00+10:00" - const initialValue = getDefaultFieldValue(field, bind, () => { - const date = new Date(); - return `${getCurrentDate(date)}T${getCurrentLocalTime(date)}${defaultTimeZone}`; - }); - const { date, rest } = parseDateTime(initialValue); - const { time, timezone = defaultTimeZone } = parseTime(rest); + const value = + bind.value || + getDefaultFieldValue(field, bind, () => { + const date = new Date(); + return `${getCurrentDate(date)}T${getCurrentLocalTime(date)}${defaultTimeZone}`; + }); - const bindValue = bind.value || ""; - useEffect(() => { - if (!date || !time || !timezone || bindValue === initialValue) { - return; - } - bind.onChange(initialValue); - }, [bindValue]); + const { date, rest } = parseDateTime(value); + const { time, timezone } = parseTime(rest, defaultTimeZone); const cellSize = trailingIcon ? 3 : 4; @@ -93,11 +90,8 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith } return bind.onChange(""); } - return bind.onChange( - `${value}T${time || getCurrentLocalTime()}${ - timezone || defaultTimeZone - }` - ); + + return bind.onChange(`${value}T${getHHmmss(time)}${timezone}`); } }} field={{ @@ -111,7 +105,7 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith { if (!value) { if (!bind.value) { @@ -120,7 +114,7 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith return bind.onChange(""); } return bind.onChange( - `${date || getCurrentDate()}T${value}${timezone || defaultTimeZone}` + `${date || getCurrentDate()}T${getHHmmss(value)}${timezone}` ); } }} @@ -135,7 +129,7 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith { if (!value) { if (!bind.value) { @@ -109,7 +104,7 @@ export const DateTimeWithoutTimezone = ({ } return bind.onChange(""); } - return bind.onChange(`${date || getCurrentDate()} ${value}`); + return bind.onChange(`${date || getCurrentDate()} ${getHHmmss(value)}`); } }} field={{ diff --git a/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/utils.tsx b/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/utils.tsx index f145ec76a1a..28d1bb6722c 100644 --- a/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/utils.tsx +++ b/packages/app-headless-cms/src/admin/plugins/fieldRenderers/dateTime/utils.tsx @@ -58,6 +58,22 @@ export const getCurrentDate = (date?: Date): string => { return `${year}-${month}-${day}`; }; +export const getHHmm = (time?: string) => { + if (!time) { + return ""; + } + const parsableTime = time || getCurrentLocalTime(); + return parsableTime.split(":").slice(0, 2).join(":"); +}; + +// Ensure a valid HH:mm:ss string, ending with :00 for seconds. +export const getHHmmss = (time?: string) => { + const parsableTime = time || getCurrentLocalTime(); + const parts = [...parsableTime.split(":").slice(0, 2), "00"]; + + return parts.join(":"); +}; + const deleteIconStyles = css({ width: "100% !important", height: "100% !important",