import { DashboardService } from './../dashboard/dashboard.service'; import { Component, OnInit } from '@angular/core'; import { LocationService } from './location.service'; @Component({ selector: 'app-location', templateUrl: './location.component.html', styleUrls: ['./location.component.css'] }) export class LocationComponent implements OnInit { locations: any; cantlocations: number; existError = false; textError = ''; showIndex = -1; showIndex1 = -1; constructor( private locationService: LocationService, private dashboardService: DashboardService, ) { } ngOnInit() { this.dashboardService.updateMenuPosition(8); this.fetchLocations(); } fetchLocations() { this.locationService.getLocations() .subscribe(data => { const aux: any = data; if (aux.resp) { this.locations = aux.locations; this.cantlocations = this.locations.length; } }); } onAdd(text: string) { this.showIndex = -1; this.showIndex1 = -1; if (text === '') { this.existError = true; this.textError = 'El nombre no puede estar en blanco'; } else { this.locationService.checkLocation(text) .subscribe(data => { const aux: any = data; if (!aux.resp) { this.existError = false; this.locationService.addLocation(text) .subscribe(data1 => { const aux1: any = data1; if (aux1.resp) { this.fetchLocations(); this.existError = false; } }); } else { this.existError = true; this.textError = 'Existe un lugar con ese nombre'; } }); } } updateIndex(ind) { this.showIndex1 = -1; if (this.showIndex === ind) { this.showIndex = -1; } else { this.showIndex = ind; } } updateIndex1(ind) { this.showIndex = -1; if (this.showIndex1 === ind) { this.showIndex1 = -1; } else { this.showIndex1 = ind; } } updateLocation(locId: string, text: string) { this.showIndex = -1; if (text === '') { this.existError = true; this.textError = 'El nombre no puede estar en blanco'; } else { this.locationService.editLocation(locId, text) .subscribe(data => { const aux: any = data; if (!aux.resp) { this.existError = true; this.textError = 'Existe un lugar con ese nombre'; } else { this.fetchLocations(); this.existError = false; } }); } } onDelete(locid: string) { this.showIndex1 = -1; this.locationService.delLocation(locid) .subscribe(data => { const aux: any = data; if (aux.resp) { this.fetchLocations(); } }); } }