Skip to content

Commit

Permalink
feat(eventing): Emit new annotations_staged_change event
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan committed Sep 1, 2020
1 parent d0e7cfc commit c8e00f4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/@types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum Event {
ANNOTATION_REMOVE = 'annotations_remove',
ANNOTATIONS_INITIALIZED = 'annotations_initialized',
ANNOTATIONS_MODE_CHANGE = 'annotations_mode_change',
ANNOTATIONS_STAGED_CHANGE = 'annotations_staged_change',
VISIBLE_SET = 'annotations_visible_set',
}

Expand Down
4 changes: 4 additions & 0 deletions src/store/eventing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import { handleActiveAnnotationEvents } from './active';
import { handleAnnotationsInitialized } from './init';
import { handleCreateErrorEvents, handleCreatePendingEvents, handleCreateSuccessEvents } from './create';
import { handleFetchErrorEvents } from './fetch';
import { handleResetCreatorAction, handleSetStagedAction } from './staged';
import { handleToggleAnnotationModeAction } from './mode';
import { toggleAnnotationModeAction } from '../common/actions';
import { setStagedAction, resetCreatorAction } from '../creator';

// Array of event handlers based on redux action. To add handling for new events add an entry keyed by action
const eventHandlers: EventHandlerMap = {
[createAnnotationAction.fulfilled.toString()]: handleCreateSuccessEvents,
[createAnnotationAction.pending.toString()]: handleCreatePendingEvents,
[createAnnotationAction.rejected.toString()]: handleCreateErrorEvents,
[fetchAnnotationsAction.rejected.toString()]: handleFetchErrorEvents,
[resetCreatorAction.toString()]: handleResetCreatorAction,
[setActiveAnnotationIdAction.toString()]: handleActiveAnnotationEvents,
[setIsInitialized.toString()]: handleAnnotationsInitialized,
[setStagedAction.toString()]: handleSetStagedAction,
[toggleAnnotationModeAction.toString()]: handleToggleAnnotationModeAction,
};

Expand Down
68 changes: 68 additions & 0 deletions src/store/eventing/staged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import eventManager from '../../common/EventManager';
import { AppState } from '../types';
import { getCreatorStaged, CreatorItem, isCreatorStagedRegion, isCreatorStagedHighlight } from '../creator';
import { Event } from '../../@types';

type Status = 'create' | 'update' | 'cancel';

type Type = 'highlight' | 'region';

type StagedChangeEvent = {
status: Status;
type: Type;
};

const getStatus = (prevStaged: CreatorItem, nextStaged: CreatorItem): Status | null => {
let status: Status | null = null;

if (prevStaged === null && nextStaged !== null) {
status = 'create';
}

if (prevStaged !== null && nextStaged !== null) {
status = 'update';
}

return status;
};

const getType = (staged: CreatorItem): Type | null => {
let type: Type | null = null;

if (isCreatorStagedRegion(staged)) {
type = 'region';
}

if (isCreatorStagedHighlight(staged)) {
type = 'highlight';
}

return type;
};

export const handleSetStagedAction = (prevState: AppState, nextState: AppState): void => {
const prevStaged = getCreatorStaged(prevState);
const nextStaged = getCreatorStaged(nextState);
const status = getStatus(prevStaged, nextStaged);
const type = getType(prevStaged) || getType(nextStaged);

if (!status || !type) {
return;
}

const payload: StagedChangeEvent = { type, status };
eventManager.emit(Event.ANNOTATIONS_STAGED_CHANGE, payload);
};

export const handleResetCreatorAction = (prevState: AppState): void => {
const prevStaged = getCreatorStaged(prevState);
const type = getType(prevStaged);

if (!type) {
return;
}

const payload: StagedChangeEvent = { type, status: 'cancel' };

eventManager.emit(Event.ANNOTATIONS_STAGED_CHANGE, payload);
};

0 comments on commit c8e00f4

Please sign in to comment.