Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import { ActivatedRoute, Params, Router } from '@angular/router';
  3. import {
  4.   ProviderControllerService,
  5.   ProviderModel
  6. } from '../../../back-end/back-end-classes';
  7. import { Subscription } from 'rxjs';
  8.  
  9. @Component({
  10.   selector: 'app-top-bar',
  11.   templateUrl: './top-bar.component.html',
  12.   styleUrls: ['./top-bar.component.scss']
  13. })
  14. export class TopBarComponent implements OnInit, OnDestroy {
  15.   options: ProviderModel[];
  16.   currentRouteParams: Subscription = Subscription.EMPTY;
  17.   isOpen: boolean;
  18.   accommodation: 'hotel' | 'apartment' = 'apartment';
  19.   transfer: 'rent' | 'public' = 'rent';
  20.   //Today's Date
  21.   defaultOutDate:Date = new Date();
  22.   //Add one week to inboundDate
  23.   defaultInDate:Date = new Date(new Date().setDate(new Date().getDate()+7));
  24.  
  25.   plannerOptions = {
  26.     budget: '1000',
  27.     originPlace: '',
  28.     destinationPlace: '',
  29.     outboundDate: this.defaultOutDate.toISOString().slice(0,10),
  30.     inboundDate: this.defaultInDate.toISOString().slice(0,10),
  31.     adults: 2,
  32.     children: 0
  33.   };
  34.  
  35.   constructor(
  36.     private router: Router,
  37.     private providerService: ProviderControllerService,
  38.     private activeRoute: ActivatedRoute
  39.   ) {}
  40.  
  41.   setOrigin = (origin: ProviderModel | string) => {
  42.     if (typeof origin === 'string') {
  43.       this.plannerOptions.originPlace = origin;
  44.     } else {
  45.       this.plannerOptions.originPlace = origin.iataCode;
  46.     }
  47.   };
  48.   setDestinationPlace = (destination: ProviderModel | string) => {
  49.     if (typeof destination === 'string') {
  50.       this.plannerOptions.destinationPlace = destination;
  51.     } else {
  52.       this.plannerOptions.destinationPlace = destination.iataCode;
  53.     }
  54.   };
  55.   setAccommodation = type => (this.accommodation = type);
  56.   setTransfer = type => (this.transfer = type);
  57.   clickSim(element, delay: number = 200) {
  58.     setTimeout(
  59.       () => element._element.nativeElement.children['0'].click(),
  60.       delay
  61.     );
  62.   }
  63.   getCities = async (): Promise<ProviderModel[]> => {
  64.     try {
  65.       return (await this.providerService.cities()).result;
  66.     } catch (e) {
  67.       console.log(e.message);
  68.     }
  69.   };
  70.  
  71.   async navigateToPlanner() {
  72.     await this.router.navigate(
  73.       [`/planner/${Object.values(this.plannerOptions).join('/')}`]);
  74.   }
  75.  
  76.   async ngOnInit() {
  77.     if (this.currentRouteParams !== Subscription.EMPTY){
  78.       this.currentRouteParams.unsubscribe();
  79.     }else{
  80.       this.currentRouteParams = this.activeRoute.params.subscribe(params => {
  81.         if (Object.keys(params).length !== 0){
  82.           this.plannerOptions.budget = params.budget;
  83.           this.plannerOptions.outboundDate = params.outboundDate;
  84.           this.plannerOptions.inboundDate = params.inboundDate;
  85.           this.plannerOptions.adults = params.adult;
  86.           this.plannerOptions.children = params.children;
  87.           this.setOrigin(params.originPlace);
  88.           this.setDestinationPlace(params.destinationPlace);
  89.         }
  90.       });
  91.     }
  92.    
  93.  
  94.     // plannerOptions = {
  95.     //   budget: this.activeRoute.snapshot.params.budget || '1000',
  96.     //   originPlace: '',
  97.     //   destinationPlace: '',
  98.     //   outboundDate:
  99.     //     this.activeRoute.snapshot.params.outboundDate || '2019-08-10',
  100.     //   inboundDate: this.activeRoute.snapshot.params.inboundDate || '2019-08-17',
  101.     //   adultsCount: this.activeRoute.snapshot.params.adults || 2,
  102.     //   childrenCount: this.activeRoute.snapshot.params.children || 0
  103.     // };
  104.  
  105.     this.getCities().then(result => (this.options = result));
  106.  
  107.     // if(Object.entries(this.activeRoute.snapshot.params).length){
  108.     //
  109.     // }
  110.   }
  111.  
  112.   ngOnDestroy() {
  113.     this.currentRouteParams.unsubscribe();
  114.   }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement