Skip to content

Allow messageLanguage parameter #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.sourcepoint.cmplibrary.model.ConsentAction
import com.sourcepoint.cmplibrary.model.exposed.SPConsents
import com.sourcepoint.cmplibrary.util.clearAllData
import com.sourcepoint.cmplibrary.util.userConsents
import com.sourcepoint.cmplibrary.model.MessageLanguage
import com.sourcepoint.reactnativecmp.consents.RNSPUserData
import org.json.JSONObject

Expand All @@ -38,14 +39,19 @@ class RNSourcepointCmpModule internal constructor(context: ReactApplicationConte
override fun getName() = NAME

@ReactMethod
override fun build(accountId: Int, propertyId: Int, propertyName: String, campaigns: ReadableMap) {
override fun build(accountId: Int, propertyId: Int, propertyName: String, campaigns: ReadableMap, messageLanguage: String) {
val convertedCampaigns = campaigns.SPCampaigns()

val config = SpConfigDataBuilder().apply {
addAccountId(accountId)
addPropertyName(propertyName)
addPropertyId(propertyId)
addMessageTimeout(30000)

if (messageLanguage.isNotBlank()) {
addMessageLanguage(getMessageLanguage(messageLanguage))
}

convertedCampaigns.gdpr?.let {
addCampaign(campaignType = CampaignType.GDPR, params = it.targetingParams, groupPmId = null)
}
Expand All @@ -66,6 +72,10 @@ class RNSourcepointCmpModule internal constructor(context: ReactApplicationConte
}
}

private fun getMessageLanguage(languageCode: String?): MessageLanguage {
return MessageLanguage.values().firstOrNull { it.value == languageCode } ?: MessageLanguage.ENGLISH
}

private fun runOnMainThread(runnable: () -> Unit) {
reactApplicationContext.runOnUiQueueThread(runnable)
}
Expand Down
2 changes: 1 addition & 1 deletion android/src/oldarch/RNSourcepointCmpSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.facebook.react.bridge.ReadableMap
abstract class RNSourcepointCmpSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {

abstract fun build(accountId: Int, propertyId: Int, propertyName: String, campaigns: ReadableMap)
abstract fun build(accountId: Int, propertyId: Int, propertyName: String, campaigns: ReadableMap, messageLanguage: String)
abstract fun loadMessage(params: ReadableMap?)
abstract fun clearLocalData()
abstract fun getUserData(promise: Promise)
Expand Down
2 changes: 1 addition & 1 deletion ios/RNSourcepointCmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@interface RCT_EXTERN_MODULE(RNSourcepointCmp, RCTEventEmitter)

RCT_EXTERN_METHOD(build:(int)accountId propertyId:(int)propertyId propertyName:(NSString *)propertyName campaigns:(SPCampaigns*)campaigns)
RCT_EXTERN_METHOD(build:(int)accountId propertyId:(int)propertyId propertyName:(NSString *)propertyName campaigns:(SPCampaigns*)campaigns messageLanguage:(NSString *)messageLanguage)

RCT_EXTERN_METHOD(loadMessage: (SPLoadMessageParams *)params)
RCT_EXTERN_METHOD(clearLocalData)
Expand Down
7 changes: 6 additions & 1 deletion ios/RNSourcepointCmp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ import React
resolve(RNSPUserData(from: consentManager?.userData).toDictionary())
}

func build(_ accountId: Int, propertyId: Int, propertyName: String, campaigns: SPCampaigns) {
func build(_ accountId: Int, propertyId: Int, propertyName: String, campaigns: SPCampaigns, messageLanguage: String) {
let manager = SPConsentManager(
accountId: accountId,
propertyId: propertyId,
propertyName: try! SPPropertyName(propertyName),
campaigns: campaigns,
delegate: self
)

if !messageLanguage.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
manager.messageLanguage = SPMessageLanguage(rawValue: messageLanguage) ?? SPMessageLanguage.English
}

manager.messageTimeoutInSeconds = 30
RNSourcepointCmp.shared?.consentManager = manager
}
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@
"react": "*",
"react-native": "*"
},
"workspaces": [
"example"
],
"packageManager": "[email protected]",
"jest": {
"preset": "react-native",
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
SPActionType,
} from './types';

import { Language } from './types';

const LINKING_ERROR =
`The package '@sourcepoint/react-native-cmp' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
Expand Down Expand Up @@ -42,9 +44,10 @@ export class SPConsentManager implements Spec {
accountId: number,
propertyId: number,
propertyName: string,
campaigns: SPCampaigns
campaigns: SPCampaigns,
messageLanguage?: Language
) {
RNSourcepointCmp.build(accountId, propertyId, propertyName, campaigns);
RNSourcepointCmp.build(accountId, propertyId, propertyName, campaigns, messageLanguage || '');
}

getUserData(): Promise<SPUserData> {
Expand Down
44 changes: 43 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
import type { TurboModule } from 'react-native';

export enum Language {
BULGARIAN = "BG",
CATALAN = "CA",
CHINESE = "ZH",
CROATIAN = "HR",
CZECH = "CS",
DANISH = "DA",
DUTCH = "NL",
ENGLISH = "EN",
ESTONIAN = "ET",
FINNISH = "FI",
FRENCH = "FR",
GAELIC = "GD",
GERMAN = "DE",
GREEK = "EL",
HEBREW = "HE",
HUNGARIAN = "HU",
ICELANDIC = "IS",
INDONESIAN = "ID",
ITALIAN = "IT",
JAPANESE = "JA",
KOREAN = "KO",
LATVIAN = "LV",
LITHUANIAN = "LT",
MACEDONIAN = "MK",
MALAY = "MS",
NORWEGIAN = "NO",
POLISH = "PL",
PORTUGUESE = "PT",
ROMANIAN = "RO",
RUSSIAN = "RU",
SERBIAN_CYRILLIC = "SR-CYRL",
SERBIAN_LATIN = "SR-LATN",
SLOVAKIAN = "SK",
SLOVENIAN = "SL",
SPANISH = "ES",
SWEDISH = "SV",
TAGALOG = "TL",
TURKISH = "TR"
};

export type SPCampaign = {
targetingParams?: Object;
supportLegacyUSPString?: boolean;
Expand Down Expand Up @@ -105,7 +146,8 @@ export interface Spec extends TurboModule {
accountId: number,
propertyId: number,
propertyName: string,
campaigns: SPCampaigns
campaigns: SPCampaigns,
messageLanguage?: Language
): void;
getUserData(): Promise<SPUserData>;
loadMessage(params?: LoadMessageParams): void;
Expand Down
Loading