SHARE
TWEET

Untitled

a guest Apr 11th, 2018 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { AuthService } from '../../../services/auth.service';
  3. import { HttpClient } from '@angular/common/http';
  4. import { ToastrService } from 'ngx-toastr';
  5. import { ActivatedRoute, Router } from '@angular/router';
  6. import { Animal } from '../../../models/Animal';
  7. import { Subscription } from 'rxjs/Subscription';
  8.  
  9.  
  10. @Component({
  11.   selector: 'app-edit-animais',
  12.   templateUrl: './edit-animais.component.html',
  13.   styleUrls: ['./edit-animais.component.scss']
  14. })
  15. export class EditAnimaisComponent implements OnInit {
  16.  
  17.   sub : Subscription;
  18.  
  19.   animalId: String = '';
  20.  
  21.   nomeAnimal: String = '';
  22.   tipoAnimal: String = '';
  23.   racaAnimal: String = '';
  24.   generoAnimal: String = '';
  25.   idadeAnimal: String = '';
  26.  
  27.   animal : Animal = new Animal();
  28.  
  29.  
  30.   loading       : boolean = false;
  31.  
  32.   public user = { id: '', email: '', name: '', role: '' };
  33.  
  34.  
  35.   constructor(public authService: AuthService, private activeRoute: ActivatedRoute, private http: HttpClient, private toastr: ToastrService, private router: Router) { }
  36.  
  37.   ngOnInit() {
  38.     this.user = this.authService.getUser();
  39.  
  40.     this.sub = this.activeRoute.params.subscribe(params => {
  41.       this.animalId = params['id'];
  42.     }),
  43.  
  44.     this.getAnimalDoResponsavel();
  45.    
  46.     console.log("this animal: ", this.animal);
  47.   }
  48.  
  49.  
  50.  
  51.   getAnimalDoResponsavel() {
  52.     console.log("animal do responsavel")
  53.     this.loading = true;
  54.     let userId = this.user.id;
  55.     this.http.get<Animal>('/animais/'+this.animalId)
  56.     .subscribe(
  57.         response => {
  58.             console.log("response: ",response)
  59.             this.animal = response;
  60.             console.log("this animal: ",this.animal)
  61.             this.loading = false;
  62.             //this.animais = response;
  63.         },
  64.         err => this.handleError(err)
  65.     );
  66.   }
  67.  
  68.   editAnimal() {
  69.    
  70.     console.log("Dentro de editAnimal");
  71.     this.loading = true;
  72.     let userId = this.user.id;
  73.     this.http.post('/utilizadores/'+userId+'/animais', {
  74.       nome: this.nomeAnimal,
  75.       tipo: this.tipoAnimal,
  76.       raca: this.racaAnimal,
  77.       genero: this.generoAnimal,
  78.       idade: this.idadeAnimal }).subscribe(
  79.         (res: any) => {
  80.                      
  81.             this.toastr.success('Animal registado com sucesso!');
  82.             this.router.navigate(['/animais']);
  83.             this.loading = false;
  84.         },
  85.         err => {
  86.             this.toastr.error(err.error.message, 'Ocorreu um erro');
  87.             this.loading = false;
  88.         }
  89.     );
  90.      
  91.  
  92.   }
  93.  
  94.   private handleError(err) {
  95.     if (this.loading) {
  96.         this.loading = false;
  97.     }
  98.     this.toastr.error(err.error.message, 'Erro');
  99.   }
  100.  
  101.  
  102.  
  103. }
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