import { Component , OnInit , Inject } from '@angular/core' ; import { Router } from '@angular/router' ; import { Profile } from '../shared/profile' ; import { Friend } from '../shared/friend' ; import { ConnectedAccount } from '../shared/connectedAccount' ; import { ProfileService } from '../services/profile.service' ; import { FriendService } from '../services/friend.service' ; import { ConnectService } from '../services/connect.service' ; import { AuthHelper } from "../util/authHelper" ; import { UrlHelper } from '../util/urlHelper' ; import { Constants } from '../util/constants' ; import { CommonUtil } from "../util/commonUtil" ; @ Component ( { selector : 'profile' , templateUrl : './profile.component.html' , styleUrls : [ './profile.component.css' , '../shared/shared.css' ] } ) export class ProfileComponent implements OnInit { isEditMode : Boolean = false ; profileId ? : number ; hasAccountNotConnected : Boolean = false ; isValidated : Boolean = true ; currentProfile : Profile = new Profile ( ) ; isAlreadyFriend : Boolean = false ; disableAddFriendAndChatId = Constants . seedFriendIdBegin ; disableAddFriendTip = '' ; constructor ( private profileService , private connectService , private friendService , private router , @ Inject ( 'auth' ) private auth ) { } ngOnInit ( ) { if ( ! this . auth . isLogin ( ) ) { this . auth . reLogin ( ) ; } this . checkIsEditMode ( ) ; if ( this . isEditMode ) { this . profileService . getCurrentUserProfile ( ) . subscribe ( profile => { this . currentProfile = profile ; this . tryParsePosition ( ) ; this . checkConnectedAccounts ( ) ; } ) ; } else { this . profileService . getUserProfile ( this . profileId ) . subscribe ( profile => { this . currentProfile = profile ; this . tryParsePosition ( ) ; this . friendService . getFriends ( ) . subscribe ( ( friends ) => { friends . forEach ( f => { if ( f . friendProfile . id == this . currentProfile . id ) this . isAlreadyFriend = true ; } ) ; } ) ; } ) ; } } checkConnectedAccounts ( ) { this . connectService . getConnectedAccounts ( ) . subscribe ( ( resp ) => { var socialProviderArray = resp as ConnectedAccount [ ] ; if ( socialProviderArray . length < 2 ) this . hasAccountNotConnected = true ; } ) ; } tryParsePosition ( ) { this . currentProfile . position = this . profileService . getProfilePosition ( this . currentProfile ) ; } doChat ( id ) { if ( parseInt ( id ) >= this . disableAddFriendAndChatId ) { this . disableAddFriendTip = Constants . disableAddFriendAndChatTip ; return ; } id && this . router . navigate ( [ 'chat' , id ] ) ; } saveProfile ( ) { this . checkPhoneNumber ( this . currentProfile . phone_number ) ; if ( ! this . isValidated ) return ; this . profileService . updateCurrentUserProfile ( this . currentProfile ) . subscribe ( resp => { location . reload ( ) ; } , error => { console . log ( error ) ; } ) ; } checkPhoneNumber ( newValue ) { if ( newValue ) this . isValidated = true ; else this . isValidated = false ; } goTo ( url ) { this . router . navigate ( [ url ] ) ; } addFriend ( friendId ) { if ( ! friendId ) return ; if ( parseInt ( friendId ) >= this . disableAddFriendAndChatId ) { this . disableAddFriendTip = Constants . disableAddFriendAndChatTip ; return ; } this . friendService . addFriend ( friendId ) . subscribe ( ( resp ) => { if ( resp == false ) return ; this . isAlreadyFriend = true ; } ) ; } private checkIsEditMode ( ) { this . profileId = UrlHelper . getQueryId ( ) ; if ( isNaN ( this . profileId ) || this . profileId < 0 ) { this . isEditMode = true ; } else { this . isEditMode = false ; } } }