Skip to content

Commit

Permalink
Handle the case where frequency or lastRun is null
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Sep 26, 2023
1 parent 16babc6 commit 9feb218
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions frontend/src/utils/StringFormatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 9feb218

Please sign in to comment.