Commit 30929896 authored by Hannah Stepanek's avatar Hannah Stepanek

Add getFirstSelectedAnnotationId to store

This moves the firstKey function from the sidebar-content into
the selection store module.
parent a64824a5
......@@ -7,24 +7,13 @@ const memoize = require('../util/memoize');
const tabs = require('../tabs');
const uiConstants = require('../ui-constants');
function firstKey(object) {
for (const k in object) {
if (!object.hasOwnProperty(k)) {
continue;
}
return k;
}
return null;
}
/**
* Returns the group ID of the first annotation in `results` whose
* ID is a key in `selection`.
* ID is `annId`.
*/
function groupIDFromSelection(selection, results) {
const id = firstKey(selection);
function getGroupID(annId, results) {
const annot = results.find(function(annot) {
return annot.id === id;
return annot.id === annId;
});
if (!annot) {
return null;
......@@ -123,10 +112,7 @@ function SidebarContentController(
if (store.hasSelectedAnnotations()) {
// Focus the group containing the selected annotation and filter
// annotations to those from this group
let groupID = groupIDFromSelection(
store.getState().selectedAnnotationMap,
results
);
let groupID = getGroupID(store.getFirstSelectedAnnotationId(), results);
if (!groupID) {
// If the selected annotation is not available, fall back to
// loading annotations for the currently focused group
......@@ -316,7 +302,7 @@ function SidebarContentController(
};
this.selectedAnnotationUnavailable = function() {
const selectedID = firstKey(store.getState().selectedAnnotationMap);
const selectedID = store.getFirstSelectedAnnotationId();
return (
!this.isLoading() && !!selectedID && !store.annotationExists(selectedID)
);
......@@ -343,7 +329,7 @@ function SidebarContentController(
// The user is logged out and has landed on a direct linked
// annotation. If there is an annotation selection and that
// selection is available to the user, show the CTA.
const selectedID = firstKey(store.getState().selectedAnnotationMap);
const selectedID = store.getFirstSelectedAnnotationId();
return (
!this.isLoading() && !!selectedID && store.annotationExists(selectedID)
);
......
......@@ -10,6 +10,7 @@
'use strict';
const { createSelector } = require('reselect');
const immutable = require('seamless-immutable');
const toSet = require('../../util/array-util').toSet;
......@@ -314,6 +315,16 @@ function clearSelectedAnnotations() {
return { type: actions.CLEAR_SELECTION };
}
/**
* Returns the annotation ID of the first annotation in `selectedAnnotationMap`.
*
* @return {string|null}
*/
const getFirstSelectedAnnotationId = createSelector(
state => state.selectedAnnotationMap,
selected => (selected ? Object.keys(selected)[0] : null)
);
module.exports = {
init: init,
update: update,
......@@ -335,5 +346,6 @@ module.exports = {
selectors: {
hasSelectedAnnotations,
isAnnotationSelected,
getFirstSelectedAnnotationId,
},
};
......@@ -340,6 +340,23 @@ describe('store', function() {
});
});
describe('#getFirstSelectedAnnotationId', function() {
it('returns the first selected annotation id it finds', function() {
store.selectAnnotations([1, 2]);
assert.equal(store.getFirstSelectedAnnotationId(), 1);
});
it('returns null if no selected annotation ids are found', function() {
store.selectAnnotations([]);
assert.isNull(store.getFirstSelectedAnnotationId());
});
it('returns null if no annotation ids are ', function() {
store.selectAnnotations([]);
assert.isNull(store.getFirstSelectedAnnotationId());
});
});
describe('#isAnnotationSelected', function() {
it('returns true if the id provided is selected', function() {
store.selectAnnotations([1]);
......
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