diff --git a/src/addons/mod/forum/components/post-options-menu/post-options-menu.html b/src/addons/mod/forum/components/post-options-menu/post-options-menu.html index 2ff947373..63660ebd7 100644 --- a/src/addons/mod/forum/components/post-options-menu/post-options-menu.html +++ b/src/addons/mod/forum/components/post-options-menu/post-options-menu.html @@ -1,11 +1,11 @@ - +

{{ 'addon.mod_forum.edit' | translate }}

- +

{{ 'addon.mod_forum.delete' | translate }}

diff --git a/src/addons/mod/forum/components/post-options-menu/post-options-menu.ts b/src/addons/mod/forum/components/post-options-menu/post-options-menu.ts index 257d8ec78..02015d077 100644 --- a/src/addons/mod/forum/components/post-options-menu/post-options-menu.ts +++ b/src/addons/mod/forum/components/post-options-menu/post-options-menu.ts @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Input, OnDestroy, OnInit } from '@angular/core'; +import { Component, Input, OnInit } from '@angular/core'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; import { CoreApp } from '@services/app'; import { AddonModForum, AddonModForumPost } from '@addons/mod/forum/services/forum'; -import { Network, NgZone, PopoverController } from '@singletons'; -import { Subscription } from 'rxjs'; +import { PopoverController } from '@singletons'; import { CoreDomUtils } from '@services/utils/dom'; +import { CoreNetworkError } from '@classes/errors/network-error'; /** * This component is meant to display a popover with the post options. @@ -28,7 +28,7 @@ import { CoreDomUtils } from '@services/utils/dom'; templateUrl: 'post-options-menu.html', styleUrls: ['./post-options-menu.scss'], }) -export class AddonModForumPostOptionsMenuComponent implements OnInit, OnDestroy { +export class AddonModForumPostOptionsMenuComponent implements OnInit { @Input() post!: AddonModForumPost; // The post. @Input() cmId!: number; @@ -38,32 +38,15 @@ export class AddonModForumPostOptionsMenuComponent implements OnInit, OnDestroy canDelete = false; loaded = false; url?: string; - isOnline!: boolean; - offlinePost!: boolean; - - protected onlineObserver?: Subscription; + offlinePost = false; /** - * Component being initialized. + * @inheritdoc */ async ngOnInit(): Promise { - this.isOnline = CoreApp.isOnline(); - - this.onlineObserver = Network.onChange().subscribe(() => { - // Execute the callback in the Angular zone, so change detection doesn't stop working. - NgZone.run(() => { - this.isOnline = CoreApp.isOnline(); - }); - }); - - if (this.post.id > 0) { - const site = CoreSites.getRequiredCurrentSite(); - this.url = site.createSiteUrl('/mod/forum/discuss.php', { d: this.post.discussionid.toString() }, 'p' + this.post.id); - this.offlinePost = false; - } else { - // Offline post, you can edit or discard the post. + this.offlinePost = this.post.id < 0; + if (this.offlinePost) { this.loaded = true; - this.offlinePost = true; return; } @@ -81,6 +64,8 @@ export class AddonModForumPostOptionsMenuComponent implements OnInit, OnDestroy } } else { this.loaded = true; + // Display the open in browser button to prevent having an empty menu. + this.setOpenInBrowserUrl(); return; } @@ -88,14 +73,20 @@ export class AddonModForumPostOptionsMenuComponent implements OnInit, OnDestroy this.canDelete = !!this.post.capabilities.delete && AddonModForum.isDeletePostAvailable(); this.canEdit = !!this.post.capabilities.edit && AddonModForum.isUpdatePostAvailable(); + if (!this.canDelete && !this.canEdit) { + // Display the open in browser button to prevent having an empty menu. + this.setOpenInBrowserUrl(); + } + this.loaded = true; } /** - * Component destroyed. + * Set the URL to open in browser. */ - ngOnDestroy(): void { - this.onlineObserver?.unsubscribe(); + protected setOpenInBrowserUrl(): void { + const site = CoreSites.getRequiredCurrentSite(); + this.url = site.createSiteUrl('/mod/forum/discuss.php', { d: this.post.discussionid.toString() }, 'p' + this.post.id); } /** @@ -110,6 +101,12 @@ export class AddonModForumPostOptionsMenuComponent implements OnInit, OnDestroy */ deletePost(): void { if (!this.offlinePost) { + if (!CoreApp.isOnline()) { + CoreDomUtils.showErrorModal(new CoreNetworkError()); + + return; + } + PopoverController.dismiss({ action: 'delete' }); } else { PopoverController.dismiss({ action: 'deleteoffline' }); @@ -120,6 +117,12 @@ export class AddonModForumPostOptionsMenuComponent implements OnInit, OnDestroy * Edit a post. */ editPost(): void { + if (!this.offlinePost && !CoreApp.isOnline()) { + CoreDomUtils.showErrorModal(new CoreNetworkError()); + + return; + } + PopoverController.dismiss({ action: 'edit' }); } diff --git a/src/addons/mod/forum/components/post/post.ts b/src/addons/mod/forum/components/post/post.ts index 5f0acd153..8ece16ae3 100644 --- a/src/addons/mod/forum/components/post/post.ts +++ b/src/addons/mod/forum/components/post/post.ts @@ -116,8 +116,15 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges this.defaultReplySubject = this.post.replysubject || ((this.post.subject.startsWith('Re: ') || this.post.subject.startsWith(reTranslated)) ? this.post.subject : `${reTranslated} ${this.post.subject}`); - this.optionsMenuEnabled = this.post.id < 0 || (AddonModForum.isGetDiscussionPostAvailable() && - (AddonModForum.isDeletePostAvailable() || AddonModForum.isUpdatePostAvailable())); + if (this.post.id < 0) { + this.optionsMenuEnabled = true; + } else if (this.post.capabilities.delete !== undefined) { + this.optionsMenuEnabled = this.post.capabilities.delete === true || this.post.capabilities.edit === true; + } else { + // Cannot know if the user can edit/delete or not, display the menu if the WebServices are available. + this.optionsMenuEnabled = this.post.id < 0 || (AddonModForum.isGetDiscussionPostAvailable() && + (AddonModForum.isDeletePostAvailable() || AddonModForum.isUpdatePostAvailable())); + } } /** diff --git a/src/addons/mod/forum/pages/discussion/discussion.html b/src/addons/mod/forum/pages/discussion/discussion.html index 1c5b8d781..d41503ce8 100644 --- a/src/addons/mod/forum/pages/discussion/discussion.html +++ b/src/addons/mod/forum/pages/discussion/discussion.html @@ -53,6 +53,8 @@ [content]="'addon.mod_forum.removefromfavourites' | translate" iconAction="fas-star" [iconSlash]="true" (action)="toggleFavouriteState(false)"> + diff --git a/src/addons/mod/forum/pages/discussion/discussion.page.ts b/src/addons/mod/forum/pages/discussion/discussion.page.ts index b7c88be26..2b422913e 100644 --- a/src/addons/mod/forum/pages/discussion/discussion.page.ts +++ b/src/addons/mod/forum/pages/discussion/discussion.page.ts @@ -107,6 +107,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes availabilityMessage: string | null = null; showQAMessage = false; leavingPage = false; + externalUrl?: string; protected forumId?: number; protected postId?: number; @@ -164,6 +165,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes } this.isOnline = CoreApp.isOnline(); + this.externalUrl = CoreSites.getCurrentSite()?.createSiteUrl('/mod/forum/discuss.php', { d: this.discussionId.toString() }); this.onlineObserver = Network.onChange().subscribe(() => { // Execute the callback in the Angular zone, so change detection doesn't stop working. NgZone.run(() => {