SHARE
TWEET

Untitled

a guest Apr 14th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { Animal } from '../../models/Animal';
  4. import { ToastrService } from 'ngx-toastr';
  5. import { BsModalService } from 'ngx-bootstrap/modal/bs-modal.service';
  6. import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
  7. import { AuthService } from '../../services/auth.service';
  8. import { DeleteAnimalComponent } from '../../dialogs/delete-animal/delete-animal.component';
  9. import { ActivatedRoute, Router } from '@angular/router';
  10.  
  11.  
  12. @Component({
  13.     selector: 'app-animais',
  14.     templateUrl: 'animais.component.html'
  15. })
  16. export class AnimaisComponent implements OnInit {
  17.     bsModalRef    : BsModalRef;
  18.     animais : Array<Animal> = [];
  19.     search        : string = "";
  20.     loading       : boolean = false;
  21.     public user = { id: '', email: '', name: '', role: '' };
  22.  
  23.     constructor(
  24.         private http: HttpClient,
  25.         private toastr: ToastrService,
  26.         private modalService: BsModalService,
  27.         public authService: AuthService,
  28.         public router: Router
  29.     ) {}
  30.    
  31.     ngOnInit() {
  32.         this.user = this.authService.getUser();
  33.         console.log("role: ",this.user.role);
  34.         if(this.user.role==="responsavelanimais"){
  35.             console.log("responsavelanimais")
  36.             this.getAnimaisDoResponsavel();
  37.         }else if(this.user.role==="admin"){
  38.             console.log("todos os animais")
  39.             this.getAllAnimais();
  40.         }  
  41.     }
  42.    
  43.     getAnimaisDoResponsavel() {
  44.         console.log("animais do responsavel")
  45.         this.loading = true;
  46.         let userId = this.user.id;
  47.         this.http.get<Animal[]>('/utilizadores/'+userId+'/animais')
  48.         .subscribe(
  49.             response => {
  50.                 this.loading = false;
  51.                 this.animais = response;
  52.             },
  53.             err => this.handleError(err)
  54.         );
  55.     }
  56.  
  57.     getAllAnimais(){
  58.         console.log("todos os animais")
  59.         this.loading = true;
  60.         this.http.get<Animal[]>('/animais')
  61.         .subscribe(
  62.             response => {
  63.                 this.loading = false;
  64.                 this.animais = response;
  65.             },
  66.             err => this.handleError(err)
  67.         );
  68.     }
  69.  
  70.  
  71.     deleteAnimal(idAnimal: String){
  72.         console.log("Animal ID 2: ", idAnimal);
  73.         this.bsModalRef = this.setupModal(idAnimal);
  74.     }
  75.  
  76.     private setupModal(idAnimal: String) : BsModalRef {
  77.         console.log("Animal ID 2: ", idAnimal);
  78.         let ref = this.modalService.show(DeleteAnimalComponent, {class: 'modal-lg'});
  79.         ref.content.animalId = idAnimal;
  80.         console.log("REF: ", ref);
  81.         return ref;
  82.     }
  83.  
  84.  
  85.     private handleError(err) {
  86.         if (this.loading) {
  87.             this.loading = false;
  88.         }
  89.         this.toastr.error(err.error.message, 'Erro');
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top