Skip to content

Commit

Permalink
fix(app-headless-cms): ensure time value contains seconds (#4293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 authored Sep 25, 2024
1 parent 00db1aa commit 98bc686
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -8,7 +8,9 @@ import {
DEFAULT_TIMEZONE,
getCurrentDate,
getCurrentLocalTime,
getCurrentTimeZone
getCurrentTimeZone,
getHHmmss,
getHHmm
} from "./utils";
import { CmsModelField } from "~/types";
import { BindComponentRenderProp } from "@webiny/form";
Expand Down Expand Up @@ -37,11 +39,11 @@ const parseDateTime = (value?: string): Pick<State, "date"> & { rest: string } =
};
};

const parseTime = (value?: string): Pick<State, "time" | "timezone"> => {
const parseTime = (value?: string, defaultTimeZone?: string): Pick<State, "time" | "timezone"> => {
if (!value) {
return {
time: "",
timezone: ""
timezone: defaultTimeZone || ""
};
}
const sign = value.includes("+") ? "+" : "-";
Expand All @@ -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;

Expand All @@ -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={{
Expand All @@ -111,7 +105,7 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith
<Input
bind={{
...bind,
value: time,
value: getHHmm(time),
onChange: async value => {
if (!value) {
if (!bind.value) {
Expand All @@ -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}`
);
}
}}
Expand All @@ -135,7 +129,7 @@ export const DateTimeWithTimezone = ({ bind, trailingIcon, field }: DateTimeWith
<Cell span={cellSize}>
<Select
label="Timezone"
value={timezone || defaultTimeZone}
value={timezone}
onChange={value => {
if (!value) {
if (!bind.value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useEffect } from "react";
import React from "react";
import { Grid, Cell } from "@webiny/ui/Grid";
import {
getCurrentDate,
getCurrentLocalTime,
getDefaultFieldValue,
getHHmm,
getHHmmss,
RemoveFieldButton
} from "./utils";
import { Input } from "./Input";
Expand Down Expand Up @@ -55,21 +57,14 @@ export const DateTimeWithoutTimezone = ({
trailingIcon
}: DateTimeWithoutTimezoneProps) => {
// "2020-05-18 09:00:00"
const initialValue = getDefaultFieldValue(field, bind, () => {
const date = new Date();
return `${getCurrentDate(date)} ${getCurrentLocalTime(date)}`;
});
const value =
bind.value ||
getDefaultFieldValue(field, bind, () => {
const date = new Date();
return `${getCurrentDate(date)} ${getCurrentLocalTime(date)}`;
});

const { date, time } = parseDateTime(initialValue);

const bindValue = bind.value || "";

useEffect(() => {
if (!date || !time || bindValue === initialValue) {
return;
}
bind.onChange(initialValue);
}, [bindValue]);
const { date, time } = parseDateTime(value);

const cellSize = trailingIcon ? 5 : 6;

Expand All @@ -87,7 +82,7 @@ export const DateTimeWithoutTimezone = ({
}
return bind.onChange("");
}
return bind.onChange(`${value} ${time || getCurrentLocalTime()}`);
return bind.onChange(`${value} ${getHHmmss(time)}`);
}
}}
field={{
Expand All @@ -101,15 +96,15 @@ export const DateTimeWithoutTimezone = ({
<Input
bind={{
...bind,
value: time,
value: getHHmm(time),
onChange: async value => {
if (!value) {
if (!bind.value) {
return;
}
return bind.onChange("");
}
return bind.onChange(`${date || getCurrentDate()} ${value}`);
return bind.onChange(`${date || getCurrentDate()} ${getHHmmss(value)}`);
}
}}
field={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 98bc686

Please sign in to comment.