Skip to content

Commit e8f0205

Browse files
author
Cello Dev
committed
feat: added productUserDetails support
1 parent e8f6db3 commit e8f0205

5 files changed

Lines changed: 117 additions & 12 deletions

File tree

README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,55 @@ Next, rebuild your app as described in the ["Adding custom native code"](https:/
213213

214214
---
215215

216-
### `Cello.initialize(productId, token)`
216+
### `Cello.initialize()`
217217

218218
Initializes the Cello Referral Component in your product
219219

220-
### Options
220+
### API Styles
221+
222+
The `initialize` method supports two calling styles:
223+
224+
#### 1. Object-based API (Recommended - New)
225+
226+
```javascript
227+
import Cello from '@getcello/cello-react-native';
228+
229+
const config = await Cello.initialize({
230+
productId: 'your-product-id',
231+
token: 'your-token',
232+
productUserDetails: {
233+
firstName: 'John',
234+
lastName: 'Doe',
235+
fullName: 'John Doe',
236+
email: 'john.doe@example.com',
237+
},
238+
});
239+
```
240+
241+
#### 2. Argument-based API (Legacy - Still Supported)
242+
243+
```javascript
244+
const config = await Cello.initialize('your-product-id', 'your-token');
245+
```
246+
247+
### InitializeOptions
248+
249+
| Property | Type | Required | Description |
250+
| ------------------ | ------------------ | -------- | --------------------------------- |
251+
| productId | string | yes | Your product ID from Cello Portal |
252+
| token | string | yes | User authentication token |
253+
| productUserDetails | ProductUserDetails | no | User details object (see below) |
254+
255+
### ProductUserDetails
256+
257+
Optional object with user information:
221258

222-
| Type | Type | Required |
223-
| --------- | ------ | -------- |
224-
| productId | string | yes |
225-
| token | string | yes |
259+
| Property | Type | Description |
260+
| --------- | ------ | -------------------- |
261+
| firstName | string | User's first name |
262+
| lastName | string | User's last name |
263+
| fullName | string | User's full name |
264+
| email | string | User's email address |
226265

227266
### Returns
228267

android/src/main/java/com/celloreactnative/CelloReactNativeModule.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import com.facebook.react.bridge.ReactApplicationContext
44
import com.facebook.react.bridge.ReactContextBaseJavaModule
55
import com.facebook.react.bridge.ReactMethod
66
import com.facebook.react.bridge.Promise
7+
import com.facebook.react.bridge.ReadableMap
78
import com.cello.cello_sdk.Cello
9+
import com.cello.cello_sdk.ProductUserDetails
810
import kotlinx.coroutines.*
911
import com.facebook.react.bridge.WritableMap
1012
import com.facebook.react.bridge.WritableNativeMap
@@ -16,15 +18,29 @@ class CelloReactNativeModule(reactContext: ReactApplicationContext) :
1618
return NAME
1719
}
1820
@ReactMethod
19-
fun initialize(productId: String, token: String, environment: String?, promise: Promise) {
21+
fun initialize(productId: String, token: String, environment: String?, productUserDetailsMap: ReadableMap?, promise: Promise) {
2022
val activity = currentActivity ?: run {
2123
promise.reject("ActivityError", "Activity is null")
2224
return
2325
}
2426

2527
CoroutineScope(Dispatchers.IO).launch {
2628
try {
27-
Cello.initialize(activity, productId, token, environment)
29+
val productUserDetails = try {
30+
productUserDetailsMap?.let { map ->
31+
ProductUserDetails(
32+
firstName = if (map.hasKey("firstName")) map.getString("firstName") else null,
33+
lastName = if (map.hasKey("lastName")) map.getString("lastName") else null,
34+
fullName = if (map.hasKey("fullName")) map.getString("fullName") else null,
35+
email = if (map.hasKey("email")) map.getString("email") else null
36+
)
37+
}
38+
} catch (e: Exception) {
39+
android.util.Log.w("CelloReactNative", "Failed to parse productUserDetails: ${e.message}")
40+
null
41+
}
42+
43+
Cello.initialize(activity, productId, token, environment, productUserDetails)
2844
withContext(Dispatchers.Main) {
2945
val client = Cello.client()
3046
if (client != null) {

ios/CelloReactNative.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(CelloReactNative, NSObject)
55
RCT_EXTERN_METHOD(initialize:(NSString *)productId
66
withToken:(NSString *)token
77
withEnvironment:(nullable NSString *)environment
8+
withProductUserDetails:(nullable NSDictionary *)productUserDetails
89
withResolver:(RCTPromiseResolveBlock)resolve
910
withRejecter:(RCTPromiseRejectBlock)reject)
1011

ios/CelloReactNative.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ import CelloSDK
33
@objc(CelloReactNative)
44
class CelloReactNative: NSObject {
55

6-
@objc(initialize:withToken:withEnvironment:withResolver:withRejecter:)
7-
func initialize(productId: String, token: String, environment: String?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
6+
@objc(initialize:withToken:withEnvironment:withProductUserDetails:withResolver:withRejecter:)
7+
func initialize(productId: String, token: String, environment: String?, productUserDetailsDict: NSDictionary?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
88
let resolver = resolve
99
let rejecter = reject
1010

11-
Cello.initialize(for: productId, with: token, environment: environment) { result in
11+
let productUserDetails: ProductUserDetails?
12+
if let dict = productUserDetailsDict, dict.count > 0 {
13+
productUserDetails = ProductUserDetails(
14+
firstName: dict["firstName"] as? String,
15+
lastName: dict["lastName"] as? String,
16+
fullName: dict["fullName"] as? String,
17+
email: dict["email"] as? String
18+
)
19+
} else {
20+
productUserDetails = nil
21+
}
22+
23+
Cello.initialize(for: productId, with: token, environment: environment, productUserDetails: productUserDetails) { result in
1224
switch result {
1325
case .success(let configuration):
1426
resolver(configuration)

src/index.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
22

3+
export interface ProductUserDetails {
4+
firstName?: string;
5+
lastName?: string;
6+
fullName?: string;
7+
email?: string;
8+
}
9+
10+
export interface InitializeOptions {
11+
productId: string;
12+
token: string;
13+
environment?: string;
14+
productUserDetails?: ProductUserDetails;
15+
}
16+
317
const LINKING_ERROR =
418
`The package 'cello-react-native' doesn't seem to be linked. Make sure: \n\n` +
519
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
@@ -28,12 +42,35 @@ const CelloEventEmitter = NativeModules.CelloEventEmitter
2842
}
2943
);
3044

45+
function initialize(options: InitializeOptions): Promise<any>;
3146
function initialize(
3247
productId: string,
3348
token: string,
3449
environment?: string
50+
): Promise<any>;
51+
52+
function initialize(
53+
productIdOrOptions: string | InitializeOptions,
54+
token?: string,
55+
environment?: string
3556
): Promise<any> {
36-
return CelloReactNative.initialize(productId, token, environment);
57+
if (typeof productIdOrOptions === 'object') {
58+
const options = productIdOrOptions;
59+
return CelloReactNative.initialize(
60+
options.productId,
61+
options.token,
62+
options.environment,
63+
options.productUserDetails
64+
);
65+
}
66+
67+
// Old API doesn't support productUserDetails, pass undefined explicitly for RN bridge
68+
return CelloReactNative.initialize(
69+
productIdOrOptions,
70+
token!,
71+
environment,
72+
undefined
73+
);
3774
}
3875

3976
function updateToken(token: string): Promise<any> {

0 commit comments

Comments
 (0)