import { Component , OnInit } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { Observable } from 'rxjs/Observable' ; import { IStarRatingOnClickEvent } from 'angular-star-rating/src/star-rating-struct' ; import { HelpersService , MoviesService , UserService } from './../../../core/services' ; @ Component ( { templateUrl : './movie-details.component.html' , styleUrls : [ './movie-details.component.scss' ] } ) export class MovieDetailsComponent implements OnInit { movie$ : Observable < any > ; movieInternalDetails : any ; movieId : string ; previousUserRating : number = 0 ; constructor ( private route , public us , private ms , private helpers ) { } ngOnInit ( ) { this . movie$ = this . route . data . map ( ( res ) => res . movie ) ; this . route . params . first ( ) . subscribe ( par => { const movieId = par [ 'id' ] ; this . getInternalDetails ( movieId ) ; this . movieId = movieId ; } ) ; this . ms . getMovieRating ( this . movieId ) . first ( ) . subscribe ( res => { this . previousUserRating = res . data . rating || 0 ; } ) ; } rateMovie ( ev ) { this . ms . rateMovie ( this . movieId , ev . rating ) . subscribe ( res => { this . previousUserRating = ev . rating ; this . getInternalDetails ( this . movieId ) ; } , err => this . helpers . showMessage ( 'There was an error while rating the movie' ) ) ; } removeRating ( ) { this . ms . removeRating ( this . movieId ) . subscribe ( res => { this . previousUserRating = 0 ; this . getInternalDetails ( this . movieId ) ; } , err => this . helpers . showMessage ( 'There was an error while removing the rating' ) ) ; } private getInternalDetails ( id ) { this . ms . getMovieInternalDetails ( id ) . map ( res => res . data ) . subscribe ( res => this . movieInternalDetails = res ) ; } }