Commit de6fa295 authored by Robert Knight's avatar Robert Knight

Match annotations by tags and ID in excludeAnnotations()

After creating an annotation, we remove the pre-created annotation from
the Redux store and replace it with the saved version from the server.

In order to determine which pre-created annotation to remove when adding
the version from the server, we need to match on local tag, since the
version from the server has no ID - but it does have a local tag since
that is copied across from the pre-to-post created version.
parent 214e40fc
......@@ -77,15 +77,25 @@ var types = {
SET_SORT_KEY: 'SET_SORT_KEY',
};
/**
* Return a copy of `current` with all matching annotations in `annotations`
* removed.
*/
function excludeAnnotations(current, annotations) {
var idsAndTags = annotations.reduce(function (map, annot) {
var id = annot.id || annot.$$tag;
map[id] = true;
return map;
}, {});
var ids = {};
var tags = {};
annotations.forEach(function (annot) {
if (annot.id) {
ids[annot.id] = true;
}
if (annot.$$tag) {
tags[annot.$$tag] = true;
}
});
return current.filter(function (annot) {
var id = annot.id || annot.$$tag;
return !idsAndTags[id];
var shouldRemove = (annot.id && (annot.id in ids)) ||
(annot.$$tag && (annot.$$tag in tags));
return !shouldRemove;
});
}
......
'use strict';
var annotationUIFactory = require('../annotation-ui');
var immutable = require('seamless-immutable');
var annotationUIFactory = require('../annotation-ui');
var annotationFixtures = require('./annotation-fixtures');
var unroll = require('./util').unroll;
var defaultAnnotation = annotationFixtures.defaultAnnotation;
var fixtures = immutable({
pair: [
Object.assign(defaultAnnotation(), {id: 1, $$tag: 't1'}),
Object.assign(defaultAnnotation(), {id: 2, $$tag: 't2'}),
],
});
describe('annotationUI', function () {
var annotationUI;
......@@ -36,7 +45,7 @@ describe('annotationUI', function () {
describe('#addAnnotations()', function () {
it('adds annotations to the current state', function () {
var annot = annotationFixtures.defaultAnnotation();
var annot = defaultAnnotation();
annotationUI.addAnnotations([annot]);
assert.deepEqual(annotationUI.getState().annotations, [annot]);
});
......@@ -44,16 +53,28 @@ describe('annotationUI', function () {
describe('#removeAnnotations()', function () {
it('removes annotations from the current state', function () {
var annot = annotationFixtures.defaultAnnotation();
var annot = defaultAnnotation();
annotationUI.addAnnotations([annot]);
annotationUI.removeAnnotations([annot]);
assert.deepEqual(annotationUI.getState().annotations, []);
});
it('matches annotations to remove by ID', function () {
annotationUI.addAnnotations(fixtures.pair);
annotationUI.removeAnnotations([{id: fixtures.pair[0].id}]);
assert.deepEqual(annotationUI.getState().annotations, [fixtures.pair[1]]);
});
it('matches annotations to remove by tag', function () {
annotationUI.addAnnotations(fixtures.pair);
annotationUI.removeAnnotations([{$$tag: fixtures.pair[0].$$tag}]);
assert.deepEqual(annotationUI.getState().annotations, [fixtures.pair[1]]);
});
});
describe('#clearAnnotations()', function () {
it('removes all annotations', function () {
var annot = annotationFixtures.defaultAnnotation();
var annot = defaultAnnotation();
annotationUI.addAnnotations([annot]);
annotationUI.clearAnnotations();
assert.deepEqual(annotationUI.getState().annotations, []);
......
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