-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathHomeHandlers.js
86 lines (78 loc) · 2.08 KB
/
HomeHandlers.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
/**
* Copyright (c) 2017-present, Justin Nguyen.
* All rights reserved.
*
* @author [email protected]
*
* @flow
* @format
*/
"use strict"
import update from "immutability-helper"
import {
SET_PICK_UP_LOCATION,
SET_DROP_OFF_LOCATION,
SET_FARE_STRUCTURE,
BOOK_TAXI_REQUEST
} from "./HomeActions"
import { getRegionFromCoordinates, taxiTypes } from "../../../global"
export const actionHandlers = {
SET_PICK_UP_LOCATION: handleSetPickupLocation,
SET_DROP_OFF_LOCATION: handleSetDropLocation,
SET_FARE_STRUCTURE: handleSetFareStructure,
BOOK_TAXI_REQUEST: handleBookTaxiRequest
}
function handleSetPickupLocation(state, action) {
const pickupLocation = action.payload
const dropoffLocation = state.dropoffLocation
// As we will not integrate with any server so ,
// just random some drivers when users set pickup
const randomDriverCount = Math.round(Math.random() * 20)
const drivers = []
for (let i = 0; i < randomDriverCount; i++) {
const randomTaxiType = Math.round(Math.random() * 1000) % taxiTypes.length
drivers.push({
id: i,
taxiType: taxiTypes[randomTaxiType].type,
latitude: pickupLocation.latitude + (Math.random() - Math.random()) / 50,
longitude: pickupLocation.longitude + (Math.random() - Math.random()) / 50
})
}
return update(state, {
pickupLocation: {
$set: pickupLocation
},
mapRegion: {
$set: getRegionFromCoordinates([pickupLocation, dropoffLocation])
},
drivers: {
$set: drivers
}
})
}
function handleSetDropLocation(state, action) {
const pickupLocation = state.pickupLocation
const dropoffLocation = action.payload
return update(state, {
dropoffLocation: {
$set: dropoffLocation
},
mapRegion: {
$set: getRegionFromCoordinates([pickupLocation, dropoffLocation])
}
})
}
function handleSetFareStructure(state, action) {
return update(state, {
fareStructure: {
$set: action.payload
}
})
}
function handleBookTaxiRequest(state, action) {
return update(state, {
bookingRecord: {
$set: action.payload
}
})
}