11import app from 'flarum/forum/app' ;
2- import FormModal from 'flarum/common/components/FormModal' ;
2+ import FormModal , { IFormModalAttrs } from 'flarum/common/components/FormModal' ;
33import User from 'flarum/common/models/User' ;
44import Button from 'flarum/common/components/Button' ;
55import Select from 'flarum/common/components/Select' ;
66import { FollowLevels } from '../../common/FollowLevels' ;
77import type Mithril from 'mithril' ;
88
9- interface SelectFollowUserTypeModalAttrs {
9+ interface SelectFollowLevelModalAttrs extends IFormModalAttrs {
1010 user : User ;
1111}
1212
13- interface SelectFollowUserTypeModalState {
13+ interface SelectFollowLevelModalState {
1414 user : User | null ;
1515 saving : boolean ;
1616 followState : 'lurk' | 'follow' | 'unfollow' | undefined ;
1717}
1818
19- // @ts -expect-error - FormModal attrs constraint issue
20- export class SelectFollowUserTypeModal extends FormModal < SelectFollowUserTypeModalAttrs > {
21- // @ts -expect-error - Custom state structure
22- state : SelectFollowUserTypeModalState = {
19+ export default class SelectFollowLevelModal extends FormModal < SelectFollowLevelModalAttrs > {
20+ followState : SelectFollowLevelModalState = {
2321 /**
2422 * User being followed
2523 */
@@ -38,24 +36,24 @@ export class SelectFollowUserTypeModal extends FormModal<SelectFollowUserTypeMod
3836 followState : undefined ,
3937 } ;
4038
41- oninit ( vnode : Mithril . Vnode < SelectFollowUserTypeModalAttrs , this> ) {
39+ oninit ( vnode : Mithril . Vnode < SelectFollowLevelModalAttrs , this> ) {
4240 super . oninit ( vnode ) ;
4341
44- this . state . user = this . attrs . user ;
42+ this . followState . user = this . attrs . user ;
4543
46- const followedStatus = this . state . user . followed ( ) ;
47- this . state . followState = ( followedStatus === true ? 'follow' : followedStatus ) || 'unfollow' ;
44+ const followedStatus = this . followState . user . followed ( ) ;
45+ this . followState . followState = ( followedStatus === true ? 'follow' : followedStatus ) || 'unfollow' ;
4846 }
4947
5048 className = ( ) : string => 'iam_follow_users-selectFollowLevelModal' ;
5149
5250 title ( ) : Mithril . Children {
53- return this . trans ( 'title' , { username : < em > { this . state . user ?. displayName ?.( ) } </ em > } ) ;
51+ return this . trans ( 'title' , { username : < em > { this . followState . user ?. displayName ?.( ) } </ em > } ) ;
5452 }
5553
5654 content ( ) : Mithril . Children {
5755 // If `this.user` isn't a valid User, exit quickly to prevent complete forum errors.
58- if ( ! ( this . state . user instanceof User ) ) {
56+ if ( ! ( this . followState . user instanceof User ) ) {
5957 // Show a more detailed error if this happens when the forum is in debug mode.
6058 return (
6159 < div class = "Modal-body" >
@@ -64,10 +62,10 @@ export class SelectFollowUserTypeModal extends FormModal<SelectFollowUserTypeMod
6462 ) ;
6563 }
6664
67- const user = this . state . user ;
65+ const user = this . followState . user ;
6866
6967 const availableLevelOptions = FollowLevels . reduce ( ( acc , curr ) => ( { ...acc , [ curr . value ] : curr . name ( ) } ) , { } as Record < string , string > ) ;
70- const selectedLevel = FollowLevels . find ( ( l ) => l . value === this . state . followState ) ;
68+ const selectedLevel = FollowLevels . find ( ( l ) => l . value === this . followState . followState ) ;
7169
7270 if ( ! selectedLevel ) {
7371 return null ;
@@ -82,7 +80,7 @@ export class SelectFollowUserTypeModal extends FormModal<SelectFollowUserTypeMod
8280 < label for = "selectFollowLevelModal-select" > { this . trans ( 'follow_select_label' ) } </ label >
8381
8482 < Select
85- disabled = { this . state . saving }
83+ disabled = { this . followState . saving }
8684 id = "selectFollowLevelModal-select"
8785 onchange = { this . onFollowLevelChange . bind ( this ) }
8886 // Dynamic attrs that change based on the input
@@ -96,10 +94,15 @@ export class SelectFollowUserTypeModal extends FormModal<SelectFollowUserTypeMod
9694 </ div >
9795 </ fieldset >
9896 < fieldset class = "selectFollowLevelModal-actions" >
99- < Button disabled = { this . state . saving } class = "Button" onclick = { this . hide . bind ( this ) } >
97+ < Button disabled = { this . followState . saving } class = "Button" onclick = { this . hide . bind ( this ) } >
10098 { this . trans ( 'cancel_btn' ) }
10199 </ Button >
102- < Button disabled = { this . state . saving } class = "Button Button--primary" onclick = { this . saveFollowLevel . bind ( this ) } loading = { this . state . saving } >
100+ < Button
101+ disabled = { this . followState . saving }
102+ class = "Button Button--primary"
103+ onclick = { this . saveFollowLevel . bind ( this ) }
104+ loading = { this . followState . saving }
105+ >
103106 { this . trans ( 'save_btn' ) }
104107 </ Button >
105108 </ fieldset >
@@ -113,7 +116,7 @@ export class SelectFollowUserTypeModal extends FormModal<SelectFollowUserTypeMod
113116 onFollowLevelChange ( ) : void {
114117 const selectElement = this . $ ( '.Select-input' ) [ 0 ] as HTMLInputElement ;
115118
116- this . state . followState = ( selectElement . value as 'lurk' | 'follow' ) || 'unfollow' ;
119+ this . followState . followState = ( selectElement . value as 'lurk' | 'follow' ) || 'unfollow' ;
117120 }
118121
119122 /**
@@ -131,17 +134,17 @@ export class SelectFollowUserTypeModal extends FormModal<SelectFollowUserTypeMod
131134 * Sends the new follow state to the server
132135 */
133136 async saveFollowLevel ( ) : Promise < void > {
134- const newFollowState = this . state . followState === 'unfollow' ? null : this . state . followState ;
137+ const newFollowState = this . followState . followState === 'unfollow' ? null : this . followState . followState ;
135138
136- this . state . saving = true ;
139+ this . followState . saving = true ;
137140
138141 // Exit early if level not changed
139- if ( this . state . user ! . attribute ( 'following' ) === newFollowState ) {
142+ if ( this . followState . user ! . attribute ( 'following' ) === newFollowState ) {
140143 this . hide ( ) ;
141144 return ;
142145 }
143146
144- await this . state . user ! . save ( { followUsers : newFollowState } ) ;
147+ await this . followState . user ! . save ( { followUsers : newFollowState } ) ;
145148
146149 this . hide ( ) ;
147150 }
0 commit comments