SHARE
TWEET
Untitled
a guest
Apr 14th, 2018
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- import { Animal } from '../../models/Animal';
- import { ToastrService } from 'ngx-toastr';
- import { BsModalService } from 'ngx-bootstrap/modal/bs-modal.service';
- import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
- import { AuthService } from '../../services/auth.service';
- import { DeleteAnimalComponent } from '../../dialogs/delete-animal/delete-animal.component';
- import { ActivatedRoute, Router } from '@angular/router';
- @Component({
- selector: 'app-animais',
- templateUrl: 'animais.component.html'
- })
- export class AnimaisComponent implements OnInit {
- bsModalRef : BsModalRef;
- animais : Array<Animal> = [];
- search : string = "";
- loading : boolean = false;
- public user = { id: '', email: '', name: '', role: '' };
- constructor(
- private http: HttpClient,
- private toastr: ToastrService,
- private modalService: BsModalService,
- public authService: AuthService,
- public router: Router
- ) {}
- ngOnInit() {
- this.user = this.authService.getUser();
- console.log("role: ",this.user.role);
- if(this.user.role==="responsavelanimais"){
- console.log("responsavelanimais")
- this.getAnimaisDoResponsavel();
- }else if(this.user.role==="admin"){
- console.log("todos os animais")
- this.getAllAnimais();
- }
- }
- getAnimaisDoResponsavel() {
- console.log("animais do responsavel")
- this.loading = true;
- let userId = this.user.id;
- this.http.get<Animal[]>('/utilizadores/'+userId+'/animais')
- .subscribe(
- response => {
- this.loading = false;
- this.animais = response;
- },
- err => this.handleError(err)
- );
- }
- getAllAnimais(){
- console.log("todos os animais")
- this.loading = true;
- this.http.get<Animal[]>('/animais')
- .subscribe(
- response => {
- this.loading = false;
- this.animais = response;
- },
- err => this.handleError(err)
- );
- }
- deleteAnimal(idAnimal: String){
- console.log("Animal ID 2: ", idAnimal);
- this.bsModalRef = this.setupModal(idAnimal);
- }
- private setupModal(idAnimal: String) : BsModalRef {
- console.log("Animal ID 2: ", idAnimal);
- let ref = this.modalService.show(DeleteAnimalComponent, {class: 'modal-lg'});
- ref.content.animalId = idAnimal;
- console.log("REF: ", ref);
- return ref;
- }
- private handleError(err) {
- if (this.loading) {
- this.loading = false;
- }
- this.toastr.error(err.error.message, 'Erro');
- }
- }
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.
