import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { LocationStrategy } from '@angular/common' ; import { Observable , of } from 'rxjs' ; import { map , catchError } from 'rxjs/operators' ; import { User , UserOptions , ApiResponse } from '../../shared/models' ; import { AuthService , ApiService } from '../../shared/services' ; interface UpdateUser extends User { new_password ? : string ; old_password ? : string ; } @ Injectable ( ) export class UserSettingsService extends ApiService { activeUser : User = null ; constructor ( private auth , private http , strat ) { super ( strat ) ; auth . userChanged . subscribe ( user => this . activeUser = user ) ; } changeDefaultBoard ( user ) { const json = JSON . stringify ( user ) ; return this . http . post ( this . apiBase + 'users/' + this . activeUser . id , json ) . pipe ( map ( ( response ) => response ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } changePassword ( oldPass , newPass ) { const updateUser = this . activeUser ; updateUser . new_password = newPass ; updateUser . old_password = oldPass ; const json = JSON . stringify ( updateUser ) ; return this . http . post ( this . apiBase + 'users/' + this . activeUser . id , json ) . pipe ( map ( ( response ) => response ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } changeUsername ( newName ) { const updateUser = this . activeUser ; updateUser . username = newName ; const json = JSON . stringify ( updateUser ) ; return this . http . post ( this . apiBase + 'users/' + this . activeUser . id , json ) . pipe ( map ( ( response ) => { this . auth . updateUser ( JSON . parse ( response . data [ 1 ] ) ) ; return response ; } ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } changeEmail ( newEmail ) { const updateUser = this . activeUser ; updateUser . email = newEmail ; const json = JSON . stringify ( updateUser ) ; return this . http . post ( this . apiBase + 'users/' + this . activeUser . id , json ) . pipe ( map ( ( response ) => { this . auth . updateUser ( JSON . parse ( response . data [ 1 ] ) ) ; return response ; } ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } changeUserOptions ( newOptions ) { const json = JSON . stringify ( newOptions ) ; return this . http . post ( this . apiBase + 'users/' + this . activeUser . id + '/opts' , json ) . pipe ( map ( ( response ) => { this . auth . updateUser ( JSON . parse ( response . data [ 2 ] ) , JSON . parse ( response . data [ 1 ] ) ) ; return response ; } ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } }