Refactor time range validation logic to always return TimeRange
Clean up namespace & logic flow in syncDashboardTimeRangeFromURLQueries. Authored-by: Jared Scheib <jared.scheib@gmail.com>pull/10616/head
parent
65441fe91c
commit
2c581d288e
|
@ -474,34 +474,28 @@ const syncDashboardTimeRangeFromURLQueries = (
|
||||||
urlQueries,
|
urlQueries,
|
||||||
location
|
location
|
||||||
) => (dispatch, getState) => {
|
) => (dispatch, getState) => {
|
||||||
const dashboard = getState().dashboardUI.dashboards.find(
|
|
||||||
d => d.id === dashboardID
|
|
||||||
)
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
upper = null,
|
dashboardUI: {dashboards},
|
||||||
lower = null,
|
dashTimeV1,
|
||||||
zoomedUpper = null,
|
} = getState()
|
||||||
zoomedLower = null,
|
const dashboard = dashboards.find(d => d.id === dashboardID)
|
||||||
} = urlQueries
|
|
||||||
|
|
||||||
let timeRange
|
|
||||||
const {dashTimeV1} = getState()
|
|
||||||
|
|
||||||
const timeRangeFromQueries = {
|
const timeRangeFromQueries = {
|
||||||
upper: urlQueries.upper,
|
upper: urlQueries.upper,
|
||||||
lower: urlQueries.lower,
|
lower: urlQueries.lower,
|
||||||
}
|
}
|
||||||
const timeRangeOrNull = validTimeRange(timeRangeFromQueries)
|
const zoomedTimeRangeFromQueries = {
|
||||||
|
lower: urlQueries.zoomedLower,
|
||||||
|
upper: urlQueries.zoomedUpper,
|
||||||
|
}
|
||||||
|
|
||||||
if (timeRangeOrNull) {
|
let validatedTimeRange = validTimeRange(timeRangeFromQueries)
|
||||||
timeRange = timeRangeOrNull
|
if (!validatedTimeRange.lower) {
|
||||||
} else {
|
|
||||||
const dashboardTimeRange = dashTimeV1.ranges.find(
|
const dashboardTimeRange = dashTimeV1.ranges.find(
|
||||||
r => r.dashboardID === idNormalizer(TYPE_ID, dashboardID)
|
r => r.dashboardID === idNormalizer(TYPE_ID, dashboardID)
|
||||||
)
|
)
|
||||||
|
|
||||||
timeRange = dashboardTimeRange || defaultTimeRange
|
validatedTimeRange = dashboardTimeRange || defaultTimeRange
|
||||||
|
|
||||||
if (timeRangeFromQueries.lower || timeRangeFromQueries.upper) {
|
if (timeRangeFromQueries.lower || timeRangeFromQueries.upper) {
|
||||||
dispatch(
|
dispatch(
|
||||||
|
@ -509,22 +503,23 @@ const syncDashboardTimeRangeFromURLQueries = (
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dispatch(setDashTimeV1(dashboardID, validatedTimeRange))
|
||||||
|
|
||||||
dispatch(setDashTimeV1(dashboardID, timeRange))
|
const validatedZoomedTimeRange = validAbsoluteTimeRange(
|
||||||
|
zoomedTimeRangeFromQueries
|
||||||
if (!validAbsoluteTimeRange({lower: zoomedLower, upper: zoomedUpper})) {
|
)
|
||||||
if (zoomedLower || zoomedUpper) {
|
if (
|
||||||
dispatch(notify(notifyInvalidZoomedTimeRangeValueInURLQuery()))
|
!validatedZoomedTimeRange.lower &&
|
||||||
}
|
(urlQueries.zoomedLower || urlQueries.zoomedUpper)
|
||||||
|
) {
|
||||||
|
dispatch(notify(notifyInvalidZoomedTimeRangeValueInURLQuery()))
|
||||||
}
|
}
|
||||||
|
dispatch(setZoomedTimeRange(validatedZoomedTimeRange))
|
||||||
dispatch(setZoomedTimeRange({zoomedLower, zoomedUpper}))
|
|
||||||
|
|
||||||
const urlQueryTimeRanges = {
|
const urlQueryTimeRanges = {
|
||||||
upper,
|
lower: validatedTimeRange.lower,
|
||||||
lower,
|
upper: validatedTimeRange.upper,
|
||||||
zoomedUpper,
|
zoomedLower: validatedZoomedTimeRange.lower,
|
||||||
zoomedLower,
|
zoomedUpper: validatedZoomedTimeRange.upper,
|
||||||
}
|
}
|
||||||
dispatch(
|
dispatch(
|
||||||
syncURLQueryFromTempVars(
|
syncURLQueryFromTempVars(
|
||||||
|
|
|
@ -34,35 +34,37 @@ export const millisecondTimeRange = ({
|
||||||
return {since, until}
|
return {since, until}
|
||||||
}
|
}
|
||||||
|
|
||||||
const validRelativeTimeRange = (timeRange: TimeRange): TimeRange | null => {
|
const nullTimeRange: TimeRange = {lower: null, upper: null}
|
||||||
const matchedRange = timeRanges.find(t => t.lower === timeRange.lower)
|
|
||||||
|
|
||||||
if (matchedRange) {
|
const validRelativeTimeRange = (timeRange: TimeRange): TimeRange => {
|
||||||
return matchedRange
|
const validatedTimeRange = timeRanges.find(t => t.lower === timeRange.lower)
|
||||||
|
|
||||||
|
if (validatedTimeRange) {
|
||||||
|
return validatedTimeRange
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return nullTimeRange
|
||||||
}
|
}
|
||||||
|
|
||||||
export const validAbsoluteTimeRange = (
|
export const validAbsoluteTimeRange = (timeRange: TimeRange): TimeRange => {
|
||||||
timeRange: TimeRange
|
if (timeRange.lower && timeRange.upper) {
|
||||||
): TimeRange | null => {
|
if (moment(timeRange.lower).isValid()) {
|
||||||
if (
|
if (
|
||||||
timeRange.lower &&
|
timeRange.upper === 'now()' ||
|
||||||
timeRange.upper &&
|
(moment(timeRange.upper).isValid() &&
|
||||||
moment(timeRange.lower).isValid() &&
|
moment(timeRange.lower).isBefore(moment(timeRange.upper)))
|
||||||
moment(timeRange.upper).isValid() &&
|
) {
|
||||||
moment(timeRange.lower).isBefore(moment(timeRange.upper))
|
return timeRange
|
||||||
) {
|
}
|
||||||
return timeRange
|
}
|
||||||
}
|
}
|
||||||
return null
|
return nullTimeRange
|
||||||
}
|
}
|
||||||
|
|
||||||
export const validTimeRange = (timeRange: TimeRange): TimeRange | null => {
|
export const validTimeRange = (timeRange: TimeRange): TimeRange => {
|
||||||
let timeRangeOrNull = validRelativeTimeRange(timeRange)
|
const validatedTimeRange = validRelativeTimeRange(timeRange)
|
||||||
if (!timeRangeOrNull) {
|
if (validatedTimeRange.lower) {
|
||||||
timeRangeOrNull = validAbsoluteTimeRange(timeRange)
|
return validatedTimeRange
|
||||||
}
|
}
|
||||||
return timeRangeOrNull
|
return validAbsoluteTimeRange(timeRange)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue