Commit a431efa0 by Hugo Häggmark

Refactored logic in ThresholdEditor

parent 030d20d9
...@@ -15,25 +15,56 @@ const setup = (propOverrides?: object) => { ...@@ -15,25 +15,56 @@ const setup = (propOverrides?: object) => {
return shallow(<ThresholdsEditor {...props} />).instance() as ThresholdsEditor; return shallow(<ThresholdsEditor {...props} />).instance() as ThresholdsEditor;
}; };
describe('Initialization', () => {
it('should add a base threshold if missing', () => {
const instance = setup();
expect(instance.state.thresholds).toEqual([{ index: 0, value: -Infinity, color: '#7EB26D' }]);
});
});
describe('Add threshold', () => { describe('Add threshold', () => {
it('should add threshold', () => { it('should add threshold', () => {
const instance = setup(); const instance = setup();
instance.onAddThreshold(0); instance.onAddThreshold(1);
expect(instance.state.thresholds).toEqual([{ index: 0, value: 50, color: 'rgb(127, 115, 64)' }]); expect(instance.state.thresholds).toEqual([
{ index: 1, value: 50, color: '#EAB839' },
{ index: 0, value: -Infinity, color: '#7EB26D' },
]);
}); });
it('should add another threshold above a first', () => { it('should add another threshold above a first', () => {
const instance = setup({ const instance = setup({
thresholds: [{ index: 0, value: 50, color: 'rgb(127, 115, 64)' }], thresholds: [{ index: 0, value: -Infinity, color: '#7EB26D' }, { index: 1, value: 50, color: '#EAB839' }],
}); });
instance.onAddThreshold(1); instance.onAddThreshold(2);
expect(instance.state.thresholds).toEqual([
{ index: 2, value: 75, color: '#6ED0E0' },
{ index: 1, value: 50, color: '#EAB839' },
{ index: 0, value: -Infinity, color: '#7EB26D' },
]);
});
it('should add another threshold between first and second index', () => {
const instance = setup({
thresholds: [
{ index: 0, value: -Infinity, color: '#7EB26D' },
{ index: 1, value: 50, color: '#EAB839' },
{ index: 2, value: 75, color: '#6ED0E0' },
],
});
instance.onAddThreshold(2);
expect(instance.state.thresholds).toEqual([ expect(instance.state.thresholds).toEqual([
{ index: 1, value: 75, color: 'rgb(170, 95, 61)' }, { index: 3, value: 75, color: '#EF843C' },
{ index: 0, value: 50, color: 'rgb(127, 115, 64)' }, { index: 2, value: 62.5, color: '#6ED0E0' },
{ index: 1, value: 50, color: '#EAB839' },
{ index: 0, value: -Infinity, color: '#7EB26D' },
]); ]);
}); });
}); });
...@@ -41,23 +72,25 @@ describe('Add threshold', () => { ...@@ -41,23 +72,25 @@ describe('Add threshold', () => {
describe('change threshold value', () => { describe('change threshold value', () => {
it('should update value and resort rows', () => { it('should update value and resort rows', () => {
const instance = setup(); const instance = setup();
const mockThresholds = [ const thresholds = [
{ index: 0, value: 50, color: 'rgba(237, 129, 40, 0.89)' }, { index: 0, value: -Infinity, color: '#7EB26D' },
{ index: 1, value: 75, color: 'rgba(237, 129, 40, 0.89)' }, { index: 1, value: 50, color: '#EAB839' },
{ index: 2, value: 75, color: '#6ED0E0' },
]; ];
instance.state = { instance.state = {
baseColor: BasicGaugeColor.Green, baseColor: BasicGaugeColor.Green,
thresholds: mockThresholds, thresholds,
}; };
const mockEvent = { target: { value: 78 } }; const mockEvent = { target: { value: 78 } };
instance.onChangeThresholdValue(mockEvent, mockThresholds[0]); instance.onChangeThresholdValue(mockEvent, thresholds[1]);
expect(instance.state.thresholds).toEqual([ expect(instance.state.thresholds).toEqual([
{ index: 0, value: 78, color: 'rgba(237, 129, 40, 0.89)' }, { index: 0, value: -Infinity, color: '#7EB26D' },
{ index: 1, value: 75, color: 'rgba(237, 129, 40, 0.89)' }, { index: 1, value: 78, color: '#EAB839' },
{ index: 2, value: 75, color: '#6ED0E0' },
]); ]);
}); });
}); });
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import tinycolor, { ColorInput } from 'tinycolor2'; // import tinycolor, { ColorInput } from 'tinycolor2';
import { Threshold, BasicGaugeColor } from '../../types'; import { Threshold, BasicGaugeColor } from '../../types';
import { ColorPicker } from '../ColorPicker/ColorPicker'; import { ColorPicker } from '../ColorPicker/ColorPicker';
import { PanelOptionsGroup } from '../PanelOptionsGroup/PanelOptionsGroup'; import { PanelOptionsGroup } from '../PanelOptionsGroup/PanelOptionsGroup';
import { colors } from '../../utils';
export interface Props { export interface Props {
thresholds: Threshold[]; thresholds: Threshold[];
...@@ -19,43 +20,41 @@ export class ThresholdsEditor extends PureComponent<Props, State> { ...@@ -19,43 +20,41 @@ export class ThresholdsEditor extends PureComponent<Props, State> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
this.state = { thresholds: props.thresholds, baseColor: BasicGaugeColor.Green }; const thresholds: Threshold[] =
props.thresholds.length > 0 ? props.thresholds : [{ index: 0, value: -Infinity, color: colors[0] }];
this.state = { thresholds, baseColor: BasicGaugeColor.Green };
} }
onAddThreshold = (index: number) => { onAddThreshold = (index: number) => {
const maxValue = 100; // hardcoded for now before we add the base threshold
const minValue = 0; // hardcoded for now before we add the base threshold
const { thresholds } = this.state; const { thresholds } = this.state;
const maxValue = 100;
const minValue = 0;
if (index === 0) {
return;
}
const newThresholds = thresholds.map(threshold => { const newThresholds = thresholds.map(threshold => {
if (threshold.index >= index) { if (threshold.index >= index) {
const index = threshold.index + 1;
threshold = { threshold = {
...threshold, ...threshold,
index: threshold.index + 1, index,
color: colors[index],
}; };
} }
return threshold; return threshold;
}); });
// Setting value to a value between the previous thresholds // Setting value to a value between the previous thresholds
let value; const beforeThreshold = newThresholds.filter(threshold => threshold.index === index - 1)[0];
const afterThreshold = newThresholds.filter(threshold => threshold.index === index + 1)[0];
if (index === 0 && thresholds.length === 0) { const beforeThresholdValue = beforeThreshold !== undefined ? Math.max(beforeThreshold.value, minValue) : minValue;
value = maxValue - (maxValue - minValue) / 2; const afterThresholdValue = afterThreshold !== undefined ? Math.min(afterThreshold.value, maxValue) : maxValue;
} else if (index === 0 && thresholds.length > 0) { const value = afterThresholdValue - (afterThresholdValue - beforeThresholdValue) / 2;
value = newThresholds[index + 1].value - (newThresholds[index + 1].value - minValue) / 2;
} else if (index > newThresholds[newThresholds.length - 1].index) { // Set a color
value = maxValue - (maxValue - newThresholds[index - 1].value) / 2; const color = colors[index];
}
// Set a color that lies between the previous thresholds
let color;
if (index === 0 && thresholds.length === 0) {
color = tinycolor.mix(BasicGaugeColor.Green, BasicGaugeColor.Red, 50).toRgbString();
} else {
color = tinycolor.mix(thresholds[index - 1].color as ColorInput, BasicGaugeColor.Red, 50).toRgbString();
}
this.setState( this.setState(
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment