CEO reducer and misc

pull/3694/head
Andrew Watkins 2018-06-15 13:55:45 -07:00
parent 2420398384
commit 643172c4ca
14 changed files with 66 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import {Cell} from 'src/types' import {Cell} from 'src/types'
import {CellType} from 'src/types/dashboard' import {CellType, ThresholdType} from 'src/types/dashboard'
import {ColorNumber, ColorString} from 'src/types/colors' import {ColorNumber, ColorString} from 'src/types/colors'
import { import {
Axes, Axes,
@ -8,6 +8,21 @@ import {
TableOptions, TableOptions,
} from 'src/types/dashboard' } from 'src/types/dashboard'
export type Action =
| ShowCellEditorOverlayAction
| HideCellEditorOverlayAction
| ChangeCellTypeAction
| RenameCellAction
| UpdateThresholdsListColorsAction
| UpdateThresholdsListTypeAction
| UpdateGaugeColorsAction
| UpdateAxesAction
| UpdateTableOptionsAction
| UpdateLineColorsAction
| ChangeTimeFormatAction
| ChangeDecimalPlacesAction
| UpdateFieldOptionsAction
interface ShowCellEditorOverlayAction { interface ShowCellEditorOverlayAction {
type: 'SHOW_CELL_EDITOR_OVERLAY' type: 'SHOW_CELL_EDITOR_OVERLAY'
payload: { payload: {
@ -36,6 +51,7 @@ interface ChangeCellTypeAction {
cellType: CellType cellType: CellType
} }
} }
export const changeCellType = (cellType: CellType): ChangeCellTypeAction => ({ export const changeCellType = (cellType: CellType): ChangeCellTypeAction => ({
type: 'CHANGE_CELL_TYPE', type: 'CHANGE_CELL_TYPE',
payload: { payload: {
@ -74,11 +90,12 @@ export const updateThresholdsListColors = (
interface UpdateThresholdsListTypeAction { interface UpdateThresholdsListTypeAction {
type: 'UPDATE_THRESHOLDS_LIST_TYPE' type: 'UPDATE_THRESHOLDS_LIST_TYPE'
payload: { payload: {
thresholdsListType: string thresholdsListType: ThresholdType
} }
} }
export const updateThresholdsListType = ( export const updateThresholdsListType = (
thresholdsListType: string thresholdsListType: ThresholdType
): UpdateThresholdsListTypeAction => ({ ): UpdateThresholdsListTypeAction => ({
type: 'UPDATE_THRESHOLDS_LIST_TYPE', type: 'UPDATE_THRESHOLDS_LIST_TYPE',
payload: { payload: {

View File

@ -8,7 +8,7 @@ export const initializeOptions = (cellType: CellType) => {
case 'table': case 'table':
return DEFAULT_TABLE_OPTIONS return DEFAULT_TABLE_OPTIONS
default: default:
return {} return DEFAULT_TABLE_OPTIONS
} }
} }

View File

@ -1,13 +1,12 @@
import _ from 'lodash' import {getDeep} from 'src/utils/wrappers'
import { import {
THRESHOLD_TYPE_TEXT,
DEFAULT_THRESHOLDS_LIST_COLORS, DEFAULT_THRESHOLDS_LIST_COLORS,
DEFAULT_GAUGE_COLORS, DEFAULT_GAUGE_COLORS,
validateGaugeColors, validateGaugeColors,
validateThresholdsListColors, validateThresholdsListColors,
getThresholdsListType, getThresholdsListType,
} from 'shared/constants/thresholds' } from 'src/shared/constants/thresholds'
import { import {
DEFAULT_LINE_COLORS, DEFAULT_LINE_COLORS,
@ -15,16 +14,31 @@ import {
} from 'src/shared/constants/graphColorPalettes' } from 'src/shared/constants/graphColorPalettes'
import {initializeOptions} from 'src/dashboards/constants/cellEditor' import {initializeOptions} from 'src/dashboards/constants/cellEditor'
import {Action} from 'src/dashboards/actions/cellEditorOverlay'
import {CellType, Cell} from 'src/types'
import {ThresholdType, TableOptions} from 'src/types/dashboard'
import {ThresholdColor, GaugeColor, LineColor} from 'src/types/colors'
interface CEOInitialState {
cell: Cell | null
thresholdsListType: ThresholdType
thresholdsListColors: ThresholdColor[]
gaugeColors: GaugeColor[]
lineColors: LineColor[]
}
export const initialState = { export const initialState = {
cell: null, cell: null,
thresholdsListType: THRESHOLD_TYPE_TEXT, thresholdsListType: ThresholdType.Text,
thresholdsListColors: DEFAULT_THRESHOLDS_LIST_COLORS, thresholdsListColors: DEFAULT_THRESHOLDS_LIST_COLORS,
gaugeColors: DEFAULT_GAUGE_COLORS, gaugeColors: DEFAULT_GAUGE_COLORS,
lineColors: DEFAULT_LINE_COLORS, lineColors: DEFAULT_LINE_COLORS,
} }
export default function cellEditorOverlay(state = initialState, action) { const cellEditorOverlay = (
state = initialState,
action: Action
): CEOInitialState => {
switch (action.type) { switch (action.type) {
case 'SHOW_CELL_EDITOR_OVERLAY': { case 'SHOW_CELL_EDITOR_OVERLAY': {
const { const {
@ -39,10 +53,10 @@ export default function cellEditorOverlay(state = initialState, action) {
) )
const gaugeColors = validateGaugeColors(colors) const gaugeColors = validateGaugeColors(colors)
const tableOptions = _.get( const tableOptions = getDeep<TableOptions>(
cell, cell,
'tableOptions', 'tableOptions',
initializeOptions('table') initializeOptions(CellType.Table)
) )
const lineColors = validateLineColors(colors) const lineColors = validateLineColors(colors)
@ -142,3 +156,5 @@ export default function cellEditorOverlay(state = initialState, action) {
return state return state
} }
export default cellEditorOverlay

View File

@ -47,6 +47,7 @@ import {
DygraphSeries, DygraphSeries,
Constructable, Constructable,
} from 'src/types' } from 'src/types'
import {LineColor} from 'src/types/colors'
const Dygraphs = D as Constructable<DygraphClass> const Dygraphs = D as Constructable<DygraphClass>
@ -59,7 +60,7 @@ interface Props {
containerStyle: object // TODO containerStyle: object // TODO
dygraphSeries: DygraphSeries dygraphSeries: DygraphSeries
timeRange: TimeRange timeRange: TimeRange
colors: object colors: LineColor[]
handleSetHoverTime: (t: string) => void handleSetHoverTime: (t: string) => void
ruleValues?: RuleValues ruleValues?: RuleValues
axes?: Axes axes?: Axes

View File

@ -1,5 +1,6 @@
import chroma from 'chroma-js' import chroma from 'chroma-js'
import uuid from 'uuid' import uuid from 'uuid'
import {LineColor} from 'src/types/colors'
const COLOR_TYPE_SCALE = 'scale' const COLOR_TYPE_SCALE = 'scale'
@ -207,7 +208,10 @@ export const LINE_COLOR_SCALES = [
return {name, colors, id} return {name, colors, id}
}) })
export const validateLineColors = (colors, numSeries) => { export const validateLineColors = (
colors: LineColor[],
numSeries = 0
): LineColor[] => {
const multipleSeriesButOneColor = numSeries > 1 && colors.length < 2 const multipleSeriesButOneColor = numSeries > 1 && colors.length < 2
if (!colors || colors.length === 0 || multipleSeriesButOneColor) { if (!colors || colors.length === 0 || multipleSeriesButOneColor) {
return DEFAULT_LINE_COLORS return DEFAULT_LINE_COLORS
@ -220,7 +224,10 @@ export const validateLineColors = (colors, numSeries) => {
return testColorsTypes ? colors : DEFAULT_LINE_COLORS return testColorsTypes ? colors : DEFAULT_LINE_COLORS
} }
export const getLineColorsHexes = (colors, numSeries) => { export const getLineColorsHexes = (
colors: LineColor[],
numSeries: number
): string[] => {
const validatedColors = validateLineColors(colors, numSeries) // ensures safe defaults const validatedColors = validateLineColors(colors, numSeries) // ensures safe defaults
const colorsHexArray = validatedColors.map(color => color.hex) const colorsHexArray = validatedColors.map(color => color.hex)

View File

@ -7,6 +7,8 @@ interface ColorBase {
export type ColorString = ColorBase & {value: string} export type ColorString = ColorBase & {value: string}
export type ColorNumber = ColorBase & {value: number} export type ColorNumber = ColorBase & {value: number}
export type LineColor = ColorString
export type GaugeColor = ColorString | ColorNumber
export interface ThresholdColor { export interface ThresholdColor {
hex: string hex: string

View File

@ -121,3 +121,9 @@ export interface DashboardFile {
meta?: DashboardFileMetaSection meta?: DashboardFileMetaSection
dashboard: Dashboard dashboard: Dashboard
} }
export enum ThresholdType {
Text = 'text',
BG = 'background',
Base = 'base',
}

View File

@ -52,7 +52,7 @@ describe('Dashboards.Reducers.cellEditorOverlay', () => {
}) })
it('should hide cell editor overlay', () => { it('should hide cell editor overlay', () => {
const actual = reducer(initialState, hideCellEditorOverlay) const actual = reducer(initialState, hideCellEditorOverlay())
const expected = null const expected = null
expect(actual.cell).toBe(expected) expect(actual.cell).toBe(expected)

View File

@ -14,7 +14,7 @@ import {
FieldOption, FieldOption,
DecimalPlaces, DecimalPlaces,
} from 'src/types/dashboard' } from 'src/types/dashboard'
import {ColorString, ColorNumber} from 'src/types/colors' import {LineColor, ColorNumber} from 'src/types/colors'
import {CellType} from 'src/types/dashboard' import {CellType} from 'src/types/dashboard'
export const sourceLinks: SourceLinks = { export const sourceLinks: SourceLinks = {
@ -131,7 +131,7 @@ export const tableOptions: TableOptions = {
wrapping: 'truncate', wrapping: 'truncate',
fixFirstColumn: true, fixFirstColumn: true,
} }
export const lineColors: ColorString[] = [ export const lineColors: LineColor[] = [
{ {
id: '574fb0a3-0a26-44d7-8d71-d4981756acb1', id: '574fb0a3-0a26-44d7-8d71-d4981756acb1',
type: 'scale', type: 'scale',