-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Transaction.js
211 lines (194 loc) · 6.76 KB
/
Transaction.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import lodashHas from 'lodash/has';
import ONYXKEYS from '../../ONYXKEYS';
import * as CollectionUtils from '../CollectionUtils';
import * as API from '../API';
let recentWaypoints = [];
Onyx.connect({
key: ONYXKEYS.NVP_RECENT_WAYPOINTS,
callback: (val) => (recentWaypoints = val || []),
});
const allTransactions = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
callback: (transaction, key) => {
if (!key || !transaction) {
return;
}
const transactionID = CollectionUtils.extractCollectionItemID(key);
allTransactions[transactionID] = transaction;
},
});
/**
* @param {String} transactionID
*/
function createInitialWaypoints(transactionID) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
waypoint0: {},
waypoint1: {},
},
},
});
}
/**
* Add a stop to the transaction
*
* @param {String} transactionID
* @param {Number} newLastIndex
*/
function addStop(transactionID) {
const transaction = lodashGet(allTransactions, transactionID, {});
const existingWaypoints = lodashGet(transaction, 'comment.waypoints', {});
const newLastIndex = _.size(existingWaypoints);
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
[`waypoint${newLastIndex}`]: {},
},
},
// Clear the existing route so that we don't show an old route
routes: {
route0: {
geometry: {
coordinates: null,
},
},
},
});
}
/**
* Saves the selected waypoint to the transaction
* @param {String} transactionID
* @param {String} index
* @param {Object} waypoint
*/
function saveWaypoint(transactionID, index, waypoint) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
[`waypoint${index}`]: waypoint,
},
},
// Empty out errors when we're saving a new waypoint as this indicates the user is updating their input
errorFields: {
route: null,
},
// Clear the existing route so that we don't show an old route
routes: {
route0: {
geometry: {
coordinates: null,
},
},
},
});
// You can save offline waypoints without verifying the address (we will geocode it on the backend)
// We're going to prevent saving those addresses in the recent waypoints though since they could be invalid addresses
// However, in the backend once we verify the address, we will save the waypoint in the recent waypoints NVP
if (!lodashHas(waypoint, 'lat') || !lodashHas(waypoint, 'lng')) {
return;
}
const recentWaypointAlreadyExists = _.find(recentWaypoints, (recentWaypoint) => recentWaypoint.address === waypoint.address);
if (!recentWaypointAlreadyExists) {
const clonedWaypoints = _.clone(recentWaypoints);
clonedWaypoints.unshift(waypoint);
Onyx.merge(ONYXKEYS.NVP_RECENT_WAYPOINTS, clonedWaypoints.slice(0, 5));
}
}
function removeWaypoint(transactionID, currentIndex) {
// Index comes from the route params and is a string
const index = Number(currentIndex);
const transaction = lodashGet(allTransactions, transactionID, {});
const existingWaypoints = lodashGet(transaction, 'comment.waypoints', {});
const totalWaypoints = _.size(existingWaypoints);
// Prevents removing the starting or ending waypoint but clear the stored address only if there are only two waypoints
if (totalWaypoints === 2 && (index === 0 || index === totalWaypoints - 1)) {
saveWaypoint(transactionID, index, null);
return;
}
const waypointValues = _.values(existingWaypoints);
waypointValues.splice(index, 1);
const reIndexedWaypoints = {};
waypointValues.forEach((waypoint, idx) => {
reIndexedWaypoints[`waypoint${idx}`] = waypoint;
});
// Onyx.merge won't remove the null nested object values, this is a workaround
// to remove nested keys while also preserving other object keys
// Doing a deep clone of the transaction to avoid mutating the original object and running into a cache issue when using Onyx.set
const newTransaction = {
...transaction,
comment: {
...transaction.comment,
waypoints: reIndexedWaypoints,
},
// Clear the existing route so that we don't show an old route
routes: {
route0: {
geometry: {
coordinates: null,
},
},
},
};
Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, newTransaction);
}
/**
* Gets the route for a set of waypoints
* Used so we can generate a map view of the provided waypoints
* @param {String} transactionID
* @param {Object} waypoints
*/
function getRoute(transactionID, waypoints) {
API.read(
'GetRoute',
{
transactionID,
waypoints: JSON.stringify(waypoints),
},
{
optimisticData: [
{
// Clears any potentially stale error messages from fetching the route
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
isLoading: true,
},
errorFields: {
route: null,
},
},
},
],
// The route and failure are sent back via pusher in the BE, we are just clearing the loading state here
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
isLoading: false,
},
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
isLoading: false,
},
},
},
],
},
);
}
export {addStop, createInitialWaypoints, saveWaypoint, removeWaypoint, getRoute};