From ea899125ebc9725d1606a138fad2b6e75fba366e Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Mon, 7 Jun 2021 17:21:14 +0200 Subject: [PATCH] MOBILE-3320 forum: Fix ratings reactivity --- .../rating/components/aggregate/aggregate.ts | 35 ++++++++++--------- src/core/features/rating/services/rating.ts | 5 ++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/core/features/rating/components/aggregate/aggregate.ts b/src/core/features/rating/components/aggregate/aggregate.ts index 77e34bda1..3c33cf348 100644 --- a/src/core/features/rating/components/aggregate/aggregate.ts +++ b/src/core/features/rating/components/aggregate/aggregate.ts @@ -47,7 +47,7 @@ export class CoreRatingAggregateComponent implements OnChanges, OnDestroy { disabled = false; labelKey = ''; - protected aggregateObserver: CoreEventObserver; + protected aggregateObserver?: CoreEventObserver; protected updateSiteObserver: CoreEventObserver; constructor() { @@ -57,27 +57,14 @@ export class CoreRatingAggregateComponent implements OnChanges, OnDestroy { this.updateSiteObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => { this.disabled = CoreRating.isRatingDisabledInSite(); }, CoreSites.getCurrentSiteId()); - - // Update aggrgate when the user adds or edits a rating. - this.aggregateObserver = - CoreEvents.on(CoreRatingProvider.AGGREGATE_CHANGED_EVENT, (data) => { - if (this.item && - data.contextLevel == this.contextLevel && - data.instanceId == this.instanceId && - data.component == this.ratingInfo.component && - data.ratingArea == this.ratingInfo.ratingarea && - data.itemId == this.itemId) { - this.item.aggregatestr = data.aggregate; - this.item.count = data.count; - } - }); } /** * Detect changes on input properties. */ ngOnChanges(): void { - this.aggregateObserver && this.aggregateObserver.off(); + this.aggregateObserver?.off(); + delete this.aggregateObserver; this.item = (this.ratingInfo.ratings || []).find((rating) => rating.itemid == this.itemId); if (!this.item) { @@ -107,6 +94,20 @@ export class CoreRatingAggregateComponent implements OnChanges, OnDestroy { } this.showCount = (this.aggregateMethod != CoreRatingProvider.AGGREGATE_COUNT); + + // Update aggrgate when the user adds or edits a rating. + this.aggregateObserver = + CoreEvents.on(CoreRatingProvider.AGGREGATE_CHANGED_EVENT, (data) => { + if (this.item && + data.contextLevel == this.contextLevel && + data.instanceId == this.instanceId && + data.component == this.ratingInfo.component && + data.ratingArea == this.ratingInfo.ratingarea && + data.itemId == this.itemId) { + this.item.aggregatestr = data.aggregate; + this.item.count = data.count; + } + }); } /** @@ -135,7 +136,7 @@ export class CoreRatingAggregateComponent implements OnChanges, OnDestroy { * Component being destroyed. */ ngOnDestroy(): void { - this.aggregateObserver.off(); + this.aggregateObserver?.off(); this.updateSiteObserver.off(); } diff --git a/src/core/features/rating/services/rating.ts b/src/core/features/rating/services/rating.ts index 51f81c11a..25642d32a 100644 --- a/src/core/features/rating/services/rating.ts +++ b/src/core/features/rating/services/rating.ts @@ -122,7 +122,8 @@ export class CoreRatingProvider { try { await CoreRatingOffline.deleteRating(component, ratingArea, contextLevel, instanceId, itemId, siteId); - this.addRatingOnline( + + const response = await this.addRatingOnline( component, ratingArea, contextLevel, @@ -134,6 +135,8 @@ export class CoreRatingProvider { aggregateMethod, siteId, ); + + return response; } catch (error) { if (CoreUtils.isWebServiceError(error)) { // The WebService has thrown an error or offline not supported, reject.