Unverified Commit f71d76cc authored by Lyza Gardner's avatar Lyza Gardner Committed by GitHub

Merge pull request #2020 from hypothesis/clear-selection-when-changing-user

Clear selection when changing user
parents a929c22e c9002b9c
...@@ -47,7 +47,7 @@ function SearchStatusBar({ rootThread }) { ...@@ -47,7 +47,7 @@ function SearchStatusBar({ rootThread }) {
filterQuery: store.getState().selection.filterQuery, filterQuery: store.getState().selection.filterQuery,
focusModeFocused: store.focusModeFocused(), focusModeFocused: store.focusModeFocused(),
focusModeUserPrettyName: store.focusModeUserPrettyName(), focusModeUserPrettyName: store.focusModeUserPrettyName(),
selectionMap: store.getState().selection.selectedAnnotationMap, selectionMap: store.getSelectedAnnotationMap(),
selectedTab: store.getState().selection.selectedTab, selectedTab: store.getState().selection.selectedTab,
})); }));
......
...@@ -39,10 +39,9 @@ function SidebarContentController( ...@@ -39,10 +39,9 @@ function SidebarContentController(
* not the order in which they appear in the document. * not the order in which they appear in the document.
*/ */
function firstSelectedAnnotation() { function firstSelectedAnnotation() {
if (store.getState().selection.selectedAnnotationMap) { const selectedAnnotationMap = store.getSelectedAnnotationMap();
const id = Object.keys( if (selectedAnnotationMap) {
store.getState().selection.selectedAnnotationMap const id = Object.keys(selectedAnnotationMap)[0];
)[0];
return store.getState().annotations.annotations.find(function (annot) { return store.getState().annotations.annotations.find(function (annot) {
return annot.id === id; return annot.id === id;
}); });
......
...@@ -26,6 +26,7 @@ describe('SearchStatusBar', () => { ...@@ -26,6 +26,7 @@ describe('SearchStatusBar', () => {
annotationCount: sinon.stub().returns(1), annotationCount: sinon.stub().returns(1),
focusModeFocused: sinon.stub().returns(false), focusModeFocused: sinon.stub().returns(false),
focusModeUserPrettyName: sinon.stub().returns('Fake User'), focusModeUserPrettyName: sinon.stub().returns('Fake User'),
getSelectedAnnotationMap: sinon.stub(),
noteCount: sinon.stub().returns(0), noteCount: sinon.stub().returns(0),
}; };
...@@ -237,12 +238,12 @@ describe('SearchStatusBar', () => { ...@@ -237,12 +238,12 @@ describe('SearchStatusBar', () => {
fakeStore.getState.returns({ fakeStore.getState.returns({
selection: { selection: {
filterQuery: null, filterQuery: null,
selectedAnnotationMap: { annId: true },
selectedTab: test.tab, selectedTab: test.tab,
}, },
}); });
fakeStore.annotationCount.returns(test.annotationCount); fakeStore.annotationCount.returns(test.annotationCount);
fakeStore.noteCount.returns(test.noteCount); fakeStore.noteCount.returns(test.noteCount);
fakeStore.getSelectedAnnotationMap.returns({ annId: true });
const wrapper = createComponent({}); const wrapper = createComponent({});
...@@ -266,10 +267,10 @@ describe('SearchStatusBar', () => { ...@@ -266,10 +267,10 @@ describe('SearchStatusBar', () => {
selection: { selection: {
filterQuery: null, filterQuery: null,
selectedTab: test.tab, selectedTab: test.tab,
selectedAnnotationMap: { annId: true },
}, },
}); });
fakeStore.annotationCount.returns(5); fakeStore.annotationCount.returns(5);
fakeStore.getSelectedAnnotationMap.returns({ annId: true });
fakeStore.noteCount.returns(3); fakeStore.noteCount.returns(3);
const wrapper = createComponent({}); const wrapper = createComponent({});
......
...@@ -226,7 +226,7 @@ describe('sidebar.components.sidebar-content', function () { ...@@ -226,7 +226,7 @@ describe('sidebar.components.sidebar-content', function () {
describe('when an annotation is anchored', function () { describe('when an annotation is anchored', function () {
it('focuses and scrolls to the annotation if already selected', function () { it('focuses and scrolls to the annotation if already selected', function () {
const uri = 'http://example.com'; const uri = 'http://example.com';
store.selectAnnotations(['123']); store.getSelectedAnnotationMap = sinon.stub().returns({ '123': true });
setFrames([{ uri: uri }]); setFrames([{ uri: uri }]);
const annot = { const annot = {
$tag: 'atag', $tag: 'atag',
......
...@@ -89,9 +89,9 @@ export default function RootThread( ...@@ -89,9 +89,9 @@ export default function RootThread(
// determines what is visible and build the visible thread structure // determines what is visible and build the visible thread structure
return buildThread(state.annotations.annotations, { return buildThread(state.annotations.annotations, {
forceVisible: truthyKeys(state.selection.forceVisible), forceVisible: truthyKeys(state.selection.forceVisible),
expanded: state.selection.expanded, expanded: store.expandedThreads(),
highlighted: state.selection.highlighted, highlighted: state.selection.highlighted,
selected: truthyKeys(state.selection.selectedAnnotationMap || {}), selected: truthyKeys(store.getSelectedAnnotationMap() || {}),
sortCompareFn: sortFn, sortCompareFn: sortFn,
filterFn: filterFn, filterFn: filterFn,
threadFilterFn: threadFilterFn, threadFilterFn: threadFilterFn,
......
...@@ -46,12 +46,9 @@ describe('rootThread', function () { ...@@ -46,12 +46,9 @@ describe('rootThread', function () {
}, },
drafts: [], drafts: [],
selection: { selection: {
expanded: {},
filterQuery: null, filterQuery: null,
focusedAnnotationMap: null,
forceVisible: {}, forceVisible: {},
highlighted: [], highlighted: [],
selectedAnnotationMap: null,
sortKey: 'Location', sortKey: 'Location',
sortKeysAvailable: ['Location'], sortKeysAvailable: ['Location'],
}, },
...@@ -64,6 +61,8 @@ describe('rootThread', function () { ...@@ -64,6 +61,8 @@ describe('rootThread', function () {
return this.state; return this.state;
}, },
expandedThreads: sinon.stub().returns({}),
getSelectedAnnotationMap: sinon.stub().returns(null),
subscribe: sinon.stub(), subscribe: sinon.stub(),
removeAnnotations: sinon.stub(), removeAnnotations: sinon.stub(),
removeSelectedAnnotation: sinon.stub(), removeSelectedAnnotation: sinon.stub(),
...@@ -129,10 +128,10 @@ describe('rootThread', function () { ...@@ -129,10 +128,10 @@ describe('rootThread', function () {
}); });
it('passes the current selection to buildThread()', function () { it('passes the current selection to buildThread()', function () {
fakeStore.state.selection.selectedAnnotationMap = { fakeStore.getSelectedAnnotationMap.returns({
id1: true, id1: true,
id2: true, id2: true,
}; });
rootThread.thread(fakeStore.state); rootThread.thread(fakeStore.state);
assert.calledWith( assert.calledWith(
fakeBuildThread, fakeBuildThread,
...@@ -144,7 +143,7 @@ describe('rootThread', function () { ...@@ -144,7 +143,7 @@ describe('rootThread', function () {
}); });
it('passes the current expanded set to buildThread()', function () { it('passes the current expanded set to buildThread()', function () {
fakeStore.state.selection.expanded = { id1: true, id2: true }; fakeStore.expandedThreads.returns({ id1: true, id2: true });
rootThread.thread(fakeStore.state); rootThread.thread(fakeStore.state);
assert.calledWith( assert.calledWith(
fakeBuildThread, fakeBuildThread,
......
...@@ -138,6 +138,7 @@ const update = { ...@@ -138,6 +138,7 @@ const update = {
const tabSettings = setTab(state.selectedTab, selectedTab); const tabSettings = setTab(state.selectedTab, selectedTab);
return { return {
filterQuery: null, filterQuery: null,
forceVisible: {},
selectedAnnotationMap: null, selectedAnnotationMap: null,
...tabSettings, ...tabSettings,
}; };
...@@ -374,14 +375,15 @@ function setFocusModeFocused(focused) { ...@@ -374,14 +375,15 @@ function setFocusModeFocused(focused) {
} }
/** /**
* Changes the focused user and sets focused enabled to `true`. * Clears any applied filters, changes the focused user and sets
* focused enabled to `true`.
* *
* @param {User} user - The user to focus on * @param {User} user - The user to focus on
*/ */
function changeFocusModeUser(user) { function changeFocusModeUser(user) {
return { return function (dispatch) {
type: actions.CHANGE_FOCUS_MODE_USER, dispatch({ type: actions.CLEAR_SELECTION });
user, dispatch({ type: actions.CHANGE_FOCUS_MODE_USER, user });
}; };
} }
...@@ -428,6 +430,10 @@ const getFirstSelectedAnnotationId = createSelector( ...@@ -428,6 +430,10 @@ const getFirstSelectedAnnotationId = createSelector(
selected => (selected ? Object.keys(selected)[0] : null) selected => (selected ? Object.keys(selected)[0] : null)
); );
function expandedThreads(state) {
return state.selection.expanded;
}
function filterQuery(state) { function filterQuery(state) {
return state.selection.filterQuery; return state.selection.filterQuery;
} }
...@@ -502,6 +508,10 @@ function focusModeUserPrettyName(state) { ...@@ -502,6 +508,10 @@ function focusModeUserPrettyName(state) {
} }
} }
function getSelectedAnnotationMap(state) {
return state.selection.selectedAnnotationMap;
}
export default { export default {
init: init, init: init,
namespace: 'selection', namespace: 'selection',
...@@ -525,6 +535,7 @@ export default { ...@@ -525,6 +535,7 @@ export default {
selectors: { selectors: {
hasSelectedAnnotations, hasSelectedAnnotations,
expandedThreads,
filterQuery, filterQuery,
focusModeFocused, focusModeFocused,
focusModeEnabled, focusModeEnabled,
...@@ -533,5 +544,6 @@ export default { ...@@ -533,5 +544,6 @@ export default {
focusModeUserPrettyName, focusModeUserPrettyName,
isAnnotationSelected, isAnnotationSelected,
getFirstSelectedAnnotationId, getFirstSelectedAnnotationId,
getSelectedAnnotationMap,
}, },
}; };
...@@ -38,7 +38,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -38,7 +38,7 @@ describe('sidebar/store/modules/selection', () => {
describe('setCollapsed()', function () { describe('setCollapsed()', function () {
it('sets the expanded state of the annotation', function () { it('sets the expanded state of the annotation', function () {
store.setCollapsed('parent_id', false); store.setCollapsed('parent_id', false);
assert.deepEqual(getSelectionState().expanded, { parent_id: true }); assert.deepEqual(store.expandedThreads(), { parent_id: true });
}); });
}); });
...@@ -109,7 +109,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -109,7 +109,7 @@ describe('sidebar/store/modules/selection', () => {
describe('selectAnnotations()', function () { describe('selectAnnotations()', function () {
it('adds the passed annotations to the selectedAnnotationMap', function () { it('adds the passed annotations to the selectedAnnotationMap', function () {
store.selectAnnotations([1, 2, 3]); store.selectAnnotations([1, 2, 3]);
assert.deepEqual(getSelectionState().selectedAnnotationMap, { assert.deepEqual(store.getSelectedAnnotationMap(), {
1: true, 1: true,
2: true, 2: true,
3: true, 3: true,
...@@ -119,7 +119,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -119,7 +119,7 @@ describe('sidebar/store/modules/selection', () => {
it('replaces any annotations originally in the map', function () { it('replaces any annotations originally in the map', function () {
store.selectAnnotations([1]); store.selectAnnotations([1]);
store.selectAnnotations([2, 3]); store.selectAnnotations([2, 3]);
assert.deepEqual(getSelectionState().selectedAnnotationMap, { assert.deepEqual(store.getSelectedAnnotationMap(), {
2: true, 2: true,
3: true, 3: true,
}); });
...@@ -128,7 +128,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -128,7 +128,7 @@ describe('sidebar/store/modules/selection', () => {
it('nulls the map if no annotations are selected', function () { it('nulls the map if no annotations are selected', function () {
store.selectAnnotations([1]); store.selectAnnotations([1]);
store.selectAnnotations([]); store.selectAnnotations([]);
assert.isNull(getSelectionState().selectedAnnotationMap); assert.isNull(store.getSelectedAnnotationMap());
}); });
}); });
...@@ -136,7 +136,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -136,7 +136,7 @@ describe('sidebar/store/modules/selection', () => {
it('adds annotations missing from the selectedAnnotationMap', function () { it('adds annotations missing from the selectedAnnotationMap', function () {
store.selectAnnotations([1, 2]); store.selectAnnotations([1, 2]);
store.toggleSelectedAnnotations([3, 4]); store.toggleSelectedAnnotations([3, 4]);
assert.deepEqual(getSelectionState().selectedAnnotationMap, { assert.deepEqual(store.getSelectedAnnotationMap(), {
1: true, 1: true,
2: true, 2: true,
3: true, 3: true,
...@@ -147,7 +147,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -147,7 +147,7 @@ describe('sidebar/store/modules/selection', () => {
it('removes annotations already in the selectedAnnotationMap', function () { it('removes annotations already in the selectedAnnotationMap', function () {
store.selectAnnotations([1, 3]); store.selectAnnotations([1, 3]);
store.toggleSelectedAnnotations([1, 2]); store.toggleSelectedAnnotations([1, 2]);
assert.deepEqual(getSelectionState().selectedAnnotationMap, { assert.deepEqual(store.getSelectedAnnotationMap(), {
2: true, 2: true,
3: true, 3: true,
}); });
...@@ -156,7 +156,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -156,7 +156,7 @@ describe('sidebar/store/modules/selection', () => {
it('nulls the map if no annotations are selected', function () { it('nulls the map if no annotations are selected', function () {
store.selectAnnotations([1]); store.selectAnnotations([1]);
store.toggleSelectedAnnotations([1]); store.toggleSelectedAnnotations([1]);
assert.isNull(getSelectionState().selectedAnnotationMap); assert.isNull(store.getSelectedAnnotationMap());
}); });
}); });
...@@ -164,7 +164,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -164,7 +164,7 @@ describe('sidebar/store/modules/selection', () => {
it('removing an annotation should also remove it from selectedAnnotationMap', function () { it('removing an annotation should also remove it from selectedAnnotationMap', function () {
store.selectAnnotations([1, 2, 3]); store.selectAnnotations([1, 2, 3]);
store.removeAnnotations([{ id: 2 }]); store.removeAnnotations([{ id: 2 }]);
assert.deepEqual(getSelectionState().selectedAnnotationMap, { assert.deepEqual(store.getSelectedAnnotationMap(), {
1: true, 1: true,
3: true, 3: true,
}); });
...@@ -175,7 +175,7 @@ describe('sidebar/store/modules/selection', () => { ...@@ -175,7 +175,7 @@ describe('sidebar/store/modules/selection', () => {
it('removes all annotations from the selection', function () { it('removes all annotations from the selection', function () {
store.selectAnnotations([1]); store.selectAnnotations([1]);
store.clearSelectedAnnotations(); store.clearSelectedAnnotations();
assert.isNull(getSelectionState().selectedAnnotationMap); assert.isNull(store.getSelectedAnnotationMap());
}); });
it('clears the current search query', function () { it('clears the current search query', function () {
...@@ -229,6 +229,19 @@ describe('sidebar/store/modules/selection', () => { ...@@ -229,6 +229,19 @@ describe('sidebar/store/modules/selection', () => {
assert.equal(store.focusModeFocused(), false); assert.equal(store.focusModeFocused(), false);
assert.equal(store.focusModeEnabled(), false); assert.equal(store.focusModeEnabled(), false);
}); });
it('clears other applied selections', () => {
store.setFocusModeFocused(true);
store.setForceVisible('someAnnotationId');
store.setFilterQuery('somequery');
store.changeFocusModeUser({
username: 'testuser',
displayName: 'Test User',
});
assert.isEmpty(getSelectionState().forceVisible);
assert.isNull(store.filterQuery());
});
}); });
describe('setFocusModeFocused()', function () { describe('setFocusModeFocused()', function () {
......
...@@ -37,19 +37,19 @@ describe('store', function () { ...@@ -37,19 +37,19 @@ describe('store', function () {
describe('initialization', function () { describe('initialization', function () {
it('does not set a selection when settings.annotations is null', function () { it('does not set a selection when settings.annotations is null', function () {
assert.isFalse(store.hasSelectedAnnotations()); assert.isFalse(store.hasSelectedAnnotations());
assert.equal(Object.keys(store.getState().selection.expanded).length, 0); assert.equal(Object.keys(store.expandedThreads()).length, 0);
}); });
it('sets the selection when settings.annotations is set', function () { it('sets the selection when settings.annotations is set', function () {
store = storeFactory(fakeRootScope, { annotations: 'testid' }); store = storeFactory(fakeRootScope, { annotations: 'testid' });
assert.deepEqual(store.getState().selection.selectedAnnotationMap, { assert.deepEqual(store.getSelectedAnnotationMap(), {
testid: true, testid: true,
}); });
}); });
it('expands the selected annotations when settings.annotations is set', function () { it('expands the selected annotations when settings.annotations is set', function () {
store = storeFactory(fakeRootScope, { annotations: 'testid' }); store = storeFactory(fakeRootScope, { annotations: 'testid' });
assert.deepEqual(store.getState().selection.expanded, { assert.deepEqual(store.expandedThreads(), {
testid: true, testid: true,
}); });
}); });
...@@ -60,7 +60,7 @@ describe('store', function () { ...@@ -60,7 +60,7 @@ describe('store', function () {
// CLEAR_SELECTION action in multiple store modules. // CLEAR_SELECTION action in multiple store modules.
it('sets `selectedAnnotationMap` to null', () => { it('sets `selectedAnnotationMap` to null', () => {
store.clearSelection(); store.clearSelection();
assert.isNull(store.getState().selection.selectedAnnotationMap); assert.isNull(store.getSelectedAnnotationMap());
}); });
it('sets `filterQuery` to null', () => { it('sets `filterQuery` to null', () => {
......
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