diff --git a/src/addons/mod/forum/components/post/post.ts b/src/addons/mod/forum/components/post/post.ts index a7021eee4..a59a9c1da 100644 --- a/src/addons/mod/forum/components/post/post.ts +++ b/src/addons/mod/forum/components/post/post.ts @@ -54,6 +54,7 @@ import { AddonModForumEditPostComponent } from '../edit-post/edit-post'; import { CoreRatingInfo } from '@features/rating/services/rating'; import { CoreForms } from '@singletons/form'; import { CoreFileEntry } from '@services/file-helper'; +import { AddonModForumSharedReplyData } from '../../pages/discussion/discussion.page'; /** * Components that shows a discussion post, its attachments and the action buttons allowed (reply, etc.). @@ -71,7 +72,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges @Input() discussion?: AddonModForumDiscussion; // Post's' discussion, only for starting posts. @Input() component!: string; // Component this post belong to. @Input() componentId!: number; // Component ID. - @Input() replyData!: AddonModForumReply; // Object with the new post data. Usually shared between posts. + @Input() replyData!: AddonModForumSharedReplyData; // Object with the new post data. Usually shared between posts. @Input() originalData!: Omit; // Object with the original post data. Usually shared between posts. @Input() trackPosts!: boolean; // True if post is being tracked. @Input() forum!: AddonModForumData; // The forum the post belongs to. Required for attachments and offline posts. @@ -93,8 +94,6 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges displaySubject = true; optionsMenuEnabled = false; - protected syncId!: string; - constructor( protected elementRef: ElementRef, @Optional() protected content?: IonContent, @@ -372,8 +371,8 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges try { await this.confirmDiscard(); - this.syncId = AddonModForumSync.getDiscussionSyncId(this.discussionId); - CoreSync.blockOperation(AddonModForumProvider.COMPONENT, this.syncId); + this.replyData.syncId = AddonModForumSync.getDiscussionSyncId(this.discussionId); + CoreSync.blockOperation(AddonModForumProvider.COMPONENT, this.replyData.syncId); this.setReplyFormData( this.post.parentid, @@ -498,9 +497,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges CoreForms.triggerFormSubmittedEvent(this.formElement, sent, CoreSites.getCurrentSiteId()); - if (this.syncId) { - CoreSync.unblockOperation(AddonModForumProvider.COMPONENT, this.syncId); - } + this.unblockOperation(); } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'addon.mod_forum.couldnotadd', true); } finally { @@ -520,9 +517,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges CoreForms.triggerFormCancelledEvent(this.formElement, CoreSites.getCurrentSiteId()); - if (this.syncId) { - CoreSync.unblockOperation(AddonModForumProvider.COMPONENT, this.syncId); - } + this.unblockOperation(); } catch (error) { // Cancelled. } @@ -552,9 +547,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges this.onPostChange.emit(); - if (this.syncId) { - CoreSync.unblockOperation(AddonModForumProvider.COMPONENT, this.syncId); - } + this.unblockOperation(); } catch (error) { // Cancelled. } @@ -578,9 +571,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges * Component being destroyed. */ ngOnDestroy(): void { - if (this.syncId) { - CoreSync.unblockOperation(AddonModForumProvider.COMPONENT, this.syncId); - } + this.unblockOperation(); } /** @@ -588,13 +579,25 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges * * @return Promise resolved if the user confirms or data was not changed and rejected otherwise. */ - protected confirmDiscard(): Promise { + protected async confirmDiscard(): Promise { if (AddonModForumHelper.hasPostDataChanged(this.replyData, this.originalData)) { // Show confirmation if some data has been modified. - return CoreDomUtils.showConfirm(Translate.instant('core.confirmloss')); - } else { - return Promise.resolve(); + await CoreDomUtils.showConfirm(Translate.instant('core.confirmloss')); } + + this.unblockOperation(); + } + + /** + * Unblock operation if there's any blocked operation. + */ + protected unblockOperation(): void { + if (!this.replyData.syncId) { + return; + } + + CoreSync.unblockOperation(AddonModForumProvider.COMPONENT, this.replyData.syncId); + delete this.replyData.syncId; } } diff --git a/src/addons/mod/forum/pages/discussion/discussion.page.ts b/src/addons/mod/forum/pages/discussion/discussion.page.ts index c9b89e9c6..ae7f6d128 100644 --- a/src/addons/mod/forum/pages/discussion/discussion.page.ts +++ b/src/addons/mod/forum/pages/discussion/discussion.page.ts @@ -74,7 +74,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes postHasOffline!: boolean; sort: SortType = 'nested'; trackPosts!: boolean; - replyData: Omit = { + replyData: AddonModForumSharedReplyData = { replyingTo: 0, isEditing: false, subject: '', @@ -795,3 +795,10 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes } } + +/** + * Reply data shared by post. + */ +export type AddonModForumSharedReplyData = Omit & { + syncId?: string; // Sync ID if some post has blocked synchronization. +};