From ac4e2e5bf2da1411a2980babdd8d27685c97ad9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 1 Jun 2024 00:31:39 +0200 Subject: [PATCH] Update time calculations in BannerComponent and TicketNormalizer Refactored BannerComponent to use an imported date function, improved time calculation logic, and enhanced code readability. Also, updated TicketNormalizer to properly normalize various datetime and user-related fields. A new date validation check has been added in the date utility file. --- .../Resources/public/chill/js/date.ts | 4 ++++ .../TicketApp/components/BannerComponent.vue | 21 +++++++++++++------ .../Normalizer/TicketNormalizer.php | 6 ++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Resources/public/chill/js/date.ts b/src/Bundle/ChillMainBundle/Resources/public/chill/js/date.ts index 8c087146a..9198f835e 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/chill/js/date.ts +++ b/src/Bundle/ChillMainBundle/Resources/public/chill/js/date.ts @@ -59,6 +59,10 @@ export const ISOToDatetime = (str: string|null): Date|null => { [hours, minutes, seconds] = time.split(':').map(s => parseInt(s)); ; + if ('0000' === timezone) { + return new Date(Date.UTC(year, month-1, date, hours, minutes, seconds)); + } + return new Date(year, month-1, date, hours, minutes, seconds); } diff --git a/src/Bundle/ChillTicketBundle/src/Resources/public/vuejs/TicketApp/components/BannerComponent.vue b/src/Bundle/ChillTicketBundle/src/Resources/public/vuejs/TicketApp/components/BannerComponent.vue index b03b10405..61b62eff9 100644 --- a/src/Bundle/ChillTicketBundle/src/Resources/public/vuejs/TicketApp/components/BannerComponent.vue +++ b/src/Bundle/ChillTicketBundle/src/Resources/public/vuejs/TicketApp/components/BannerComponent.vue @@ -79,6 +79,7 @@ import AddresseeComponent from "./AddresseeComponent.vue"; // Types import { Ticket } from "../../../types"; +import {ISOToDatetime} from "../../../../../../../ChillMainBundle/Resources/public/chill/js/date"; export default defineComponent({ name: "BannerComponent", @@ -95,14 +96,21 @@ export default defineComponent({ setup(props) { const { t } = useI18n(); const today = ref(new Date()); - const createdAt = ref(props.ticket.createdAt as any); + const createdAt = ref(props.ticket.createdAt); setInterval(function () { today.value = new Date(); - }, 1000); + }, 5000); const since = computed(() => { - const date = new Date(createdAt.value.date); + if (null === createdAt.value) { + return ""; + } + const date = ISOToDatetime(createdAt.value.datetime); + + if (null === date) { + return ""; + } const timeDiff = Math.abs(today.value.getTime() - date.getTime()); const daysDiff = Math.floor(timeDiff / (1000 * 3600 * 24)); @@ -115,11 +123,12 @@ export default defineComponent({ const secondsDiff = Math.floor((timeDiff % (1000 * 60)) / 1000); if (daysDiff < 1 && hoursDiff < 1 && minutesDiff < 1) { - return `${t("banner.seconds", { count: secondsDiff })}`; + return `${t("banner.seconds", {count: secondsDiff})}`; + } else if (daysDiff < 1 && hoursDiff < 1) { + return `${t("banner.minutes", { count: minutesDiff })}`; } else if (daysDiff < 1) { return `${t("banner.hours", { count: hoursDiff })} - ${t("banner.minutes", { count: minutesDiff })} - ${t("banner.seconds", { count: secondsDiff })}`; + ${t("banner.minutes", { count: minutesDiff })}`; } else { return `${t("banner.days", { count: daysDiff })}, ${t( "banner.hours", diff --git a/src/Bundle/ChillTicketBundle/src/Serializer/Normalizer/TicketNormalizer.php b/src/Bundle/ChillTicketBundle/src/Serializer/Normalizer/TicketNormalizer.php index c505a835c..77dc3da26 100644 --- a/src/Bundle/ChillTicketBundle/src/Serializer/Normalizer/TicketNormalizer.php +++ b/src/Bundle/ChillTicketBundle/src/Serializer/Normalizer/TicketNormalizer.php @@ -44,8 +44,10 @@ final class TicketNormalizer implements NormalizerInterface, NormalizerAwareInte 'currentInputs' => $this->normalizer->normalize($object->getCurrentInputs(), $format, ['groups' => 'read']), 'currentMotive' => $this->normalizer->normalize($object->getMotive(), $format, ['groups' => 'read']), 'history' => array_values($this->serializeHistory($object, $format, ['groups' => 'read'])), - 'createdAt' => $object->getCreatedAt(), - 'updatedBy' => $object->getUpdatedBy(), + 'createdAt' => $this->normalizer->normalize($object->getCreatedAt(), $format, $context), + 'updatedAt' => $this->normalizer->normalize($object->getUpdatedAt(), $format, $context), + 'updatedBy' => $this->normalizer->normalize($object->getUpdatedBy(), $format, $context), + 'createdBy' => $this->normalizer->normalize($object->getCreatedBy(), $format, $context), ]; }