1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import { Component , OnInit , computed , effect , input , linkedSignal , signal } from '@angular/core' ;
15+ import { Component , computed , effect , input , linkedSignal , signal , untracked } from '@angular/core' ;
1616import { CoreSitesReadingStrategy } from '@services/sites' ;
1717import { CoreNetwork } from '@services/network' ;
1818import { AddonModForum , AddonModForumPost } from '@addons/mod/forum/services/forum' ;
@@ -32,64 +32,83 @@ import { AddonModForumHelper } from '../../services/forum-helper';
3232 CoreSharedModule ,
3333 ] ,
3434} )
35- export class AddonModForumPostOptionsMenuComponent implements OnInit {
35+ export class AddonModForumPostOptionsMenuComponent {
3636
3737 readonly post = input . required < AddonModForumPost > ( ) ; // The post.
3838 readonly cmId = input . required < number > ( ) ;
3939 readonly forumId = input . required < number > ( ) ; // The forum Id.
4040
4141 protected readonly postCalculated = linkedSignal ( ( ) => this . post ( ) ) ;
42- readonly canEdit = computed ( ( ) => this . isOfflinePost ( ) ||
43- ( AddonModForumHelper . canUpdatePost ( this . postCalculated ( ) ) && this . isOnline ( ) ) ) ;
42+ readonly canEdit = computed ( ( ) => this . isOfflinePost ( ) || AddonModForumHelper . canUpdatePost ( this . postCalculated ( ) ) ) ;
4443
45- readonly canDelete = computed ( ( ) => this . isOfflinePost ( ) ||
46- ( AddonModForumHelper . canDeletePost ( this . postCalculated ( ) ) && this . isOnline ( ) ) ) ;
44+ readonly canDelete = computed ( ( ) => this . isOfflinePost ( ) || AddonModForumHelper . canDeletePost ( this . postCalculated ( ) ) ) ;
45+ readonly editDeleteDisabled = computed ( ( ) => this . unknownCapabilities ( ) || ( ! this . isOfflinePost ( ) && ! this . isOnline ( ) ) ) ;
4746
4847 readonly canMarkAsRead = computed ( ( ) =>
49- ! this . isOfflinePost ( ) && AddonModForumHelper . canSetReadState ( this . postCalculated ( ) ) && this . isOnline ( ) ) ;
48+ ! this . isOfflinePost ( ) && AddonModForumHelper . canSetReadState ( this . postCalculated ( ) ) ) ;
49+
50+ readonly markAsReadDisabled = computed ( ( ) => this . unknownCapabilities ( ) || ! this . isOnline ( ) ) ;
5051
5152 readonly loaded = signal ( false ) ;
5253
5354 readonly isOfflinePost = computed ( ( ) => this . postCalculated ( ) . id < 0 ) ;
5455 protected readonly isOnline = CoreNetwork . onlineSignal ;
5556
56- constructor ( ) {
57- effect ( ( ) => {
58- // Dismiss the modal to prevent having an empty menu.
59- if ( ! this . canDelete ( ) && ! this . canEdit ( ) && ! this . canMarkAsRead ( ) ) {
60- this . dismiss ( ) ;
61- }
62- } ) ;
63- }
57+ protected readonly unknownCapabilities = computed ( ( ) => {
58+ if ( this . isOfflinePost ( ) ) {
59+ return false ;
60+ }
61+
62+ const post = this . postCalculated ( ) ;
63+
64+ return post . capabilities . delete === undefined ;
65+ } ) ;
6466
6567 /**
6668 * @inheritdoc
6769 */
68- async ngOnInit ( ) : Promise < void > {
69- if ( this . isOfflinePost ( ) ) {
70- this . loaded . set ( true ) ;
70+ constructor ( ) {
71+ effect ( async ( ) => {
72+ const unknownCapabilities = this . unknownCapabilities ( ) ;
73+ const isOnline = this . isOnline ( ) ;
7174
72- return ;
73- }
75+ if ( ! unknownCapabilities ) {
76+ this . loaded . set ( true ) ;
77+
78+ return ;
79+ }
80+
81+ // Only check this one because the others are always available if this one is.
82+ const wsAvailable = AddonModForum . isDeletePostAvailable ( ) ;
83+ if ( ! wsAvailable ) {
84+ // If the WS is not available, we cannot do anything.
85+ this . loaded . set ( true ) ;
86+
87+ return ;
88+ }
7489
75- let post = this . postCalculated ( ) ;
76- if ( post . capabilities . delete === undefined ) {
7790 const forumId = this . forumId ( ) ;
78- if ( forumId ) {
79- try {
80- post =
81- await AddonModForum . getDiscussionPost ( forumId , post . discussionid , post . id , {
82- cmId : this . cmId ( ) ,
83- readingStrategy : CoreSitesReadingStrategy . ONLY_NETWORK ,
84- } ) ;
85- this . postCalculated . set ( post ) ;
86- } catch ( error ) {
87- CoreAlerts . showError ( error , { default : 'Error getting discussion post.' } ) ;
88- }
91+ if ( forumId && isOnline ) {
92+ this . loaded . set ( false ) ;
93+
94+ await untracked ( async ( ) => {
95+ let post = this . postCalculated ( ) ;
96+
97+ try {
98+ post =
99+ await AddonModForum . getDiscussionPost ( forumId , post . discussionid , post . id , {
100+ cmId : this . cmId ( ) ,
101+ readingStrategy : CoreSitesReadingStrategy . ONLY_NETWORK ,
102+ } ) ;
103+ this . postCalculated . set ( post ) ;
104+ } catch ( error ) {
105+ CoreAlerts . showError ( error , { default : 'Error getting discussion post.' } ) ;
106+ }
107+ } ) ;
89108 }
90- }
91109
92- this . loaded . set ( true ) ;
110+ this . loaded . set ( true ) ;
111+ } ) ;
93112 }
94113
95114 /**
0 commit comments