MOBILE-3213 forum: Fix discussion links when nested view is on

main
Pau Ferrer Ocaña 2019-12-13 10:29:10 +01:00
parent 6c47098dd8
commit 63bff7c4f8
3 changed files with 14 additions and 15 deletions

View File

@ -326,10 +326,17 @@ export class AddonModForumDiscussionPage implements OnDestroy {
}).then(() => {
let posts = offlineReplies.concat(onlinePosts);
const startingPost = this.forumProvider.extractStartingPost(posts);
if (startingPost) {
// Update discussion data from first post.
this.discussion = Object.assign(this.discussion || {}, startingPost);
}
// If sort type is nested, normal sorting is disabled and nested posts will be displayed.
if (this.sort == 'nested') {
// Sort first by creation date to make format tree work.
this.forumProvider.sortDiscussionPosts(posts, 'ASC');
posts = this.utils.formatTree(posts, 'parent', 'id', this.discussion.id);
} else {
// Set default reply subject.
@ -364,7 +371,7 @@ export class AddonModForumDiscussionPage implements OnDestroy {
}
}));
// Fetch the discussion if not passed as parameter.
// The discussion object was not passed as parameter and there is no starting post. Should not happen.
if (!this.discussion) {
promises.push(this.loadDiscussion(this.forumId, this.discussionId));
}
@ -373,12 +380,9 @@ export class AddonModForumDiscussionPage implements OnDestroy {
}).catch(() => {
// Ignore errors.
}).then(() => {
const startingPost = this.forumProvider.extractStartingPost(posts);
if (startingPost) {
// Update discussion data from first post.
this.discussion = Object.assign(this.discussion || {}, startingPost);
} else if (!this.discussion) {
// The discussion object was not passed as parameter and there is no starting post.
if (!this.discussion) {
// The discussion object was not passed as parameter and there is no starting post. Should not happen.
return Promise.reject('Invalid forum discussion.');
}

View File

@ -51,7 +51,7 @@ export class AddonModForumDiscussionLinkHandler extends CoreContentLinksHandlerB
return [{
action: (siteId, navCtrl?): void => {
const pageParams: any = {
courseId: courseId || parseInt(params.courseid, 10) || parseInt(params.cid, 10),
courseId: courseId || parseInt(params.courseid, 10) || parseInt(params.cid, 10) || undefined,
discussionId: parseInt(params.d, 10),
cmId: data.cmid && parseInt(data.cmid, 10),
forumId: data.instance && parseInt(data.instance, 10)

View File

@ -278,14 +278,9 @@ export class AddonModForumProvider {
* @return Starting post or undefined if not found.
*/
extractStartingPost(posts: any[]): any {
// Check the last post first, since they'll usually be ordered by create time.
for (let i = posts.length - 1; i >= 0; i--) {
if (posts[i].parent == 0) {
return posts.splice(i, 1).pop(); // Remove it from the array.
}
}
const index = posts.findIndex((post) => post.parent == 0);
return undefined;
return index >= 0 ? posts.splice(index, 1).pop() : undefined;
}
/**