Skip to content

Commit 698cf84

Browse files
authored
Merge pull request #170 from Iterable/updateCart-addition
updateCart and trackPurchase additions
2 parents 82a6637 + f7f9e1f commit 698cf84

File tree

8 files changed

+45
-4
lines changed

8 files changed

+45
-4
lines changed

android/src/main/java/com/iterable/reactnative/RNIterableAPIModule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ public void trackEvent(String name, ReadableMap dataFields) {
146146
}
147147
}
148148

149+
@ReactMethod
150+
public void updateCart(ReadableArray items) {
151+
IterableLogger.v(TAG, "UpdateCart API");
152+
153+
IterableApi.getInstance().updateCart(Serialization.commerceItemsFromReadableArray(items));
154+
}
155+
149156
@ReactMethod
150157
public void trackPurchase(Double total, ReadableArray items, ReadableMap dataFields) {
151158
IterableLogger.v(TAG, "TrackPurchase API");

android/src/main/java/com/iterable/reactnative/Serialization.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ static List<CommerceItem> commerceItemsFromReadableArray(ReadableArray array) {
8181
}
8282

8383
static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException {
84-
8584
String[] categories = null;
8685
JSONArray categoriesArray = itemMap.optJSONArray("categories");
86+
8787
if (categoriesArray != null) {
8888
for (int i = 0; i < categoriesArray.length(); i++) {
8989
if (categories == null) {
@@ -92,6 +92,7 @@ static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException
9292
categories[i] = categoriesArray.getString(i);
9393
}
9494
}
95+
9596
return new CommerceItem(itemMap.getString("id"),
9697
itemMap.getString("name"),
9798
itemMap.getDouble("price"),
@@ -100,7 +101,8 @@ static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException
100101
itemMap.optString("description", null),
101102
itemMap.optString("url", null),
102103
itemMap.optString("imageUrl", null),
103-
categories
104+
categories,
105+
itemMap.optJSONObject("dataFields")
104106
);
105107
}
106108

ios/RNIterableAPI/RNIterableAPI.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ @interface RCT_EXTERN_REMAP_MODULE(RNIterableAPI, ReactIterableAPI, NSObject)
5050
appAlreadyRunning: (BOOL) appAlreadyRunning
5151
dataFields: (NSDictionary *) dataFields)
5252

53+
RCT_EXTERN_METHOD(updateCart: (NSArray *) items)
54+
5355
RCT_EXTERN_METHOD(trackPurchase: (nonnull NSNumber *) total
5456
items: (NSArray *) items
5557
dataFields: (NSDictionary *) dataFields)

ios/RNIterableAPI/ReactIterableAPI.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ class ReactIterableAPI: RCTEventEmitter {
166166
appAlreadyRunning: appAlreadyRunning,
167167
dataFields: dataFields)
168168
}
169+
170+
@objc(updateCart:)
171+
func updateCart(items: [[AnyHashable: Any]]) {
172+
ITBInfo()
173+
174+
IterableAPI.updateCart(items: items.compactMap(CommerceItem.from(dict:)))
175+
}
169176

170177
@objc(trackPurchase:items:dataFields:)
171178
func trackPurchase(total: NSNumber,

ios/RNIterableAPI/Serialization.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extension CommerceItem {
9090
let url = dict["url"] as? String
9191
let imageUrl = dict["imageUrl"] as? String
9292
let categories = dict["categories"] as? [String]
93+
let dataFields = dict["dataFields"] as? [AnyHashable: Any]
9394

9495
return CommerceItem(id: id,
9596
name: name,
@@ -99,7 +100,8 @@ extension CommerceItem {
99100
description: description,
100101
url: url,
101102
imageUrl: imageUrl,
102-
categories: categories)
103+
categories: categories,
104+
dataFields: dataFields)
103105
}
104106
}
105107

ts/Iterable.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ class IterableCommerceItem {
170170
url?: string
171171
imageUrl?: string
172172
categories?: Array<string>
173+
dataFields?: any
173174

174-
constructor(id: string, name: string, price: number, quantity: number, sku?: string, description?: string, url?: string, imageUrl?: string, categories?: Array<string>) {
175+
constructor(id: string, name: string, price: number, quantity: number, sku?: string, description?: string, url?: string, imageUrl?: string, categories?: Array<string>, dataFields?: any | undefined) {
175176
this.id = id
176177
this.name = name
177178
this.price = price
@@ -181,6 +182,7 @@ class IterableCommerceItem {
181182
this.url = url
182183
this.imageUrl = imageUrl
183184
this.categories = categories
185+
this.dataFields = dataFields
184186
}
185187
}
186188

@@ -302,6 +304,15 @@ class Iterable {
302304
RNIterableAPI.trackPushOpenWithCampaignId(campaignId, templateId, messageId, appAlreadyRunning, dataFields)
303305
}
304306

307+
/**
308+
*
309+
* @param {Array<IterableCommerceItem>} items
310+
*/
311+
static updateCart(items: Array<IterableCommerceItem>) {
312+
console.log("updateCart")
313+
RNIterableAPI.updateCart(items)
314+
}
315+
305316
/**
306317
*
307318
* @param {number} total

ts/__mocks__/MockRNIterableAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export class MockRNIterableAPI {
3131

3232
static trackPushOpenWithCampaignId = jest.fn()
3333

34+
static updateCart = jest.fn()
35+
3436
static trackPurchase = jest.fn()
3537

3638
static trackInAppOpen = jest.fn()

ts/__tests__/Iterable.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ test("trackPushOpenWithCampaignId", () => {
5757
)
5858
})
5959

60+
test("updateCart", () => {
61+
Iterable.updateCart([new IterableCommerceItem("id1", "Boba Tea", 18, 26)])
62+
63+
expect(MockRNIterableAPI.updateCart).toBeCalledWith(
64+
[new IterableCommerceItem("id1", "Boba Tea", 18, 26)]
65+
)
66+
})
67+
6068
test("trackPurchase", () => {
6169
Iterable.trackPurchase(
6270
10,

0 commit comments

Comments
 (0)