-
Notifications
You must be signed in to change notification settings - Fork 18
/
HomeActions.js
113 lines (101 loc) · 2.76 KB
/
HomeActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Copyright (c) 2017-present, Justin Nguyen.
* All rights reserved.
*
* @author tuan3.nguyen@gmail.com
*
* @flow
* @format
*/
"use strict"
import { urls } from "../../../global/urls"
export const SET_PICK_UP_LOCATION = "SET_PICK_UP_LOCATION"
export const SET_DROP_OFF_LOCATION = "SET_DROP_OFF_LOCATION"
export const SET_FARE_STRUCTURE = "SET_FARE_STRUCTURE"
export const BOOK_TAXI_REQUEST = "BOOK_TAXI_REQUEST"
function setPickupLocationAction(location) {
return {
type: SET_PICK_UP_LOCATION,
payload: location
}
}
function setDropLocationAction(location) {
return {
type: SET_DROP_OFF_LOCATION,
payload: location
}
}
function setFareStructureAction(fare) {
return {
type: SET_FARE_STRUCTURE,
payload: fare
}
}
function bookTaxiRequestAction(bookingInfo) {
return {
type: BOOK_TAXI_REQUEST,
payload: bookingInfo
}
}
function calculateFare() {
return (dispatch, getState) => {
const homeState = getState().home
const { pickupLocation, dropoffLocation } = homeState
if (pickupLocation && dropoffLocation) {
const url = urls.distanceMatrix(
pickupLocation.latitude,
pickupLocation.longitude,
dropoffLocation.latitude,
dropoffLocation.longitude
)
fetch(url)
.then(response => response.json())
.then(json => json.rows[0].elements[0])
.then(element => dispatch(setFareStructureAction(element)))
.catch(error => console.log(error))
}
}
}
export function setDropLocation(location) {
return dispatch => {
dispatch(setDropLocationAction(location))
dispatch(calculateFare())
}
}
export function setPickupLocation(location) {
return dispatch => {
dispatch(setPickupLocationAction(location))
dispatch(calculateFare())
}
}
// TODO use redux-sage may help to avoid such dispatch chain
export function getCurrentLocation() {
// Fallback coords at eTown2, Cong Hoa street.
const fallbackCoords = { latitude: 10.8018791, longitude: 106.6391644 }
const geoOptions = { timeout: 20000, enableHighAccuracy: false, maximumAge: 1000 }
return (dispatch, getState) => {
navigator.geolocation.getCurrentPosition(
position => dispatch(setPickupLocation(position.coords)),
error => dispatch(setPickupLocation(fallbackCoords)),
geoOptions
)
}
}
export function bookTaxi() {
// TODO API call.
return (dispatch, getState) => {
const state = getState()
dispatch(
bookTaxiRequestAction({
pickupLocation: state.home.pickupLocation,
dropoffLocation: state.home.dropoffLocation,
status: "pending",
taxiType: state.template.selectedTaxiType
})
)
}
}
export function cancelBookingTaxi() {
// TODO API call.
return dispatch => dispatch(bookTaxiRequestAction(null))
}