From b36ab14b246333d8e47cfde9bfcd8de4e8357b28 Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Fri, 19 Jan 2018 14:18:57 -0800 Subject: [PATCH] Introduce addAnnotation action and reducer --- ui/spec/shared/reducers/annotationsSpec.js | 11 +++++++++++ ui/src/shared/actions/annotations.js | 7 +++++++ ui/src/shared/constants/annotations.js | 1 + ui/src/shared/reducers/annotations.js | 8 ++++++++ 4 files changed, 27 insertions(+) create mode 100644 ui/src/shared/constants/annotations.js diff --git a/ui/spec/shared/reducers/annotationsSpec.js b/ui/spec/shared/reducers/annotationsSpec.js index 7488768dee..36a64d77b2 100644 --- a/ui/spec/shared/reducers/annotationsSpec.js +++ b/ui/spec/shared/reducers/annotationsSpec.js @@ -1,11 +1,14 @@ import reducer from 'shared/reducers/annotations' import { + addAnnotation, deleteAnnotation, loadAnnotations, updateAnnotation, } from 'shared/actions/annotations' +import {DEFAULT_ANNOTATION_ID} from 'src/shared/constants/annotations' + const a1 = { id: '1', group: '', @@ -48,4 +51,12 @@ describe.only('Shared.Reducers.annotations', () => { expect(actual).to.deep.equal(expected) }) + + it('can add an annotation', () => { + const state = [] + const expected = [{...a1, id: DEFAULT_ANNOTATION_ID}] + const actual = reducer(state, addAnnotation(a1)) + + expect(actual).to.deep.equal(expected) + }) }) diff --git a/ui/src/shared/actions/annotations.js b/ui/src/shared/actions/annotations.js index c2028a2d07..c72511dc9b 100644 --- a/ui/src/shared/actions/annotations.js +++ b/ui/src/shared/actions/annotations.js @@ -18,3 +18,10 @@ export const deleteAnnotation = annotation => ({ annotation, }, }) + +export const addAnnotation = annotation => ({ + type: 'ADD_ANNOTATION', + payload: { + annotation, + }, +}) diff --git a/ui/src/shared/constants/annotations.js b/ui/src/shared/constants/annotations.js new file mode 100644 index 0000000000..fbbdccf60b --- /dev/null +++ b/ui/src/shared/constants/annotations.js @@ -0,0 +1 @@ +export const DEFAULT_ANNOTATION_ID = 'DEFAULT_ANNOTATION_ID' diff --git a/ui/src/shared/reducers/annotations.js b/ui/src/shared/reducers/annotations.js index 2b2ad91797..eaf49974d5 100644 --- a/ui/src/shared/reducers/annotations.js +++ b/ui/src/shared/reducers/annotations.js @@ -1,3 +1,5 @@ +import {DEFAULT_ANNOTATION_ID} from 'src/shared/constants/annotations' + const initialState = [ { id: '0', @@ -36,6 +38,12 @@ const annotationsReducer = (state = initialState, action) => { return newState } + + case 'ADD_ANNOTATION': { + const {annotation} = action.payload + + return [...state, {...annotation, id: DEFAULT_ANNOTATION_ID}] + } } return state