Skip to content

Commit

Permalink
Merge pull request #170 from Iterable/updateCart-addition
Browse files Browse the repository at this point in the history
updateCart and trackPurchase additions
  • Loading branch information
roninopf authored Aug 10, 2021
2 parents 82a6637 + f7f9e1f commit 698cf84
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public void trackEvent(String name, ReadableMap dataFields) {
}
}

@ReactMethod
public void updateCart(ReadableArray items) {
IterableLogger.v(TAG, "UpdateCart API");

IterableApi.getInstance().updateCart(Serialization.commerceItemsFromReadableArray(items));
}

@ReactMethod
public void trackPurchase(Double total, ReadableArray items, ReadableMap dataFields) {
IterableLogger.v(TAG, "TrackPurchase API");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ static List<CommerceItem> commerceItemsFromReadableArray(ReadableArray array) {
}

static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException {

String[] categories = null;
JSONArray categoriesArray = itemMap.optJSONArray("categories");

if (categoriesArray != null) {
for (int i = 0; i < categoriesArray.length(); i++) {
if (categories == null) {
Expand All @@ -92,6 +92,7 @@ static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException
categories[i] = categoriesArray.getString(i);
}
}

return new CommerceItem(itemMap.getString("id"),
itemMap.getString("name"),
itemMap.getDouble("price"),
Expand All @@ -100,7 +101,8 @@ static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException
itemMap.optString("description", null),
itemMap.optString("url", null),
itemMap.optString("imageUrl", null),
categories
categories,
itemMap.optJSONObject("dataFields")
);
}

Expand Down
2 changes: 2 additions & 0 deletions ios/RNIterableAPI/RNIterableAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ @interface RCT_EXTERN_REMAP_MODULE(RNIterableAPI, ReactIterableAPI, NSObject)
appAlreadyRunning: (BOOL) appAlreadyRunning
dataFields: (NSDictionary *) dataFields)

RCT_EXTERN_METHOD(updateCart: (NSArray *) items)

RCT_EXTERN_METHOD(trackPurchase: (nonnull NSNumber *) total
items: (NSArray *) items
dataFields: (NSDictionary *) dataFields)
Expand Down
7 changes: 7 additions & 0 deletions ios/RNIterableAPI/ReactIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ class ReactIterableAPI: RCTEventEmitter {
appAlreadyRunning: appAlreadyRunning,
dataFields: dataFields)
}

@objc(updateCart:)
func updateCart(items: [[AnyHashable: Any]]) {
ITBInfo()

IterableAPI.updateCart(items: items.compactMap(CommerceItem.from(dict:)))
}

@objc(trackPurchase:items:dataFields:)
func trackPurchase(total: NSNumber,
Expand Down
4 changes: 3 additions & 1 deletion ios/RNIterableAPI/Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extension CommerceItem {
let url = dict["url"] as? String
let imageUrl = dict["imageUrl"] as? String
let categories = dict["categories"] as? [String]
let dataFields = dict["dataFields"] as? [AnyHashable: Any]

return CommerceItem(id: id,
name: name,
Expand All @@ -99,7 +100,8 @@ extension CommerceItem {
description: description,
url: url,
imageUrl: imageUrl,
categories: categories)
categories: categories,
dataFields: dataFields)
}
}

Expand Down
13 changes: 12 additions & 1 deletion ts/Iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ class IterableCommerceItem {
url?: string
imageUrl?: string
categories?: Array<string>
dataFields?: any

constructor(id: string, name: string, price: number, quantity: number, sku?: string, description?: string, url?: string, imageUrl?: string, categories?: Array<string>) {
constructor(id: string, name: string, price: number, quantity: number, sku?: string, description?: string, url?: string, imageUrl?: string, categories?: Array<string>, dataFields?: any | undefined) {
this.id = id
this.name = name
this.price = price
Expand All @@ -181,6 +182,7 @@ class IterableCommerceItem {
this.url = url
this.imageUrl = imageUrl
this.categories = categories
this.dataFields = dataFields
}
}

Expand Down Expand Up @@ -302,6 +304,15 @@ class Iterable {
RNIterableAPI.trackPushOpenWithCampaignId(campaignId, templateId, messageId, appAlreadyRunning, dataFields)
}

/**
*
* @param {Array<IterableCommerceItem>} items
*/
static updateCart(items: Array<IterableCommerceItem>) {
console.log("updateCart")
RNIterableAPI.updateCart(items)
}

/**
*
* @param {number} total
Expand Down
2 changes: 2 additions & 0 deletions ts/__mocks__/MockRNIterableAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class MockRNIterableAPI {

static trackPushOpenWithCampaignId = jest.fn()

static updateCart = jest.fn()

static trackPurchase = jest.fn()

static trackInAppOpen = jest.fn()
Expand Down
8 changes: 8 additions & 0 deletions ts/__tests__/Iterable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ test("trackPushOpenWithCampaignId", () => {
)
})

test("updateCart", () => {
Iterable.updateCart([new IterableCommerceItem("id1", "Boba Tea", 18, 26)])

expect(MockRNIterableAPI.updateCart).toBeCalledWith(
[new IterableCommerceItem("id1", "Boba Tea", 18, 26)]
)
})

test("trackPurchase", () => {
Iterable.trackPurchase(
10,
Expand Down

0 comments on commit 698cf84

Please sign in to comment.