From 9feb218889367d830abcbbf9016dd3b84aecc355 Mon Sep 17 00:00:00 2001 From: aestene Date: Tue, 26 Sep 2023 12:19:37 +0200 Subject: [PATCH] Handle the case where frequency or lastRun is null --- frontend/src/utils/StringFormatting.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/src/utils/StringFormatting.tsx b/frontend/src/utils/StringFormatting.tsx index e23ebcc3a..0cd146ee8 100644 --- a/frontend/src/utils/StringFormatting.tsx +++ b/frontend/src/utils/StringFormatting.tsx @@ -2,21 +2,23 @@ export const formatBackendDateTimeToDate = (date: Date) => { return new Date(date.toString()) } -export const getInspectionDeadline = (inspectionFrequency: string, lastRunTime: Date): Date => { +export const getInspectionDeadline = ( + inspectionFrequency: string | null, + lastRunTime: Date | null +): Date | undefined => { + if (!inspectionFrequency || !lastRunTime) return undefined + const dayHourSecondsArray = inspectionFrequency.split(':') const days: number = +dayHourSecondsArray[0] const hours: number = +dayHourSecondsArray[1] const minutes: number = +dayHourSecondsArray[2] lastRunTime = formatBackendDateTimeToDate(lastRunTime) - let deadline = lastRunTime deadline.setDate(deadline.getDate() + days) deadline.setHours(deadline.getHours() + hours) deadline.setMinutes(deadline.getMinutes() + minutes) return deadline - // More flexibly we can also define the deadline in terms of milliseconds: - // new Date(lastRunTime.getTime() + (1000 * 60 * days) + (1000 * 60 * 60 * hours) + (1000 * 60 * 60 * 24 * days)) } export const getDeadlineInDays = (deadlineDate: Date) => {