Commit f05b1b05 authored by Robert Knight's avatar Robert Knight

Use async/await to simplify the first part of the load() method

parent 06209ef5
...@@ -163,10 +163,12 @@ function groups( ...@@ -163,10 +163,12 @@ function groups(
* *
* @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.
let uri = null;
if (isSidebar) { if (isSidebar) {
uri = getDocumentUriForGroupSearch(); uri = await getDocumentUriForGroupSearch();
} }
const directLinkedGroupId = store.getState().directLinkedGroupId; const directLinkedGroupId = store.getState().directLinkedGroupId;
const directLinkedAnnId = store.getState().directLinkedAnnotationId; const directLinkedAnnId = store.getState().directLinkedAnnotationId;
...@@ -176,67 +178,62 @@ function groups( ...@@ -176,67 +178,62 @@ function groups(
let directLinkedAnnotationGroupId = null; let directLinkedAnnotationGroupId = null;
// Step 1: Get the URI of the active document, so we can fetch groups // Step 2: Concurrently fetch the groups the user is a member of,
// associated with that document. // the groups associated with the current document and the annotation
return uri // or group that was direct-linked (if any).
.then(uri => { if (authority) {
// Step 2: Concurrently fetch the groups the user is a member of, params.authority = authority;
// the groups associated with the current document and the annotation }
// or group that was direct-linked (if any). if (uri) {
if (authority) { params.document_uri = uri;
params.authority = authority; }
} documentUri = uri;
if (uri) {
params.document_uri = uri;
}
documentUri = uri;
const profileGroupsApi = api.profile.groups.read({ const profileGroupsApi = api.profile.groups.read({
expand: params.expand, 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 needs to be a second API request to fetch its group since
// the group may not be in the results returned by group.list,
// profile.groups, or the direct-linked group.
let directLinkedAnnApi = Promise.resolve(null);
if (directLinkedAnnId) {
directLinkedAnnApi = api.annotation
.get({ id: directLinkedAnnId })
.catch(() => {
// If the annotation does not exist or the user doesn't have permission.
return null;
}); });
const listGroupsApi = api.groups.list(params); }
let groupApiRequests = [ groupApiRequests = groupApiRequests.concat(directLinkedAnnApi);
profileGroupsApi,
listGroupsApi, // If there is a direct-linked group, add an API request to get that
auth.tokenGetter(), // particular group since it may not be in the results returned by
]; // group.list or profile.groups.
let directLinkedGroupApi = Promise.resolve(null);
// If there is a direct-linked annotation, fetch the annotation to see if (directLinkedGroupId) {
// if there needs to be a second API request to fetch its group since directLinkedGroupApi = fetchGroup({
// the group may not be in the results returned by group.list, id: directLinkedGroupId,
// profile.groups, or the direct-linked group. expand: params.expand,
let directLinkedAnnApi = Promise.resolve(null); }).then(group => {
if (directLinkedAnnId) { // If the group does not exist or the user doesn't have permission.
directLinkedAnnApi = api.annotation if (group === null) {
.get({ id: directLinkedAnnId }) store.setDirectLinkedGroupFetchFailed();
.catch(() => { } else {
// If the annotation does not exist or the user doesn't have permission. store.clearDirectLinkedGroupFetchFailed();
return null;
});
}
groupApiRequests = groupApiRequests.concat(directLinkedAnnApi);
// 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
// group.list or profile.groups.
let directLinkedGroupApi = Promise.resolve(null);
if (directLinkedGroupId) {
directLinkedGroupApi = 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(directLinkedGroupApi); return group;
return Promise.all(groupApiRequests); });
}) }
groupApiRequests = groupApiRequests.concat(directLinkedGroupApi);
return Promise.all(groupApiRequests)
.then( .then(
([ ([
myGroups, myGroups,
......
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