Skip to content

Commit 732e35a

Browse files
authored
feat(events): Add toggleAnnotationMode events (#522)
* feat(events): Add toggleAnnotationMode events * Add middleware handler that will emit an `annotations_mode_change` when toggleAnnotationMode is dispatched. * feat(events): Add toggleAnnotationMode events * Update test description * feat(events): Add toggleAnnotationMode events * Addressing PR Feedback * feat(events): Add toggleAnnotationMode events * Add object around emitted nextState item
1 parent c86ae59 commit 732e35a

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/@types/events.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ enum Event {
33
ACTIVE_SET = 'annotations_active_set',
44
ANNOTATION_CREATE = 'annotations_create',
55
ANNOTATION_FETCH_ERROR = 'annotations_fetch_error',
6-
ANNOTATIONS_INITIALIZED = 'annotations_initialized',
76
ANNOTATION_REMOVE = 'annotations_remove',
7+
ANNOTATIONS_INITIALIZED = 'annotations_initialized',
8+
ANNOTATIONS_MODE_CHANGE = 'annotations_mode_change',
89
VISIBLE_SET = 'annotations_visible_set',
910
}
1011

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import eventManager from '../../../common/EventManager';
2+
import { AppState } from '../../types';
3+
import { handleToggleAnnotationModeAction } from '../mode';
4+
5+
jest.mock('../../../common/EventManager');
6+
7+
describe('store/eventing/mode', () => {
8+
test('should emit annotations_mode_change with the next mode when changing annotation modes.', () => {
9+
const nextState = {
10+
common: {
11+
mode: 'region',
12+
},
13+
} as AppState;
14+
15+
handleToggleAnnotationModeAction({} as AppState, nextState);
16+
17+
expect(eventManager.emit).toBeCalledWith('annotations_mode_change', { mode: 'region' });
18+
});
19+
});

src/store/eventing/middleware.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {
66
setActiveAnnotationIdAction,
77
setIsInitialized,
88
} from '../annotations/actions';
9+
import { EventHandlerMap } from './types';
910
import { handleActiveAnnotationEvents } from './active';
11+
import { handleAnnotationsInitialized } from './init';
1012
import { handleCreateErrorEvents, handleCreatePendingEvents, handleCreateSuccessEvents } from './create';
1113
import { handleFetchErrorEvents } from './fetch';
12-
import { handleAnnotationsInitialized } from './init';
13-
import { EventHandlerMap } from './types';
14+
import { handleToggleAnnotationModeAction } from './mode';
15+
import { toggleAnnotationModeAction } from '../common/actions';
1416

1517
// Array of event handlers based on redux action. To add handling for new events add an entry keyed by action
1618
const eventHandlers: EventHandlerMap = {
@@ -20,6 +22,7 @@ const eventHandlers: EventHandlerMap = {
2022
[fetchAnnotationsAction.rejected.toString()]: handleFetchErrorEvents,
2123
[setActiveAnnotationIdAction.toString()]: handleActiveAnnotationEvents,
2224
[setIsInitialized.toString()]: handleAnnotationsInitialized,
25+
[toggleAnnotationModeAction.toString()]: handleToggleAnnotationModeAction,
2326
};
2427

2528
function getEventingMiddleware(handlers: EventHandlerMap = eventHandlers): Middleware {

src/store/eventing/mode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import eventManager from '../../common/EventManager';
2+
import { AppState } from '../types';
3+
import { Event } from '../../@types';
4+
5+
export const handleToggleAnnotationModeAction = (prevState: AppState, nextState: AppState): void => {
6+
eventManager.emit(Event.ANNOTATIONS_MODE_CHANGE, { mode: nextState.common.mode });
7+
};

0 commit comments

Comments
 (0)