Commit 67928814 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Make page note tab auto-select even if page notes have replies

Before, the page notes tab would not be selected if any of the page
notes had replies. Now will correctly auto-select page notes tab if
all top-level (i.e. non-reply) annotations are page notes.

Also add some rudimentary tests for the `ADD_ANNOTATIONS` reducer in
the `selection` store module as none existed.

Fixes #1827
parent 8aec28d6
......@@ -19,8 +19,7 @@ import immutable from 'seamless-immutable';
import uiConstants from '../../ui-constants';
import * as metadata from '../../util/annotation-metadata';
import * as arrayUtil from '../../util/array';
import { toSet } from '../../util/array';
import { countIf, toSet } from '../../util/array';
import * as util from '../util';
/**
......@@ -204,14 +203,18 @@ const update = {
return setTab(state.selectedTab, action.tab);
},
/**
* Automatically select the Page Notes tab, for convenience, if all of the
* top-level annotations in `action.annotations` are Page Notes and the
* previous annotation count was 0 (i.e. collection empty).
*/
ADD_ANNOTATIONS(state, action) {
const noteCount = arrayUtil.countIf(
action.annotations,
metadata.isPageNote
const topLevelAnnotations = action.annotations.filter(
annotation => !metadata.isReply(annotation)
);
// If there are no annotations at all, ADD_ANNOTATIONS will not be called.
const haveOnlyPageNotes = noteCount === action.annotations.length;
// If this is the init phase and there are only page notes, select the page notes tab.
const noteCount = countIf(action.annotations, metadata.isPageNote);
const haveOnlyPageNotes = noteCount === topLevelAnnotations.length;
if (action.currentAnnotationCount === 0 && haveOnlyPageNotes) {
return { selectedTab: uiConstants.TAB_NOTES };
}
......@@ -228,7 +231,7 @@ const update = {
let selectedTab = state.selectedTab;
if (
selectedTab === uiConstants.TAB_ORPHANS &&
arrayUtil.countIf(action.remainingAnnotations, metadata.isOrphan) === 0
countIf(action.remainingAnnotations, metadata.isOrphan) === 0
) {
selectedTab = uiConstants.TAB_ANNOTATIONS;
}
......
......@@ -2,6 +2,7 @@ import uiConstants from '../../../ui-constants';
import createStore from '../../create-store';
import annotations from '../annotations';
import selection from '../selection';
import * as fixtures from '../../../test/annotation-fixtures';
describe('sidebar/store/modules/selection', () => {
let store;
......@@ -423,4 +424,59 @@ describe('sidebar/store/modules/selection', () => {
assert.equal(getSelectionState().sortKey, 'Newest');
});
});
describe('ADD_ANNOTATIONS', () => {
it('should select the page notes tab if all top-level annotations are page notes', () => {
store.dispatch({
type: 'ADD_ANNOTATIONS',
annotations: [fixtures.oldPageNote(), fixtures.oldPageNote()],
currentAnnotationCount: 0,
});
assert.equal(getSelectionState().selectedTab, uiConstants.TAB_NOTES);
});
it('should select the page notes tab if page notes have replies', () => {
const pageNote = fixtures.oldPageNote();
const reply = fixtures.newReply();
reply.references = [pageNote.id];
store.dispatch({
type: 'ADD_ANNOTATIONS',
annotations: [pageNote, reply],
currentAnnotationCount: 0,
});
assert.equal(getSelectionState().selectedTab, uiConstants.TAB_NOTES);
});
it('should not select the page notes tab if there were previously annotations in the store', () => {
store.dispatch({
type: 'ADD_ANNOTATIONS',
annotations: [fixtures.oldPageNote(), fixtures.oldPageNote()],
currentAnnotationCount: 4,
});
assert.equal(
getSelectionState().selectedTab,
uiConstants.TAB_ANNOTATIONS
);
});
it('should not select the page notes tab if there are non-page-note annotations at the top level', () => {
store.dispatch({
type: 'ADD_ANNOTATIONS',
annotations: [
fixtures.oldPageNote(),
fixtures.oldPageNote(),
fixtures.oldHighlight(),
],
currentAnnotationCount: 0,
});
assert.equal(
getSelectionState().selectedTab,
uiConstants.TAB_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