MOBILE-4270 comments: Update cached data after add/delete
parent
f0ba0cc479
commit
15faf46367
|
@ -187,11 +187,7 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
|
|
||||||
this.comments = comments.concat(this.comments);
|
this.comments = comments.concat(this.comments);
|
||||||
|
|
||||||
this.comments.forEach((comment, index) => {
|
this.comments.forEach((comment, index) => this.calculateCommentData(comment, this.comments[index - 1]));
|
||||||
comment.showDate = this.showDate(comment, this.comments[index - 1]);
|
|
||||||
comment.showUserData = this.showUserData(comment, this.comments[index - 1]);
|
|
||||||
comment.showTail = this.showTail(comment, this.comments[index + 1]);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.canDeleteComments = this.addDeleteCommentsAvailable &&
|
this.canDeleteComments = this.addDeleteCommentsAvailable &&
|
||||||
(this.hasOffline || this.comments.some((comment) => !!comment.delete));
|
(this.hasOffline || this.comments.some((comment) => !!comment.delete));
|
||||||
|
@ -216,6 +212,18 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate some comment data.
|
||||||
|
*
|
||||||
|
* @param comment Comment.
|
||||||
|
* @param prevComment Previous comment.
|
||||||
|
*/
|
||||||
|
protected calculateCommentData(comment: CoreCommentsDataToDisplay, prevComment?: CoreCommentsDataToDisplay): void {
|
||||||
|
comment.showDate = this.showDate(comment, prevComment);
|
||||||
|
comment.showUserData = this.showUserData(comment, prevComment);
|
||||||
|
comment.showTail = this.showTail(comment, prevComment);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to load more commemts.
|
* Function to load more commemts.
|
||||||
*
|
*
|
||||||
|
@ -245,9 +253,8 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
this.refreshIcon = CoreConstants.ICON_LOADING;
|
this.refreshIcon = CoreConstants.ICON_LOADING;
|
||||||
this.syncIcon = CoreConstants.ICON_LOADING;
|
this.syncIcon = CoreConstants.ICON_LOADING;
|
||||||
|
|
||||||
try {
|
await CoreUtils.ignoreErrors(this.invalidateComments());
|
||||||
await this.invalidateComments();
|
|
||||||
} finally {
|
|
||||||
this.page = 0;
|
this.page = 0;
|
||||||
this.comments = [];
|
this.comments = [];
|
||||||
|
|
||||||
|
@ -257,7 +264,6 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
refresher?.complete();
|
refresher?.complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show sync warnings if any.
|
* Show sync warnings if any.
|
||||||
|
@ -325,13 +331,11 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
if (commentsResponse) {
|
if (commentsResponse) {
|
||||||
this.invalidateComments();
|
this.invalidateComments();
|
||||||
|
|
||||||
const addedComments = await this.loadCommentProfile(commentsResponse);
|
const addedComment = await this.loadCommentProfile(commentsResponse);
|
||||||
addedComments.showDate = this.showDate(addedComments, this.comments[this.comments.length - 1]);
|
this.calculateCommentData(addedComment, this.comments[this.comments.length - 1]);
|
||||||
addedComments.showUserData = this.showUserData(addedComments, this.comments[this.comments.length - 1]);
|
|
||||||
addedComments.showTail = this.showTail(addedComments, this.comments[this.comments.length + 1]);
|
|
||||||
|
|
||||||
// Add the comment to the top.
|
// Add the comment to the top.
|
||||||
this.comments = this.comments.concat([addedComments]);
|
this.comments = this.comments.concat([addedComment]);
|
||||||
this.canDeleteComments = this.addDeleteCommentsAvailable;
|
this.canDeleteComments = this.addDeleteCommentsAvailable;
|
||||||
|
|
||||||
CoreEvents.trigger(CoreCommentsProvider.COMMENTS_COUNT_CHANGED_EVENT, {
|
CoreEvents.trigger(CoreCommentsProvider.COMMENTS_COUNT_CHANGED_EVENT, {
|
||||||
|
@ -343,6 +347,8 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
countChange: 1,
|
countChange: 1,
|
||||||
}, CoreSites.getCurrentSiteId());
|
}, CoreSites.getCurrentSiteId());
|
||||||
|
|
||||||
|
this.refreshInBackground();
|
||||||
|
|
||||||
} else if (commentsResponse === false) {
|
} else if (commentsResponse === false) {
|
||||||
// Comments added in offline mode.
|
// Comments added in offline mode.
|
||||||
await this.loadOfflineData();
|
await this.loadOfflineData();
|
||||||
|
@ -410,6 +416,8 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
area: this.area,
|
area: this.area,
|
||||||
countChange: -1,
|
countChange: -1,
|
||||||
}, CoreSites.getCurrentSiteId());
|
}, CoreSites.getCurrentSiteId());
|
||||||
|
|
||||||
|
this.refreshInBackground();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.loadOfflineData();
|
this.loadOfflineData();
|
||||||
|
@ -602,6 +610,28 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
|
||||||
this.showDelete = !this.showDelete;
|
this.showDelete = !this.showDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh cached data in background.
|
||||||
|
*/
|
||||||
|
protected async refreshInBackground(): Promise<void> {
|
||||||
|
await CoreUtils.ignoreErrors(this.invalidateComments());
|
||||||
|
|
||||||
|
const promises: Promise<unknown>[] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i <= this.page; i++) {
|
||||||
|
promises.push(CoreComments.getComments(
|
||||||
|
this.contextLevel,
|
||||||
|
this.instanceId,
|
||||||
|
this.componentName,
|
||||||
|
this.itemId,
|
||||||
|
this.area,
|
||||||
|
i,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue