import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { LocationStrategy } from '@angular/common' ; import { BehaviorSubject , Observable , of } from 'rxjs' ; import { map , catchError } from 'rxjs/operators' ; import { ApiResponse , User , Board , AutoAction } from '../shared/models' ; import { ApiService } from '../shared/services' ; @ Injectable ( { providedIn : 'root' } ) export class SettingsService extends ApiService { private users = new BehaviorSubject < User [ ] > ( [ ] ) ; private boards = new BehaviorSubject < Board [ ] > ( [ ] ) ; private actions = new BehaviorSubject < AutoAction [ ] > ( [ ] ) ; public usersChanged = this . users . asObservable ( ) ; public boardsChanged = this . boards . asObservable ( ) ; public actionsChanged = this . actions . asObservable ( ) ; constructor ( private http , strat ) { super ( strat ) ; } updateUsers ( users ) { this . users . next ( users ) ; } getUsers ( ) { return this . http . get ( this . apiBase + 'users' ) . pipe ( map ( ( response ) => response ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } updateBoards ( boards ) { this . getActions ( ) . subscribe ( ( response ) => { this . actions . next ( response . data [ 1 ] ) ; this . boards . next ( boards ) ; } ) ; } getBoards ( ) { return this . http . get ( this . apiBase + 'boards' ) . pipe ( map ( ( response ) => response ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } updateActions ( actions ) { this . actions . next ( actions ) ; } getActions ( ) { return this . http . get ( this . apiBase + 'autoactions' ) . pipe ( map ( ( response ) => response ) , catchError ( ( err ) => of ( err . error as ApiResponse ) ) ) ; } }