Project: Mall App Client: Maiora
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

malls.page.ts 5.2 KiB

5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { Component, OnInit, ViewChild,ElementRef } from '@angular/core';
  2. import { MallService } from '../services/mall.service';
  3. import { ToastService } from '../services/toast.service';
  4. import { Router } from '@angular/router';
  5. @Component({
  6. selector: 'app-malls',
  7. templateUrl: './malls.page.html',
  8. styleUrls: ['./malls.page.scss'],
  9. })
  10. export class MallsPage implements OnInit {
  11. @ViewChild('searchbar', null) searchElement: ElementRef;
  12. selected_tab: string = 'you';
  13. tempMalls: any = [];
  14. allMalls: any = [];
  15. show_sort_popup: boolean = false;
  16. showSearchBar: boolean = false;
  17. selected_sort: string = null;
  18. searchTerm: string = '';
  19. selectedFoodType: string = '';
  20. constructor(
  21. private mallService: MallService,
  22. private router: Router,
  23. private toastService: ToastService
  24. ) { }
  25. getMallsByFoodType(type: string) {
  26. this.selectedFoodType = type;
  27. console.log(this.tempMalls);
  28. this.mallService.mallsByTypes(type).then((mallsByTypes: Array<any>) => {
  29. let malls: any = [];
  30. if (mallsByTypes.length > 0) {
  31. mallsByTypes.forEach((mallLocations) => {
  32. mallLocations.location.forEach((mall) => {
  33. malls.push(mall);
  34. });
  35. });
  36. this.allMalls = malls;
  37. this.tempMalls = malls;
  38. } else {
  39. this.toastService.presentToast("No Malls of the type " + this.selectedFoodType);
  40. this.allMalls = [];
  41. this.tempMalls = [];
  42. }
  43. }, () => {
  44. this.toastService.presentToast("Failed to get malls of this food type", "warning");
  45. });
  46. }
  47. ngOnInit() {
  48. this.getMallsByLocation();
  49. }
  50. toggleMallBookmark(index: number) {
  51. this.tempMalls[index].mall.is_bookmarked = !this.tempMalls[index].mall.is_bookmarked;
  52. console.log(this.tempMalls[index].mall);
  53. this.mallService.updateMallData(this.tempMalls[index]);
  54. }
  55. doRefresh(e: any) {
  56. this.getMallsByLocation();
  57. e.target.complete();
  58. }
  59. toggleSearchBar() {
  60. if (this.searchTerm.length > 0) {
  61. this.searchTerm = '';
  62. this.tempMalls = JSON.parse(JSON.stringify(this.allMalls));
  63. } else {
  64. this.showSearchBar = !this.showSearchBar;
  65. setTimeout(()=>{ // this will make the execution after the above boolean has changed
  66. this.searchElement.nativeElement.focus();
  67. }, 1);
  68. }
  69. }
  70. searchAllMalls() {
  71. if (this.searchTerm.trim().length > 0) {
  72. this.tempMalls = this.allMalls.filter((mallData: any) => {
  73. return mallData.mall.mall_name.toLowerCase().includes(this.searchTerm.toLowerCase());
  74. });
  75. } else {
  76. this.tempMalls = JSON.parse(JSON.stringify(this.allMalls));
  77. }
  78. }
  79. shareMallDetails(mallData) {
  80. if (window.navigator && window.navigator['share']) {
  81. const shareData = {
  82. title: mallData.mall.mall_name,
  83. text: mallData.mall.description,
  84. url: 'https://maps.google.com/?q=' + mallData.latitude + ',' + mallData.longitude
  85. }
  86. window.navigator['share'](shareData);
  87. }
  88. }
  89. getMallsByLocation() {
  90. if (navigator.geolocation) {
  91. this.toastService.presentToast("Getting malls based on your location...", "dark");
  92. navigator.geolocation.getCurrentPosition((position) => {
  93. this.mallService.mallsByLocation(position.coords.latitude, position.coords.longitude).then((response: any) => {
  94. if (response.length > 0) {
  95. this.allMalls = response;
  96. this.tempMalls = response;
  97. } else {
  98. this.toastService.presentToast("No malls near you your location, Getting all the malls...", "warning");
  99. this.getAllMalls();
  100. }
  101. }, () => {
  102. this.toastService.presentToast("Failed to fetch malls for your location", "danger");
  103. });
  104. });
  105. } else {
  106. this.getAllMalls();
  107. }
  108. }
  109. getAllMalls() {
  110. this.mallService.allMalls().then((response) => {
  111. this.allMalls = response;
  112. this.tempMalls = response;
  113. }, (error) => {
  114. console.log(error);
  115. });
  116. }
  117. showMallDetails(mall: any) {
  118. this.router.navigate(['/mall-details', { mall: JSON.stringify(mall) }]);
  119. }
  120. sortBy(type: string) {
  121. this.selected_sort = type;
  122. switch(this.selected_sort) {
  123. case 'name': this.tempMalls.sort(function(a: any, b: any){
  124. if(a.mall.mall_name < b.mall.mall_name) { return -1; }
  125. if(a.mall.mall_name > b.mall.mall_name) { return 1; }
  126. return 0;
  127. });
  128. break;
  129. case 'rating': this.tempMalls.sort(function(a: any, b: any){
  130. if(a.mall.rating < b.mall.rating) { return -1; }
  131. if(a.mall.rating > b.mall.rating) { return 1; }
  132. return 0;
  133. }).reverse();
  134. break;
  135. }
  136. }
  137. }