Unverified Commit 641e7953 authored by Hannah Stepanek's avatar Hannah Stepanek Committed by GitHub

Merge pull request #1112 from hypothesis/add-direct-linked-group-to-store

Add direct linked group to store
parents d86d2ba1 3ab1445d
......@@ -49,8 +49,6 @@ function SidebarContentController(
streamFilter
) {
const self = this;
this.directLinkedGroupFetchFailed =
!!settings.group && settings.group !== store.focusedGroup().id;
function thread() {
return rootThread.thread(store.getState());
......@@ -311,7 +309,7 @@ function SidebarContentController(
this.scrollTo = scrollToAnnotation;
this.areAllAnnotationsVisible = function() {
if (this.directLinkedGroupFetchFailed) {
if (store.getState().directLinkedGroupFetchFailed) {
return true;
}
const selection = store.getState().selectedAnnotationMap;
......@@ -322,7 +320,7 @@ function SidebarContentController(
};
this.selectedGroupUnavailable = function() {
return !this.isLoading() && this.directLinkedGroupFetchFailed;
return !this.isLoading() && store.getState().directLinkedGroupFetchFailed;
};
this.selectedAnnotationUnavailable = function() {
......@@ -387,8 +385,7 @@ function SidebarContentController(
store.clearSelectedAnnotations();
store.selectTab(selectedTab);
// Clear direct-linked group fetch failed state.
this.directLinkedGroupFetchFailed = false;
store.clearDirectLinkedGroupFetchFailed();
};
}
......
......@@ -213,11 +213,11 @@ describe('sidebar.components.sidebar-content', function() {
});
it('clears the directLinkedGroupFetchFailed state', () => {
ctrl.directLinkedGroupFetchFailed = true;
store.setDirectLinkedGroupFetchFailed();
ctrl.clearSelection();
assert.isFalse(ctrl.directLinkedGroupFetchFailed);
assert.isFalse(store.getState().directLinkedGroupFetchFailed);
});
});
......@@ -234,12 +234,8 @@ describe('sidebar.components.sidebar-content', function() {
it('returns false if selected group is unavailable', () => {
fakeSettings.group = 'group-id';
store.loadGroups([{ id: 'default-id' }]);
store.focusGroup('default-id');
fakeGroups.focused.returns({ id: 'default-id' });
store.setDirectLinkedGroupFetchFailed();
$scope.$digest();
// Re-construct the controller after the environment setup.
makeSidebarContentController();
assert.isFalse(ctrl.showSelectedTabs());
});
......@@ -343,12 +339,8 @@ describe('sidebar.components.sidebar-content', function() {
beforeEach(() => {
setFrames([{ uri: 'http://www.example.com' }]);
fakeSettings.group = 'group-id';
store.loadGroups([{ id: 'default-id' }]);
store.focusGroup('default-id');
fakeGroups.focused.returns({ id: 'default-id' });
store.setDirectLinkedGroupFetchFailed();
$scope.$digest();
// Re-construct the controller after the environment setup.
makeSidebarContentController();
});
[null, 'acct:person@example.com'].forEach(userid => {
......@@ -364,10 +356,6 @@ describe('sidebar.components.sidebar-content', function() {
});
});
it('sets directLinkedGroupFetchFailed to true', () => {
assert.isTrue(ctrl.directLinkedGroupFetchFailed);
});
it('areAllAnnotationsVisible returns true since there is an error message', () => {
assert.isTrue(ctrl.areAllAnnotationsVisible());
});
......@@ -387,10 +375,6 @@ describe('sidebar.components.sidebar-content', function() {
$scope.$digest();
});
it('sets directLinkedGroupFetchFailed to false', () => {
assert.isFalse(ctrl.directLinkedGroupFetchFailed);
});
it('areAllAnnotationsVisible returns false since group has no annotations', () => {
assert.isFalse(ctrl.areAllAnnotationsVisible());
});
......
......@@ -77,7 +77,8 @@ function groups(
directLinkedGroup.scopes.enforced
) {
groups = groups.filter(g => g.id !== directLinkedGroupId);
directLinkedGroupId = undefined;
store.setDirectLinkedGroupFetchFailed();
directLinkedGroupId = null;
}
}
......@@ -213,6 +214,14 @@ function groups(
selectedGroupApi = fetchGroup({
id: directLinkedGroupId,
expand: params.expand,
}).then(group => {
// If the group does not exist or the user doesn't have permission.
if (group === null) {
store.setDirectLinkedGroupFetchFailed();
} else {
store.clearDirectLinkedGroupFetchFailed();
}
return group;
});
}
groupApiRequests = groupApiRequests.concat(selectedGroupApi);
......
......@@ -85,6 +85,8 @@ describe('groups', function() {
const group = this.getState().focusedGroup;
return group ? group.id : null;
},
setDirectLinkedGroupFetchFailed: sinon.stub(),
clearDirectLinkedGroupFetchFailed: sinon.stub(),
}
);
fakeSession = sessionWithThreeGroups();
......@@ -191,6 +193,8 @@ describe('groups', function() {
fakeSettings.group = outOfScopeEnforcedGroup.id;
fakeApi.group.read.returns(Promise.resolve(outOfScopeEnforcedGroup));
return svc.load().then(groups => {
// The failure state is captured in the store.
assert.called(fakeStore.setDirectLinkedGroupFetchFailed);
// The focus group is not set to the direct-linked group.
assert.calledWith(fakeStore.focusGroup, dummyGroups[0].id);
// The direct-linked group is not in the list of groups.
......@@ -211,6 +215,8 @@ describe('groups', function() {
)
);
return svc.load().then(() => {
// The failure state is captured in the store.
assert.called(fakeStore.setDirectLinkedGroupFetchFailed);
// The focus group is not set to the direct-linked group.
assert.calledWith(fakeStore.focusGroup, dummyGroups[0].id);
});
......@@ -381,6 +387,16 @@ describe('groups', function() {
});
});
it('clears the directLinkedGroupFetchFailed state if loading a direct-linked group', () => {
const svc = service();
fakeSettings.group = dummyGroups[1].id;
fakeApi.groups.list.returns(Promise.resolve(dummyGroups));
return svc.load().then(() => {
assert.called(fakeStore.clearDirectLinkedGroupFetchFailed);
assert.notCalled(fakeStore.setDirectLinkedGroupFetchFailed);
});
});
[null, 'some-group-id'].forEach(groupId => {
it('does not set the focused group if not present in the groups list', () => {
const svc = service();
......
......@@ -36,6 +36,7 @@ const debugMiddleware = require('./debug-middleware');
const activity = require('./modules/activity');
const annotations = require('./modules/annotations');
const directLinkedGroup = require('./modules/direct-linked-group');
const frames = require('./modules/frames');
const links = require('./modules/links');
const groups = require('./modules/groups');
......@@ -86,6 +87,7 @@ function store($rootScope, settings) {
const modules = [
activity,
annotations,
directLinkedGroup,
frames,
links,
groups,
......
'use strict';
const util = require('../util');
function init() {
return {
/**
* Indicates that an error occured in retrieving/showing the direct linked group.
* This could be because:
* - the group does not exist
* - the user does not have permission
* - the group is out of scope for the given page
* @type {boolean}
*/
directLinkedGroupFetchFailed: false,
};
}
const update = {
UPDATE_DIRECT_LINKED_GROUP_FETCH_FAILED(state, action) {
return {
directLinkedGroupFetchFailed: action.directLinkedGroupFetchFailed,
};
},
};
const actions = util.actionTypes(update);
/**
* Set the direct linked group fetch failure to true.
*/
function setDirectLinkedGroupFetchFailed() {
return {
type: actions.UPDATE_DIRECT_LINKED_GROUP_FETCH_FAILED,
directLinkedGroupFetchFailed: true,
};
}
/**
* Clear the direct linked group fetch failure.
*/
function clearDirectLinkedGroupFetchFailed() {
return {
type: actions.UPDATE_DIRECT_LINKED_GROUP_FETCH_FAILED,
directLinkedGroupFetchFailed: false,
};
}
module.exports = {
init,
update,
actions: {
setDirectLinkedGroupFetchFailed,
clearDirectLinkedGroupFetchFailed,
},
selectors: {},
};
'use strict';
const createStore = require('../../create-store');
const directLinkedGroup = require('../direct-linked-group');
describe('sidebar/store/modules/direct-linked-group', () => {
let store;
beforeEach(() => {
store = createStore([directLinkedGroup]);
});
it('sets directLinkedGroupFetchFailed to true', () => {
store.setDirectLinkedGroupFetchFailed();
assert.isTrue(store.getState().directLinkedGroupFetchFailed);
});
it('sets directLinkedGroupFetchFailed to false', () => {
store.setDirectLinkedGroupFetchFailed();
store.clearDirectLinkedGroupFetchFailed();
assert.isFalse(store.getState().directLinkedGroupFetchFailed);
});
});
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