hirebike.page.ts 4.41 KB
Newer Older
Priyanka Upadhye's avatar
Priyanka Upadhye committed
1
2
3
4
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';

import { Geolocation } from '@ionic-native/geolocation/ngx';
import { RestService } from '../rest.service';
5
import { Observable, Subject } from 'rxjs';
Priyanka Upadhye's avatar
Priyanka Upadhye committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Storage } from '@ionic/storage';
import { ToastService } from '../services/toast.service';
import { Router } from '@angular/router';
declare var H: any;
@Component({
  selector: 'app-hirebike',
  templateUrl: './hirebike.page.html',
  styleUrls: ['./hirebike.page.scss'],
})
export class HirebikePage implements OnInit {

  private platform: any;

  reservedBike: any = {};
  bikeDetails: any = {};
22
23
  address = "sample";
  isBikeHired = false;
Priyanka Upadhye's avatar
Priyanka Upadhye committed
24
25
  noReservation = true;

26
27
  startRideSubject: Subject<any> = new Subject();
  gotReservedBikeSubject: Subject<any> = new Subject();
Priyanka Upadhye's avatar
Priyanka Upadhye committed
28
29
30
31
32
33
34

  constructor(private geolocation: Geolocation,
    public restService: RestService,
    public httpClient: HttpClient,
    private storage: Storage,
    private toastService: ToastService,
    private router: Router) {
35
36
37
      this.platform = new H.service.Platform({
        'apikey': 'tiVTgBnPbgV1spie5U2MSy-obhD9r2sGiOCbBzFY2_k'
      });
Priyanka Upadhye's avatar
Priyanka Upadhye committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  }

  ngOnInit() {
    this.getReservedBike();
  }

  ngAfterViewInit() {

  }

  getReservedBike() {
    this.storage.get('token').then((token) => {
      const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
      //call reserved bike api
      let reserveUrl = 'http://193.196.52.237:8081/active-rent';
      let bikeReservationStatusApi = this.httpClient.get(reserveUrl, { headers });
      bikeReservationStatusApi.subscribe((resp: any) => {
        console.log('Reserved Bike', resp);
        if (resp.data) {
          this.reservedBike = resp.data;
58
          this.isBikeHired = this.reservedBike.rented;
Priyanka Upadhye's avatar
Priyanka Upadhye committed
59
60
61
62
63
64
65
          //Call Bike Details api
          let bikeDetailsUrl = 'http://193.196.52.237:8081/bikes/' + this.reservedBike.bikeId;
          let bikeDetailsApi = this.httpClient.get(bikeDetailsUrl, { headers });
          bikeDetailsApi.subscribe((resp: any) => {
            console.log('Bike Details', resp);
            this.bikeDetails = resp.data;
            this.noReservation = false;
66
            this.reverseGeocode(this.platform, this.bikeDetails.lat, this.bikeDetails.lon);
67
68
69
            
            //pass reserved bike subject here map
            this.gotReservedBikeSubject.next(resp.data);
Priyanka Upadhye's avatar
Priyanka Upadhye committed
70
71
72
73
74
75
          }, (reservedBikeError) => console.log(reservedBikeError));
        }
      }, (bikeDetailsError) => console.log(bikeDetailsError));
    });
  }

76
  startTrip1() {
Priyanka Upadhye's avatar
Priyanka Upadhye committed
77
    this.storage.get('token').then((token) => {
Priyanka Upadhye's avatar
Priyanka Upadhye committed
78
      let url = 'http://193.196.52.237:8081/rent' + '?bikeId=' + this.bikeDetails.id;
Priyanka Upadhye's avatar
Priyanka Upadhye committed
79
      const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
Priyanka Upadhye's avatar
Priyanka Upadhye committed
80
      let bikeApi = this.httpClient.get(url, { headers });
Priyanka Upadhye's avatar
Priyanka Upadhye committed
81
      bikeApi.subscribe((resp) => {
Priyanka Upadhye's avatar
Priyanka Upadhye committed
82
83
        console.log('my data: ', resp);
        this.toastService.showToast("Trip Started");
84
        this.isBikeHired = true;
Priyanka Upadhye's avatar
Priyanka Upadhye committed
85
86
      }, (error) => {
        console.log(error)
87
        this.toastService.showToast("This is ongoing Trip")
Priyanka Upadhye's avatar
Priyanka Upadhye committed
88
      });
Priyanka Upadhye's avatar
Priyanka Upadhye committed
89
    });
Priyanka Upadhye's avatar
Priyanka Upadhye committed
90

Priyanka Upadhye's avatar
Priyanka Upadhye committed
91
92
  }

93
94
95
96
  startTrip() {
    this.isBikeHired = true;
    this.startRideSubject.next('some value');
  }
Priyanka Upadhye's avatar
Priyanka Upadhye committed
97

98
  CancelTrip() {
Priyanka Upadhye's avatar
Priyanka Upadhye committed
99
100
101
102
103
104
105
106
107
    this.storage.get('token').then((token) => {
      let url = 'http://193.196.52.237:8081/rent' + '?bikeId=' + this.bikeDetails.id;
      const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
      let bikeApi = this.httpClient.delete(url, { headers });
      bikeApi.subscribe((resp) => {
        console.log('my data: ', resp);
        this.toastService.showToast("Trip Ended!");
      }, (error) => {
        console.log(error)
Priyanka Upadhye's avatar
Priyanka Upadhye committed
108
        this.toastService.showToast("No Ongong Trip to End")
Priyanka Upadhye's avatar
Priyanka Upadhye committed
109
110
      });
    });
Priyanka Upadhye's avatar
Priyanka Upadhye committed
111
  }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  reverseGeocode(platform, lat, lng) {
    var prox = lat + ',' + lng + ',56';
    var geocoder = platform.getGeocodingService(),
      parameters = {
        prox: prox,
        mode: 'retrieveAddresses',
        maxresults: '1',
        gen: '9'
      };

    geocoder.reverseGeocode(parameters, result => {
      console.log(result);
      var streets = result.Response.View[0].Result[0].Location.Address.Street;
      var houseNumber = result.Response.View[0].Result[0].Location.Address.HouseNumber;
      var zipcode = result.Response.View[0].Result[0].Location.Address.PostalCode;
127

128
      this.address = streets;
129

Priyanka Upadhye's avatar
Priyanka Upadhye committed
130

131
132
133
134
    }, (error) => {
      alert(error);
    });
  }
Priyanka Upadhye's avatar
Priyanka Upadhye committed
135
}