andreadc

star-rating.component.ts

Feb 27th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class StarRatingComponent implements OnInit {
  2.  
  3.   @Input() userId: number;
  4.  
  5.   ratingArray:string[] = [];
  6.   rating:number;
  7.   count:number;
  8.  
  9.   yellowStar:string = "/assets/images/yellow-star.jpg";
  10.   midStar:string = "/assets/images/mid-star.jpg";
  11.   greyStar:string = "/assets/images/grey-star.jpg";
  12.  
  13.   subscriptionGetRating;
  14.  
  15.   constructor(private userService:UserService, public ratingService:RatingService) { }
  16.  
  17.   ngOnInit(): void {
  18.  
  19.     this.subscriptionGetRating = this.ratingService.getRating(this.userId).subscribe({
  20.      
  21.       next:data => {
  22.      
  23.         this.count = data['count'];
  24.         this.rating = data['rating'] / this.count;
  25.  
  26.         if(this.count == 0){
  27.  
  28.           this.rating = 0;
  29.  
  30.           for(let k=0;k<=4;k++){
  31.             this.ratingArray[k] = this.greyStar;
  32.           }
  33.  
  34.         } else{
  35.  
  36.           let trunc = Math.trunc(this.rating);
  37.      
  38.           for(let j=0;j<trunc+1;j++){
  39.      
  40.             if(this.rating>j && this.rating<j+1){
  41.               this.ratingArray[j] = this.midStar;
  42.             }else {
  43.               if(j!=trunc)
  44.                 this.ratingArray[j] = this.yellowStar;
  45.             }
  46.            
  47.           }
  48.      
  49.           if (this.rating == Math.floor(this.rating)){
  50.             for(let k=trunc;k<=4;k++){
  51.               this.ratingArray[k] = this.greyStar;
  52.             }
  53.           } else {
  54.             for(let k=trunc+1;k<=4;k++){
  55.               this.ratingArray[k] = this.greyStar;
  56.             }
  57.           }
  58.  
  59.         }
  60.  
  61.       },
  62.  
  63.       error:error => {
  64.         console.error(error);
  65.       }
  66.      
  67.     });
  68.  
  69.   }
  70.  
  71.   ngOnDestroy(){
  72.     if(this.subscriptionGetRating)
  73.       this.subscriptionGetRating.unsubscribe();
  74.   }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment