Skip to content

Commit

Permalink
Fix [Scheduled, cross-projects] Custom range sets an invalid date by …
Browse files Browse the repository at this point in the history
…default (#2550)
  • Loading branch information
Taras-Hlukhovetskyi committed Jun 23, 2024
1 parent 01f82fc commit d2ff364
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
24 changes: 15 additions & 9 deletions src/common/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
ANY_TIME_DATE_OPTION,
getTimeFrameWarningMsg,
CUSTOM_RANGE_DATE_OPTION,
roundSeconds,
} from '../../utils/datePicker.util'
import { initialState, datePickerActions, datePickerReducer } from './datePickerReducer'
import { DATE_PICKER_TIME_FRAME_LIMITS } from '../../types'
Expand Down Expand Up @@ -163,14 +164,14 @@ const DatePicker = ({
useEffect(() => {
datePickerDispatch({
type: datePickerActions.UPDATE_DATE_FROM,
payload: date || new Date()
payload: date || roundSeconds(new Date())
})
}, [date])

useEffect(() => {
datePickerDispatch({
type: datePickerActions.UPDATE_DATE_TO,
payload: dateTo || new Date()
payload: dateTo || roundSeconds(new Date(), true)
})
}, [dateTo])

Expand Down Expand Up @@ -334,10 +335,10 @@ const DatePicker = ({
setInputIsInvalid(false)
setExternalInvalid(true)

let dates = [new Date(datePickerState.configFrom.selectedDate)]
let dates = [roundSeconds(datePickerState.configFrom.selectedDate)]

if (isRange) {
dates.push(new Date(datePickerState.configTo.selectedDate))
dates.push(roundSeconds(datePickerState.configTo.selectedDate, true))
}

onChange(dates, false, CUSTOM_RANGE_DATE_OPTION)
Expand All @@ -363,7 +364,11 @@ const DatePicker = ({
}

if (!isValueEmpty) {
dates = event.target.value.split(datesDivider).map(date => new Date(date))
dates = event.target.value
.split(datesDivider)
.map(
(date, index) => roundSeconds(date, index > 0)
)
const { isTimeRangeInvalid, timeRangeInvalidMessage } = validateTimeRange(dates)

if (isTimeRangeInvalid) {
Expand Down Expand Up @@ -437,13 +442,13 @@ const DatePicker = ({
setIsDatePickerOpened(state => !state)
datePickerDispatch({
type: datePickerActions.UPDATE_SELECTED_DATE_FROM,
payload: date || new Date()
payload: date || roundSeconds(new Date())
})

if (isRange) {
datePickerDispatch({
type: datePickerActions.UPDATE_SELECTED_DATE_TO,
payload: dateTo || new Date()
payload: dateTo || roundSeconds(new Date(), true)
})
}
}
Expand All @@ -463,7 +468,7 @@ const DatePicker = ({

const onSelectOption = option => {
if (option.handler) {
onChange(option.handler(), option.isPredefined, option.id)
onChange(option.handler(isRange), option.isPredefined, option.id)
if (isNil(externalInvalid)) {
setInputIsInvalid(false)
}
Expand Down Expand Up @@ -542,6 +547,7 @@ const DatePicker = ({
}
DatePicker.defaultProps = {
className: '',
date: new Date(),
dateTo: new Date(),
disabled: false,
externalInvalid: null,
Expand All @@ -562,7 +568,7 @@ DatePicker.defaultProps = {

DatePicker.propTypes = {
className: PropTypes.string,
date: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.string]).isRequired,
date: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.string]),
dateTo: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.string]),
disabled: PropTypes.bool,
externalInvalid: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const ProjectsJobsMonitoring = ({ fetchAllJobRuns, fetchJobFunction, fetchJobs }
initialValue: {
value: datePickerFutureOptions
.find(option => option.id === NEXT_24_HOUR_DATE_OPTION)
.handler(),
.handler(true),
isPredefined: true,
initialSelectedOptionId: NEXT_24_HOUR_DATE_OPTION
},
Expand Down Expand Up @@ -261,8 +261,8 @@ const ProjectsJobsMonitoring = ({ fetchAllJobRuns, fetchJobFunction, fetchJobs }
let inDateRange = true

if (filters.dates) {
const timeTo = filters.dates.value[0]?.getTime?.() || ''
const timeFrom = filters.dates.value[1]?.getTime?.() || ''
const timeTo = filters.dates.value[1]?.getTime?.() || ''
const timeFrom = filters.dates.value[0]?.getTime?.() || ''
const nextRun = job.nextRun.getTime()

if (timeFrom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const ScheduledMonitoring = () => {

filters = {
dates: {
value: next24HourOption.handler(),
value: next24HourOption.handler(true),
isPredefined: next24HourOption.isPredefined,
initialSelectedOptionId: next24HourOption.id
},
Expand Down
32 changes: 21 additions & 11 deletions src/utils/datePicker.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,54 +142,54 @@ export const datePickerFutureOptions = [
id: NEXT_HOUR_DATE_OPTION,
label: 'Next hour',
isPredefined: true,
handler: () => {
handler: (isRange) => {
return getDates((fromDate, toDate) => {
fromDate.setHours(toDate.getHours() + 1)
})
}, true, isRange)
},
timeFrameMilliseconds: TIME_FRAME_LIMITS.HOUR
},
{
id: NEXT_24_HOUR_DATE_OPTION,
label: 'Next 24 hours',
isPredefined: true,
handler: () => {
handler: (isRange) => {
return getDates((fromDate, toDate) => {
fromDate.setDate(toDate.getDate() + 1)
})
}, true, isRange)
},
timeFrameMilliseconds: TIME_FRAME_LIMITS['24_HOURS']
},
{
id: NEXT_WEEK_DATE_OPTION,
label: 'Next week',
isPredefined: true,
handler: () => {
handler: (isRange) => {
return getDates((fromDate, toDate) => {
fromDate.setDate(toDate.getDate() + 7)
})
}, true, isRange)
},
timeFrameMilliseconds: TIME_FRAME_LIMITS.WEEK
},
{
id: NEXT_MONTH_DATE_OPTION,
label: 'Next month',
isPredefined: true,
handler: () => {
handler: (isRange) => {
return getDates((fromDate, toDate) => {
fromDate.setMonth(toDate.getMonth() + 1)
})
}, true, isRange)
},
timeFrameMilliseconds: TIME_FRAME_LIMITS.MONTH
},
{
id: NEXT_YEAR_DATE_OPTION,
label: 'Next year',
isPredefined: true,
handler: () => {
handler: (isRange) => {
return getDates((fromDate, toDate) => {
fromDate.setFullYear(toDate.getFullYear() + 1)
})
}, true, isRange)
},
timeFrameMilliseconds: TIME_FRAME_LIMITS.YEAR
},
Expand All @@ -201,12 +201,14 @@ export const datePickerFutureOptions = [
}
]

const getDates = setDate => {
const getDates = (setDate, isFutureTime, isRange) => {
let fromDate = new Date()
let toDate = new Date()

setDate(fromDate, toDate)

if (isFutureTime && isRange) return [null, fromDate]

return [fromDate]
}

Expand Down Expand Up @@ -353,3 +355,11 @@ export const getTimeFrameWarningMsg = (timeFrameLimit) => {

return `Maximum time range is ${mappedTime[timeFrameLimit]}`
}


export const roundSeconds = (date = new Date(), top = false) => {
const seconds = top ? 59 : 0
const milliseconds = top ? 999 : 0

return new Date(new Date(date).setSeconds(seconds, milliseconds))
}

0 comments on commit d2ff364

Please sign in to comment.