From 4ef173aac9ae6c5733ac57b22889ec0cd284b3ae Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Fri, 17 Feb 2017 12:17:44 -0600 Subject: [PATCH] Add dashboard timeRange to reducer --- ui/spec/dashboards/reducers/uiSpec.js | 45 ++++++++++++++------------- ui/src/dashboards/actions/index.js | 9 ++++++ ui/src/dashboards/reducers/ui.js | 10 ++++++ 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/ui/spec/dashboards/reducers/uiSpec.js b/ui/spec/dashboards/reducers/uiSpec.js index 69fe7ba2fc..5a1b3e7709 100644 --- a/ui/spec/dashboards/reducers/uiSpec.js +++ b/ui/spec/dashboards/reducers/uiSpec.js @@ -3,46 +3,49 @@ import reducer from 'src/dashboards/reducers/ui' import { loadDashboards, setDashboard, -} from 'src/dashboards/actions'; + setTimeRange, +} from 'src/dashboards/actions' const noopAction = () => { - return {type: 'NOOP'}; + return {type: 'NOOP'} } -let state = undefined; +let state = undefined +const timeRange = {upper: null, lower: 'now() - 15m'} +const d1 = {id: 1, cells: [], name: "d1"} +const d2 = {id: 2, cells: [], name: "d2"} +const dashboards = [d1, d2] describe('DataExplorer.Reducers.UI', () => { it('can load the dashboards', () => { - const dashboard = {id: 2, cells: [], name: "d2"} - const dashboards = [ - {id: 1, cells: [], name: "d1"}, - dashboard, - ] - - const actual = reducer(state, loadDashboards(dashboards, dashboard.id)) + const actual = reducer(state, loadDashboards(dashboards, d1.id)) const expected = { dashboards, - dashboard, + dashboard: d1, + timeRange, } expect(actual).to.deep.equal(expected) - }); + }) it('can set a dashboard', () => { - const d1 = {id: 2, cells: [], name: "d2"} - const d2 = {id: 2, cells: [], name: "d2"} - const dashboards = [ - d1, - d2, - ] - const loadedState = reducer(state, loadDashboards(dashboards, d1.id)) const actual = reducer(loadedState, setDashboard(d2.id)) const expected = { dashboards, dashboard: d2, + timeRange, } expect(actual).to.deep.equal(expected) - }); -}); + }) + + it('can set the time range', () => { + const expected = {upper: null, lower: 'now() - 1h'} + const actual = reducer(state, setTimeRange(expected)) + + console.log('actual: ', actual) + + expect(actual.timeRange).to.deep.equal(expected) + }) +}) diff --git a/ui/src/dashboards/actions/index.js b/ui/src/dashboards/actions/index.js index a6902d939a..197245a6f1 100644 --- a/ui/src/dashboards/actions/index.js +++ b/ui/src/dashboards/actions/index.js @@ -19,6 +19,15 @@ export function setDashboard(dashboardID) { } } +export function setTimeRange(timeRange) { + return { + type: 'SET_DASHBOARD_TIME_RANGE', + payload: { + timeRange, + }, + } +} + export function getDashboards(dashboardID) { return (dispatch) => { getDashboardsAPI().then(({data: {dashboards}}) => { diff --git a/ui/src/dashboards/reducers/ui.js b/ui/src/dashboards/reducers/ui.js index d9de09f26c..c2e5df66c1 100644 --- a/ui/src/dashboards/reducers/ui.js +++ b/ui/src/dashboards/reducers/ui.js @@ -5,6 +5,10 @@ import {EMPTY_DASHBOARD} from 'src/dashboards/constants' const initialState = { dashboards: [], dashboard: EMPTY_DASHBOARD, + timeRange: { + upper: null, + lower: 'now() - 15m', + }, }; export default function ui(state = initialState, action) { @@ -27,6 +31,12 @@ export default function ui(state = initialState, action) { return {...state, ...newState} } + + case 'SET_DASHBOARD_TIME_RANGE': { + const {timeRange} = action.payload + + return {...state, timeRange}; + } } return state;