@@ -77,6 +77,7 @@ const settings = () => {
7777 saveBillingInfoMutation : null as MutationState < TutorMutationResponse < string > > | null ,
7878 saveWithdrawMethodMutation : null as MutationState < TutorMutationResponse < string > > | null ,
7979 resetPasswordMutation : null as MutationState < TutorMutationResponse < string > > | null ,
80+ handleUpdateNotification : null as MutationState < unknown , unknown > | null ,
8081
8182 init ( ) {
8283 if ( ! this . $el ) {
@@ -91,6 +92,15 @@ const settings = () => {
9192 this . handleSaveWithdrawMethod = this . handleSaveWithdrawMethod . bind ( this ) ;
9293 this . handleResetPassword = this . handleResetPassword . bind ( this ) ;
9394
95+ this . handleUpdateNotification = this . query . useMutation ( this . updateNotification , {
96+ onSuccess : ( data : TutorMutationResponse < string > ) => {
97+ this . toast . success ( data ?. message ?? __ ( 'Successfully updated profile' , 'tutor' ) ) ;
98+ } ,
99+ onError : ( error : Error ) => {
100+ this . toast . error ( convertToErrorMessage ( error ) || __ ( 'Failed to update profile' , 'tutor' ) ) ;
101+ } ,
102+ } ) ;
103+
94104 this . fetchCountriesQuery = this . query . useQuery ( 'fetch-countries' , ( ) => this . fetchCountries ( ) ) ;
95105
96106 this . uploadProfilePhotoMutation = this . query . useMutation ( this . uploadProfilePhoto , {
@@ -151,6 +161,34 @@ const settings = () => {
151161 } ) ;
152162 } ,
153163
164+ async updateNotification ( payload : Record < string , boolean > ) {
165+ // Transform boolean values to "on"/"off" strings and restructure keys
166+ const transformedPayload = Object . keys ( payload ) . reduce (
167+ ( formattedPayload , key ) => {
168+ const value = payload [ key ] ;
169+ const stringValue = typeof value === 'boolean' ? ( value ? 'on' : 'off' ) : value ;
170+
171+ // Check if key contains double underscore
172+ if ( key . includes ( '__' ) ) {
173+ const [ firstPart , secondPart ] = key . split ( '__' ) ;
174+ if ( firstPart && secondPart ) {
175+ const transformedKey = `tutor_notification_preference[email][${ firstPart } ][${ secondPart } ]` ;
176+ formattedPayload [ transformedKey ] = stringValue ;
177+ }
178+ } else {
179+ const transformedKey = `tutor_notification_preference[${ key } ]` ;
180+ formattedPayload [ transformedKey ] = stringValue ;
181+ }
182+
183+ return formattedPayload ;
184+ } ,
185+ { } as Record < string , string > ,
186+ ) ;
187+
188+ console . log ( 'noti' , transformedPayload ) ;
189+ return wpAjaxInstance . post ( endpoints . UPDATE_PROFILE_NOTIFICATION , transformedPayload ) . then ( ( res ) => res . data ) ;
190+ } ,
191+
154192 async fetchCountries ( ) {
155193 return await axios . get ( `${ tutorConfig . tutor_url } ${ endpoints . FETCH_COUNTRIES } ` ) . then ( ( res ) => res . data ) ;
156194 } ,
0 commit comments