Skip to content

Commit

Permalink
change on un-named perspective: get default perspective, before getti…
Browse files Browse the repository at this point in the history
…ng first perspective
  • Loading branch information
annyhe committed Jun 5, 2017
1 parent df3ec5a commit 6533ce1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 104 deletions.
5 changes: 3 additions & 2 deletions view/newPerspective/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ function getPerspectiveUrl() {
document.title += ' - ' + p;
return { url: '/v1/perspectives/' + p, named: true };
} else {

return { url: GET_DEFAULT_PERSPECTIVE, named: false };
const object = { named: false };
object.url = GET_DEFAULT_PERSPECTIVE;
return object;
}
} // whichPerspective

Expand Down
200 changes: 98 additions & 102 deletions view/newPerspective/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function getFilterQuery(p) {
p.subjectTagFilter.join().replace(/,/g, ',' + sign);
}

if (p.statusFilter.length) {
if (p.statusFilter && p.statusFilter.length) {
if (q) {
q += '&';
}
Expand Down Expand Up @@ -256,58 +256,55 @@ function getValuesObject(request, getPerspectiveUrl, handleHierarchyEvent, handl
let hierarchyLoadEvent; // will be custom event
let gotLens = false;

// get the perspectives first,
// in case url ends with /perspectives
// and we need to get the first perspective by alphabetical order
const perspectiveNames = request('/v1/perspectives?fields=name');

// if perspective is named, get the named perspective
// otherwise get the default perspective
const { url, named } = getPerspectiveUrl();
console.log('before get all', url, named)
const perspectivePromise = request(url)

// get the perspectives and the named/default perspective
const arr = [request('/v1/perspectives?fields=name'),
request(url)
.catch((err) => {

// throw 404 error if named perspective is not found
if (named && err.status === httpStatus.NOT_FOUND) {
throw new Error('perspective named ' + url.split('/').pop() + ' is not found');
// if default perspective is not found,
// do not throw error. After the perspectives resolves
// GET the first perspective
if (!named && err.status === httpStatus.NOT_FOUND) {
return Promise.resolve();
}

// otherwise default perspective is not found. handle once received
// result from perspectiveNames
return Promise.resolve();
});
// either named perspective or another error.
throw err;
})
];

return Promise.all(perspectiveNames, perspectivePromise)
return Promise.all(arr)
.then((responses) => {
console.log('responses', responses)

// const perspectives = responses[0].body;

// // assign perspective-related values to the accumulator object
// valuesObj.persNames = perspectives.map((perspective) => perspective.name).sort();

// // if no errornamed perspective or default exists, .body is an object.
// // otherwise default perspective does NOT exist. GET the first perspective by
// // alphabetical order
// if (!responses[1].error) {
// valuesObj.perspective = responses[1].body;
// valuesObj.name = valuesObj.perspective.name;

// // check to see there are perspectives
// // before GETting the perspective
// } else if (perspectives.length) {
// valuesObj.perspective = request('/v1/perspectives/' +
// perspectives[0]).then((res) => {
// valuesObj.name = res.body.name;
// return res.body;
// });
// } else {

// // no efault perspective exist, and no perspectives exist
// throw new Error('no perspectives exist.');
// }
const perspectives = responses[0].body;

// assign perspective-related values to the accumulator object
valuesObj.persNames = perspectives.map((perspective) => perspective.name).sort();

// if named perspective or default exists, .body is an object.
if (responses[1]) {
valuesObj.perspective = responses[1].body;
valuesObj.name = valuesObj.perspective.name;

// get default perspective does NOT exist. GET the first perspective by
// alphabetical order.
// check to see there are perspectives
} else if (perspectives.length) {
valuesObj.name = perspectives[0].name;

// redirect to the first perspective. The rest of the code
// won't be executed.
window.location.href = '/perspectives/' + valuesObj.name;
} else {

// no perspectives exist
throw new Error('no perspectives exist.');
}

// valuesObj.perspective have been assigned.
/**
* getLens and getHierarchy are dispatched simultaneously.
* Both lensLoadEvent and hierarchyLoadEvent will be called once:
Expand All @@ -320,7 +317,6 @@ function getValuesObject(request, getPerspectiveUrl, handleHierarchyEvent, handl
* 3. When getHierarchy resolves:
* in handleHierarchyEvent, because gotLens is true, hierarchyLoadEvent
* is dispatched.
* Else if getHierarchy is resolved first:
* 1. in handleHierarchyEvent:
* Because gotLens is false,
Expand All @@ -330,65 +326,65 @@ function getValuesObject(request, getPerspectiveUrl, handleHierarchyEvent, handl
* lensLoadEvent is dispatched. Since hierarchyLoadEvent is truthy,
* it is also dispatched.
*/
// const getLens = request('/v1/lenses/' + valuesObj.perspective.lensId)
// .then((res) => {

// // hierarchyLoadEvent can be undefined or a custom event
// // if hierarchyLoadEvent is custom event, it will be dispatched
// handleLensDomEvent(res.body.library, hierarchyLoadEvent);

// // set the lens received flag to true, to dispatch lens load
// // when hierarchy is resolved in getHierarchy
// gotLens = true;

// return res;
// });

// const filterString = getFilterQuery(valuesObj.perspective);
// const getHierarchy = request('/v1/subjects/' +
// valuesObj.perspective.rootSubject + '/hierarchy' + filterString)
// .then((res) => {

// // if gotLens is false, hierarchyLoadEvent will be assigned
// // and NOT dispatched. Otherwise dispatch the hierarchy event
// hierarchyLoadEvent = handleHierarchyEvent(res.body, gotLens);

// return res;
// });

// const promisesArr = [
// getLens,
// getHierarchy,
// request('/v1/lenses?fields=isPublished,name'),
// request('/v1/subjects?fields=isPublished,absolutePath,tags'),
// request('/v1/aspects?fields=isPublished,name,tags'),
// ];
// return Promise.all(promisesArr);
const getLens = request('/v1/lenses/' + valuesObj.perspective.lensId)
.then((res) => {

// hierarchyLoadEvent can be undefined or a custom event
// if hierarchyLoadEvent is custom event, it will be dispatched
handleLensDomEvent(res.body.library, hierarchyLoadEvent);

// set the lens received flag to true, to dispatch lens load
// when hierarchy is resolved in getHierarchy
gotLens = true;

return res;
});

const filterString = getFilterQuery(valuesObj.perspective);
const getHierarchy = request('/v1/subjects/' +
valuesObj.perspective.rootSubject + '/hierarchy' + filterString)
.then((res) => {

// if gotLens is false, hierarchyLoadEvent will be assigned
// and NOT dispatched. Otherwise dispatch the hierarchy event
hierarchyLoadEvent = handleHierarchyEvent(res.body, gotLens);

return res;
});

const promisesArr = [
getLens,
getHierarchy,
request('/v1/lenses'),
request('/v1/subjects?fields=isPublished,absolutePath,tags'),
request('/v1/aspects?fields=isPublished,name,tags'),
];
return Promise.all(promisesArr);
})
// .then((responses) => {
// const lens = responses[0].body;
// const rootSubject = responses[1].body;
// const lenses = responses[2].body;
// const subjects = responses[3].body;
// const aspects = responses[4].body;

// // assign perspective-related values to the accumulator object
// valuesObj.lens = lens;
// valuesObj.rootSubject = rootSubject;

// // assign non-perspective values to the accumulator object.
// // TODO: subjects are objects. Change to use strings
// valuesObj.subjects = getPublishedFromArr(subjects);
// valuesObj.subjectTagFilter = getTagsFromArrays(valuesObj.subjects);
// valuesObj.lenses = getPublishedFromArr(lenses);

// // aspectFilter is an array of strings
// const publishedAspects = getPublishedFromArr(aspects);
// valuesObj.aspectFilter = publishedAspects.map((aspect) => aspect.name);
// valuesObj.aspectTagFilter = getTagsFromArrays(publishedAspects);

// return valuesObj;
// });
.then((responses) => {
const lens = responses[0].body;
const rootSubject = responses[1].body;
const lenses = responses[2].body;
const subjects = responses[3].body;
const aspects = responses[4].body;

// assign perspective-related values to the accumulator object
valuesObj.lens = lens;
valuesObj.rootSubject = rootSubject;

// assign non-perspective values to the accumulator object.
// TODO: subjects are objects. Change to use strings
valuesObj.subjects = getPublishedFromArr(subjects);
valuesObj.subjectTagFilter = getTagsFromArrays(valuesObj.subjects);
valuesObj.lenses = getPublishedFromArr(lenses);

// aspectFilter is an array of strings
const publishedAspects = getPublishedFromArr(aspects);
valuesObj.aspectFilter = publishedAspects.map((aspect) => aspect.name);
valuesObj.aspectTagFilter = getTagsFromArrays(publishedAspects);

return valuesObj;
});
} // getValuesObject

module.exports = {
Expand Down

0 comments on commit 6533ce1

Please sign in to comment.