WIP Validate timeRange from URL query param
parent
0a2d817586
commit
b831c08c56
|
@ -1,3 +1,5 @@
|
|||
import {TimeRange} from 'src/types/query'
|
||||
|
||||
interface InputTimeRange {
|
||||
seconds?: number
|
||||
lower?: string
|
||||
|
@ -28,3 +30,10 @@ export const millisecondTimeRange = ({
|
|||
}
|
||||
return {since, until}
|
||||
}
|
||||
|
||||
export const validTimeRange = (timeRange: TimeRange): boolean => {
|
||||
if (!timeRange || !timeRange.lower) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -438,10 +438,18 @@ export const notifyBuilderDisabled = () => ({
|
|||
message: `Your query contains a user-defined Template Variable. The Schema Explorer cannot render the query and is disabled.`,
|
||||
})
|
||||
|
||||
// Template Variables & URL Queries
|
||||
// ----------------------------------------------------------------------------
|
||||
export const notifyInvalidTempVarValueInURLQuery = ({key, value}) => ({
|
||||
...defaultErrorNotification,
|
||||
icon: 'cube',
|
||||
message: `Invalid URL query value of '${value}' supplied for template variable '${key}'.`,
|
||||
message: `Invalid URL query value supplied for template variable '${key}': '${value}'.`,
|
||||
})
|
||||
|
||||
export const notifyInvalidTimeRangeValueInURLQuery = () => ({
|
||||
...defaultErrorNotification,
|
||||
icon: 'cube',
|
||||
message: `Invalid URL query value supplied for lower or upper time range.`,
|
||||
})
|
||||
|
||||
// Rule Builder Notifications
|
||||
|
|
|
@ -3,8 +3,11 @@ import queryString from 'query-string'
|
|||
|
||||
import {enablePresentationMode} from 'src/shared/actions/app'
|
||||
import {setDashTimeV1} from 'src/dashboards/actions'
|
||||
import {notify as notifyAction} from 'shared/actions/notifications'
|
||||
import {notifyInvalidTimeRangeValueInURLQuery} from 'shared/copy/notifications'
|
||||
import {timeRanges, defaultTimeRange} from 'src/shared/data/timeRanges'
|
||||
import idNormalizer, {TYPE_ID} from 'src/normalizers/id'
|
||||
import {validTimeRange} from 'src/dashboards/utils/time'
|
||||
|
||||
export const queryStringConfig = store => {
|
||||
let prevPath
|
||||
|
@ -24,14 +27,15 @@ export const queryStringConfig = store => {
|
|||
if (currentPath !== prevPath) {
|
||||
const {dashTimeV1} = store.getState()
|
||||
|
||||
const foundTimeRange = dashTimeV1.ranges.find(
|
||||
const dashboardTimeRange = dashTimeV1.ranges.find(
|
||||
r => r.dashboardID === idNormalizer(TYPE_ID, dashboardID)
|
||||
)
|
||||
|
||||
let timeRange = foundTimeRange || defaultTimeRange
|
||||
if (!dashboardTimeRange) {
|
||||
return
|
||||
}
|
||||
|
||||
// if lower and upper in urlQueries.
|
||||
// and if valid.
|
||||
let timeRange = dashboardTimeRange
|
||||
|
||||
if (urlQueries.upper) {
|
||||
timeRange = {
|
||||
|
@ -42,9 +46,17 @@ export const queryStringConfig = store => {
|
|||
} else {
|
||||
timeRange = timeRanges.find(t => t.lower === urlQueries.lower)
|
||||
}
|
||||
if (foundTimeRange) {
|
||||
dispatch(setDashTimeV1(+dashboardID, timeRange))
|
||||
|
||||
const isValidTimeRange = validTimeRange(timeRange)
|
||||
|
||||
if (!isValidTimeRange) {
|
||||
dispatch(
|
||||
notifyAction(notifyInvalidTimeRangeValueInURLQuery(timeRange))
|
||||
)
|
||||
timeRange = defaultTimeRange
|
||||
}
|
||||
|
||||
dispatch(setDashTimeV1(+dashboardID, timeRange))
|
||||
}
|
||||
prevPath = currentPath
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue