Commit 7812d163 authored by Robert Knight's avatar Robert Knight

Simplify the code for updating the focused group when session state changes

parent 0d0e47ea
...@@ -48,9 +48,10 @@ function groups(localStorage, session, $rootScope, features, $http) { ...@@ -48,9 +48,10 @@ function groups(localStorage, session, $rootScope, features, $http) {
url: baseURI + 'groups/' + id + '/leave', url: baseURI + 'groups/' + id + '/leave',
}); });
// TODO - Optimistically call remove() to // the groups list will be updated in response to a session state
// remove the group locally when // change notification from the server. We could improve the UX here
// https://github.com/hypothesis/h/pull/2587 has been merged // by optimistically updating the session state
return response; return response;
}; };
...@@ -65,12 +66,7 @@ function groups(localStorage, session, $rootScope, features, $http) { ...@@ -65,12 +66,7 @@ function groups(localStorage, session, $rootScope, features, $http) {
} else if (features.flagEnabled('groups')) { } else if (features.flagEnabled('groups')) {
var fromStorage = get(localStorage.getItem(STORAGE_KEY)); var fromStorage = get(localStorage.getItem(STORAGE_KEY));
if (fromStorage) { if (fromStorage) {
var matches = all().filter(function (group) { focusedGroup = fromStorage;
return group.id === fromStorage.id;
});
if (matches.length > 0) {
focusedGroup = matches[0];
}
return focusedGroup; return focusedGroup;
} }
} }
...@@ -80,7 +76,7 @@ function groups(localStorage, session, $rootScope, features, $http) { ...@@ -80,7 +76,7 @@ function groups(localStorage, session, $rootScope, features, $http) {
/** Set the group with the passed id as the currently focused group. */ /** Set the group with the passed id as the currently focused group. */
function focus(id) { function focus(id) {
var g = get(id); var g = get(id);
if (typeof g !== 'undefined') { if (g) {
focusedGroup = g; focusedGroup = g;
localStorage.setItem(STORAGE_KEY, g.id); localStorage.setItem(STORAGE_KEY, g.id);
$rootScope.$broadcast(events.GROUP_FOCUSED, g.id); $rootScope.$broadcast(events.GROUP_FOCUSED, g.id);
...@@ -90,11 +86,8 @@ function groups(localStorage, session, $rootScope, features, $http) { ...@@ -90,11 +86,8 @@ function groups(localStorage, session, $rootScope, features, $http) {
// reset the focused group if the user leaves it // reset the focused group if the user leaves it
$rootScope.$on(events.SESSION_CHANGED, function () { $rootScope.$on(events.SESSION_CHANGED, function () {
if (focusedGroup) { if (focusedGroup) {
var match = session.state.groups.filter(function (group) { focusedGroup = get(focusedGroup.id);
return group.id === focusedGroup.id; if (!focusedGroup) {
});
if (match.length === 0) {
focusedGroup = null;
$rootScope.$broadcast(events.GROUP_FOCUSED, focused()); $rootScope.$broadcast(events.GROUP_FOCUSED, focused());
} }
} }
......
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