SHARE
TWEET
Untitled
a guest
Apr 11th, 2018
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { AuthService } from '../../../services/auth.service';
- import { HttpClient } from '@angular/common/http';
- import { ToastrService } from 'ngx-toastr';
- import { ActivatedRoute, Router } from '@angular/router';
- import { Animal } from '../../../models/Animal';
- import { Subscription } from 'rxjs/Subscription';
- @Component({
- selector: 'app-edit-animais',
- templateUrl: './edit-animais.component.html',
- styleUrls: ['./edit-animais.component.scss']
- })
- export class EditAnimaisComponent implements OnInit {
- sub : Subscription;
- animalId: String = '';
- nomeAnimal: String = '';
- tipoAnimal: String = '';
- racaAnimal: String = '';
- generoAnimal: String = '';
- idadeAnimal: String = '';
- animal : Animal = new Animal();
- loading : boolean = false;
- public user = { id: '', email: '', name: '', role: '' };
- constructor(public authService: AuthService, private activeRoute: ActivatedRoute, private http: HttpClient, private toastr: ToastrService, private router: Router) { }
- ngOnInit() {
- this.user = this.authService.getUser();
- this.sub = this.activeRoute.params.subscribe(params => {
- this.animalId = params['id'];
- }),
- this.getAnimalDoResponsavel();
- console.log("this animal: ", this.animal);
- }
- getAnimalDoResponsavel() {
- console.log("animal do responsavel")
- this.loading = true;
- let userId = this.user.id;
- this.http.get<Animal>('/animais/'+this.animalId)
- .subscribe(
- response => {
- console.log("response: ",response)
- this.animal = response;
- console.log("this animal: ",this.animal)
- this.loading = false;
- //this.animais = response;
- },
- err => this.handleError(err)
- );
- }
- editAnimal() {
- console.log("Dentro de editAnimal");
- this.loading = true;
- let userId = this.user.id;
- this.http.post('/utilizadores/'+userId+'/animais', {
- nome: this.nomeAnimal,
- tipo: this.tipoAnimal,
- raca: this.racaAnimal,
- genero: this.generoAnimal,
- idade: this.idadeAnimal }).subscribe(
- (res: any) => {
- this.toastr.success('Animal registado com sucesso!');
- this.router.navigate(['/animais']);
- this.loading = false;
- },
- err => {
- this.toastr.error(err.error.message, 'Ocorreu um erro');
- this.loading = false;
- }
- );
- }
- 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.
