Unverified Commit b485628f authored by Hannah Stepanek's avatar Hannah Stepanek Committed by GitHub

Merge pull request #1124 from hypothesis/simplify-groups-load-part-2

Simplify groups service `load` method using async/await
parents b22b7a3e ee9e092c
...@@ -138,7 +138,7 @@ function groups( ...@@ -138,7 +138,7 @@ function groups(
// to include groups associated with this page. This is retained to determine // to include groups associated with this page. This is retained to determine
// whether we need to re-fetch groups if the URLs of frames connected to the // whether we need to re-fetch groups if the URLs of frames connected to the
// sidebar app changes. // sidebar app changes.
let documentUri; let documentUri = null;
/* /*
* Fetch an individual group. * Fetch an individual group.
...@@ -158,54 +158,35 @@ function groups( ...@@ -158,54 +158,35 @@ function groups(
* group. * group.
* *
* The groups that are fetched depend on the current user, the URI of * The groups that are fetched depend on the current user, the URI of
* the current document, and whether any direct-links were followed (either * the current document, and the direct-linked group and/or annotation.
* to an annotation or group).
* *
* @return {Promise<Group[]>} * @return {Promise<Group[]>}
*/ */
function load() { async function load() {
let uri = Promise.resolve(null); // Step 1: Get the URI of the active document, so we can fetch groups
// associated with that document.
if (isSidebar) { if (isSidebar) {
uri = getDocumentUriForGroupSearch(); documentUri = await getDocumentUriForGroupSearch();
} }
const directLinkedGroupId = store.getState().directLinkedGroupId;
const directLinkedAnnId = store.getState().directLinkedAnnotationId;
const params = {
expand: ['organization', 'scopes'],
};
let directLinkedAnnotationGroupId = null;
// Step 1: Get the URI of the active document, so we can fetch groups
// associated with that document.
return uri
.then(uri => {
// Step 2: Concurrently fetch the groups the user is a member of, // Step 2: Concurrently fetch the groups the user is a member of,
// the groups associated with the current document and the annotation // the groups associated with the current document and the annotation
// or group that was direct-linked (if any). // and/or group that was direct-linked (if any).
const params = {
expand: ['organization', 'scopes'],
};
if (authority) { if (authority) {
params.authority = authority; params.authority = authority;
} }
if (uri) { if (documentUri) {
params.document_uri = uri; params.document_uri = documentUri;
} }
documentUri = uri;
const profileGroupsApi = api.profile.groups.read({
expand: params.expand,
});
const listGroupsApi = api.groups.list(params);
let groupApiRequests = [
profileGroupsApi,
listGroupsApi,
auth.tokenGetter(),
];
// If there is a direct-linked annotation, fetch the annotation to see // If there is a direct-linked annotation, fetch the annotation in case
// if there needs to be a second API request to fetch its group since // the associated group has not already been fetched and we need to make
// the group may not be in the results returned by group.list, // an additional request for it.
// profile.groups, or the direct-linked group. const directLinkedAnnId = store.getState().directLinkedAnnotationId;
let directLinkedAnnApi = Promise.resolve(null); let directLinkedAnnApi = null;
if (directLinkedAnnId) { if (directLinkedAnnId) {
directLinkedAnnApi = api.annotation directLinkedAnnApi = api.annotation
.get({ id: directLinkedAnnId }) .get({ id: directLinkedAnnId })
...@@ -214,12 +195,12 @@ function groups( ...@@ -214,12 +195,12 @@ function groups(
return null; return null;
}); });
} }
groupApiRequests = groupApiRequests.concat(directLinkedAnnApi);
// If there is a direct-linked group, add an API request to get that // If there is a direct-linked group, add an API request to get that
// particular group since it may not be in the results returned by // particular group since it may not be in the set of groups that are
// group.list or profile.groups. // fetched by other requests.
let directLinkedGroupApi = Promise.resolve(null); const directLinkedGroupId = store.getState().directLinkedGroupId;
let directLinkedGroupApi = null;
if (directLinkedGroupId) { if (directLinkedGroupId) {
directLinkedGroupApi = fetchGroup({ directLinkedGroupApi = fetchGroup({
id: directLinkedGroupId, id: directLinkedGroupId,
...@@ -234,58 +215,58 @@ function groups( ...@@ -234,58 +215,58 @@ function groups(
return group; return group;
}); });
} }
groupApiRequests = groupApiRequests.concat(directLinkedGroupApi);
return Promise.all(groupApiRequests); const [
})
.then(
([
myGroups, myGroups,
featuredGroups, featuredGroups,
token, token,
directLinkedAnn, directLinkedAnn,
directLinkedGroup, directLinkedGroup,
]) => { ] = await Promise.all([
api.profile.groups.read({ expand: params.expand }),
api.groups.list(params),
auth.tokenGetter(),
directLinkedAnnApi,
directLinkedGroupApi,
]);
// Step 3. Add the direct-linked group to the list of featured groups, // Step 3. Add the direct-linked group to the list of featured groups,
// and if there was a direct-linked annotation, fetch its group if we // and if there was a direct-linked annotation, fetch its group if we
// don't already have it. // don't already have it.
// If there is a direct-linked group, add it to the featured groups list. // If there is a direct-linked group, add it to the featured groups list.
let allFeaturedGroups = if (
directLinkedGroup !== null && directLinkedGroup &&
!featuredGroups.some(g => g.id === directLinkedGroup.id) !featuredGroups.some(g => g.id === directLinkedGroup.id)
? featuredGroups.concat([directLinkedGroup]) ) {
: featuredGroups; featuredGroups.push(directLinkedGroup);
}
// If there's a direct-linked annotation it may require an extra API call // If there's a direct-linked annotation it may require an extra API call
// to fetch its group. // to fetch its group.
let directLinkedAnnotationGroupId = null;
if (directLinkedAnn) { if (directLinkedAnn) {
// Set the directLinkedAnnotationGroupId to be used later in // Set the directLinkedAnnotationGroupId to be used later in
// the filterGroups method. // the filterGroups method.
directLinkedAnnotationGroupId = directLinkedAnn.group; directLinkedAnnotationGroupId = directLinkedAnn.group;
const directLinkedAnnGroup = myGroups
.concat(allFeaturedGroups)
.some(g => g.id === directLinkedAnn.group);
// If the direct-linked annotation's group has not already been fetched, // If the direct-linked annotation's group has not already been fetched,
// fetch it. // fetch it.
const directLinkedAnnGroup = myGroups
.concat(featuredGroups)
.find(g => g.id === directLinkedAnn.group);
if (!directLinkedAnnGroup) { if (!directLinkedAnnGroup) {
const initialFeaturedGroups = allFeaturedGroups; const directLinkedAnnGroup = await fetchGroup({
allFeaturedGroups = fetchGroup({
id: directLinkedAnn.group, id: directLinkedAnn.group,
expand: params.expand, expand: params.expand,
}).then(directLinkedAnnGroup => {
if (!directLinkedAnnGroup) {
return initialFeaturedGroups;
}
return initialFeaturedGroups.concat(directLinkedAnnGroup);
}); });
if (directLinkedAnnGroup) {
featuredGroups.push(directLinkedAnnGroup);
} }
} }
return Promise.all([myGroups, allFeaturedGroups, documentUri, token]);
} }
)
.then(([myGroups, featuredGroups, documentUri, token]) => {
// Step 4. Combine all the groups into a single list and set additional // Step 4. Combine all the groups into a single list and set additional
// metadata on them that will be used elsewhere in the app. // metadata on them that will be used elsewhere in the app.
const isLoggedIn = token !== null; const isLoggedIn = token !== null;
...@@ -305,22 +286,17 @@ function groups( ...@@ -305,22 +286,17 @@ function groups(
store.loadGroups(groups); store.loadGroups(groups);
if ( if (isFirstLoad) {
isFirstLoad && if (groups.some(g => g.id === directLinkedAnnotationGroupId)) {
groups.some(g => g.id === directLinkedAnnotationGroupId)
) {
store.focusGroup(directLinkedAnnotationGroupId); store.focusGroup(directLinkedAnnotationGroupId);
} else if ( } else if (groups.some(g => g.id === directLinkedGroupId)) {
isFirstLoad &&
groups.some(g => g.id === directLinkedGroupId)
) {
store.focusGroup(directLinkedGroupId); store.focusGroup(directLinkedGroupId);
} else if (isFirstLoad && groups.some(g => g.id === prevFocusedGroup)) { } else if (groups.some(g => g.id === prevFocusedGroup)) {
store.focusGroup(prevFocusedGroup); store.focusGroup(prevFocusedGroup);
} }
}
return groups; return groups;
});
} }
const sortGroups = memoize(groups => { const sortGroups = memoize(groups => {
......
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