@@ -11,10 +11,11 @@ import { ValidationService } from '../../services/messenger/validation.service';
1111import { UpdateAccountInformationCommand } from '../../types/requests/UpdateAccountInformationCommand' ;
1212import { SessionService } from '../../services/api/session.service' ;
1313import { Event , Router } from '@angular/router' ;
14- import { Subject , takeUntil } from 'rxjs' ;
14+ import { firstValueFrom , Subject , takeUntil } from 'rxjs' ;
1515import { RoutingConstants } from 'src/app/types/constants/RoutingConstants' ;
1616import { AppInfoService } from 'src/app/services/api/app-info.service' ;
1717import { ModalWindowStateService } from 'src/app/services/states/modalWindowState.service' ;
18+ import { UpdateProfilePictureResponse } from '../../types/responses/UpdateProfilePictureResponse' ;
1819
1920@Component ( {
2021 selector : 'app-settings' ,
@@ -81,7 +82,7 @@ export class SettingsComponent implements OnInit, OnDestroy {
8182 next : ( response ) => {
8283 this . currentUser = response . user ;
8384
84- if ( response . user . userNameChanged === false ) {
85+ if ( ! response . user . userNameChanged ) {
8586 this . currentUser . username = '' ;
8687 }
8788 } ,
@@ -108,13 +109,13 @@ export class SettingsComponent implements OnInit, OnDestroy {
108109 }
109110
110111 onOpenAvatarClick ( ) : void {
111- this . _modalWindowStateService . setIsModalWindowShowing ( true )
112- this . _modalWindowStateService . setPicture ( this . currentUser . pictureUrl )
112+ this . _modalWindowStateService . setIsModalWindowShowing ( true ) ;
113+ this . _modalWindowStateService . setPicture ( this . currentUser . pictureUrl ) ;
113114 }
114115
115116 closeModalWindowClick ( ) : void {
116- this . _modalWindowStateService . setIsModalWindowShowing ( false )
117- this . _modalWindowStateService . setPictureNull ( )
117+ this . _modalWindowStateService . setIsModalWindowShowing ( false ) ;
118+ this . _modalWindowStateService . setPictureNull ( ) ;
118119 }
119120
120121 onLogoutClick ( ) : void {
@@ -187,7 +188,7 @@ export class SettingsComponent implements OnInit, OnDestroy {
187188 }
188189 }
189190
190- onSaveChangesUpdateProfilePictureClick ( ) : void {
191+ async onSaveChangesUpdateProfilePictureClick ( ) {
191192 const formData = new FormData ( ) ;
192193 const validationResult = this . _validationService . validatePictureFileName ( this . fileName ) ;
193194
@@ -198,19 +199,17 @@ export class SettingsComponent implements OnInit, OnDestroy {
198199 const file = this . file as File ;
199200 formData . append ( 'pictureFile' , file ) ;
200201
201- this . _usersService
202- . updateProfilePicture ( formData )
203- . pipe ( takeUntil ( this . componentDestroyed$ ) )
204- . subscribe ( {
205- next : ( response ) => {
206- this . clearProfilePictureFile ( ) ;
207- alert ( response . message ) ;
208- this . currentUser . pictureUrl = response . newUserPictureUrl ;
209- } ,
210- error : ( error ) => {
211- this . _errorNotificationService . notifyOnError ( error ) ;
212- }
213- } ) ;
202+ const updateProfileImage$ = this . _usersService . updateProfilePicture ( formData ) ;
203+
204+ try {
205+ const result = await firstValueFrom < UpdateProfilePictureResponse > ( updateProfileImage$ ) ;
206+ alert ( result . message ) ;
207+ this . currentUser . pictureUrl = result . newUserPictureUrl ;
208+ } catch ( e : any ) {
209+ alert ( e . error . errorMessage ) ;
210+ }
211+
212+ this . clearProfilePictureFile ( ) ;
214213 }
215214
216215 onSaveChangesChangePasswordClick ( ) : void {
@@ -279,7 +278,14 @@ export class SettingsComponent implements OnInit, OnDestroy {
279278 } ;
280279 }
281280
282- clearProfilePictureFile ( ) : void {
281+ clearProfilePictureFile ( ) {
282+ const fileInput = document . getElementById ( 'ProfilePicture' ) as HTMLInputElement ;
283+
284+ if ( ! fileInput ) {
285+ return ;
286+ }
287+
288+ fileInput . value = '' ;
283289 this . file = null ;
284290 this . fileName = '' ;
285291 }
0 commit comments