SHARE
TWEET

Untitled

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