SHARE
TWEET

Untitled

a guest Apr 14th, 2018 62 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 { AuthService } from '../../services/auth.service';
  6. import { DeleteAnimalComponent } from '../../dialogs/delete-animal/delete-animal.component';
  7. import { ActivatedRoute, Router } from '@angular/router';
  8. import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';
  9.  
  10.  
  11. @Component({
  12.     selector: 'app-animais',
  13.     templateUrl: 'animais.component.html'
  14. })
  15. export class AnimaisComponent implements OnInit {
  16.  
  17.     animais : Array<Animal> = [];
  18.     search        : string = "";
  19.     loading       : boolean = false;
  20.     public user = { id: '', email: '', name: '', role: '' };
  21.     public modalRef: BsModalRef;
  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.  
  77.     public deleteAnimal(idAnimal: String) {
  78.         console.log("idAnimal: ", idAnimal);
  79.         this.modalRef = this.modalService.show(DeleteAnimalComponent);
  80.         this.modalRef.content.animalId = 'My Modal Content';
  81.         return this.modalRef;
  82.      }
  83.      
  84.  
  85. /*
  86.     private setupModal(idAnimal: String) : BsModalRef {
  87.         console.log("Animal ID 2: ", idAnimal);
  88.         let ref = this.modalService.show(DeleteAnimalComponent, {class: 'modal-lg'});
  89.         ref.content.animalId = idAnimal;
  90.         console.log("REF: ", ref);
  91.         return ref;
  92.     }
  93. */
  94.  
  95.     private handleError(err) {
  96.         if (this.loading) {
  97.             this.loading = false;
  98.         }
  99.         this.toastr.error(err.error.message, 'Erro');
  100.     }
  101. }
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