From ae47c8eb8fbaf6303745b926aaa4e6a0398052b6 Mon Sep 17 00:00:00 2001 From: kj1352 Date: Wed, 29 Jan 2020 11:53:02 +0530 Subject: [PATCH] fetch malls by location --- src/app/malls/malls.page.ts | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/app/malls/malls.page.ts b/src/app/malls/malls.page.ts index eadfbdd..50291de 100644 --- a/src/app/malls/malls.page.ts +++ b/src/app/malls/malls.page.ts @@ -1,6 +1,7 @@ import { Component, OnInit, ViewChild,ElementRef } from '@angular/core'; import { MallService } from '../services/mall.service'; import { Router } from '@angular/router'; +import { Geolocation } from '@ionic-native/geolocation/ngx'; @Component({ selector: 'app-malls', @@ -12,6 +13,7 @@ export class MallsPage implements OnInit { selected_tab: string = 'you'; tempMalls: any = []; allMalls: any = []; + nearByMalls: any = []; show_sort_popup: boolean = false; showSearchBar: boolean = false; selected_sort: string = null; @@ -19,18 +21,35 @@ export class MallsPage implements OnInit { constructor( private mallService: MallService, - private router: Router + private router: Router, + private geolocation: Geolocation ) { } ngOnInit() { - this.storeAllMalls(); + this.fetchMallsByLocation(); + this.storeAllMalls(); } + fetchMallsByLocation() { + this.geolocation.getCurrentPosition().then((position) => { + this.mallService.mallsByLocation(position.coords.latitude, position.coords.longitude) + .then((response) => { + this.nearByMalls = response; + this.tempMalls = response; + }) + }); + } + toggleSearchBar() { - this.showSearchBar = !this.showSearchBar; - setTimeout(()=>{ // this will make the execution after the above boolean has changed - this.searchElement.nativeElement.focus(); - }, 1); + if (this.searchTerm.length > 0) { + this.searchTerm = ''; + } else { + this.showSearchBar = !this.showSearchBar; + this.fetchMallsByLocation(); + setTimeout(()=>{ // this will make the execution after the above boolean has changed + this.searchElement.nativeElement.focus(); + }, 1000); + } } searchAllMalls() { @@ -39,7 +58,7 @@ export class MallsPage implements OnInit { return mallData.mall.mall_name.toLowerCase().includes(this.searchTerm.toLowerCase()); }); } else { - this.tempMalls = JSON.parse(JSON.stringify(this.allMalls)); + this.tempMalls = JSON.parse(JSON.stringify(this.nearByMalls)); } } @@ -47,7 +66,6 @@ export class MallsPage implements OnInit { this.mallService.allMalls().then((response) => { localStorage.allMalls = JSON.stringify(response); this.allMalls = JSON.parse(localStorage.allMalls); - this.tempMalls = JSON.parse(localStorage.allMalls); }, (error) => { console.log(error) });