import { Component , Input } from '@angular/core' ; import { LoginService } from 'app/admin/services/login.service' ; import { UserService } from 'app/shared/user.service' ; import { ModificationService } from 'app/shared/modification.service' ; import { ModificationType } from 'app/shared/enums/modificationType.enum' ; import { Router } from '@angular/router' ; import { User } from 'app/shared/models/user.model' ; @ Component ( { selector : 'mdi-admin-profile-page' , templateUrl : './profilePage.component.html' , styleUrls : [ './profilePage.component.scss' ] , providers : [ LoginService , UserService , ModificationService ] } ) export class AdminProfilePageComponent { public editUser : User = new User ( ) ; public disabledAlias : boolean = true ; constructor ( private loginService , private userService , private router ) { } async ngOnInit ( ) { await this . loginService . isAuthed ( ) ; console . log ( 'authed' ) ; this . loadUser ( ) ; } goBack ( ) { this . router . navigateByUrl ( '/admin/index' ) ; } async logout ( ) { await this . loginService . logout ( ) ; } async loadUser ( ) { this . editUser = await this . userService . getAdminProfile ( ) ; } async submitProfile ( ) { this . editUser = await this . userService . updateAdminProfile ( this . editUser ) ; alert ( 'Updated!' ) ; } resizedataURL ( datas , wantedWidth , wantedHeight ) { return new Promise ( resolve => { var img = document . createElement ( 'img' ) ; img . onload = function ( ) { var canvas = document . createElement ( 'canvas' ) ; var ctx = canvas . getContext ( '2d' ) ; canvas . width = wantedWidth ; canvas . height = wantedHeight ; ctx . drawImage ( this as any , 0 , 0 , wantedWidth , wantedHeight ) ; var dataURI = canvas . toDataURL ( ) ; resolve ( dataURI ) ; } ; img . src = datas ; } ) ; } updatedAvatar : string | null = null ; handleFileSelect ( evt ) { var f = evt . target . files [ 0 ] ; var reader = new FileReader ( ) ; reader . onload = ( ( theFile ) => { return ( e ) => { var binaryData = e . target . result ; var base64String = window . btoa ( binaryData ) ; this . resizedataURL ( `data:image/png;base64,${ base64String }` , 100 , 100 ) . then ( data => { this . updatedAvatar = data as string ; this . updateProfileAvatar ( data ) ; } ) ; } ; } ) ( f ) ; reader . readAsBinaryString ( f ) ; } updateProfileAvatar ( data ) { this . userService . updateAdminProfileAvatar ( data ) . then ( user => { this . editUser = user ; this . updatedAvatar = null ; } ) ; } }