Skip to content

DIA-4070 standard action name #7

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

Merged
merged 4 commits into from
May 31, 2024
Merged
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 @@ -111,7 +111,9 @@ class RNSourcepointCmpModule internal constructor(context: ReactApplicationConte
}

override fun onAction(view: View, consentAction: ConsentAction): ConsentAction {
sendEvent(SDKEvent.onAction, createMap().apply { putString("actionType", consentAction.actionType.name) })
sendEvent(SDKEvent.onAction, createMap().apply {
putString("actionType", RNSourcepointActionType.from(consentAction.actionType).name)
})
return consentAction
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sourcepoint.reactnativecmp

import com.facebook.react.bridge.ReadableMap
import com.sourcepoint.cmplibrary.data.network.util.CampaignsEnv
import com.sourcepoint.cmplibrary.model.exposed.ActionType
import com.sourcepoint.cmplibrary.model.exposed.TargetingParam

fun campaignsEnvFrom(rawValue: String?): CampaignsEnv? =
Expand All @@ -24,6 +25,23 @@ data class SPCampaigns(
val environment: CampaignsEnv?
)

enum class RNSourcepointActionType {
acceptAll, rejectAll, showPrivacyManager, saveAndExit, dismiss, pmCancel, unknown;

companion object {
fun from(spAction: ActionType): RNSourcepointActionType =
when (spAction) {
ActionType.ACCEPT_ALL -> acceptAll
ActionType.REJECT_ALL -> rejectAll
ActionType.SHOW_OPTIONS -> showPrivacyManager
ActionType.SAVE_AND_EXIT -> saveAndExit
ActionType.MSG_CANCEL -> dismiss
ActionType.PM_DISMISS -> pmCancel
else -> unknown
}
}
}

fun ReadableMap.SPCampaign() = SPCampaign(
rawTargetingParam = this.getMap("targetingParams"),
supportLegacyUSPString = if(this.hasKey("supportLegacyUSPString")) this.getBoolean("supportLegacyUSPString") else false
Expand Down
8 changes: 8 additions & 0 deletions e2e/full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const app = {
await this.forSDKToBeFinished();
},

assertActionWarnWith: async function (actionType: string) {
await expect(element(by.text(`action: ${actionType}`))).toExist();
},

reloadMessages: async function ({ clearData }: { clearData: boolean }) {
if (clearData) await this.clearData();
await this.loadMessagesButton.tap();
Expand Down Expand Up @@ -97,7 +101,9 @@ describe('SourcepointSDK', () => {
it('Accepting All, works', async () => {
await launchApp();
await app.acceptAll(); // GDPR
await app.assertActionWarnWith('acceptAll');
await app.acceptAll(); // USNAT
await app.assertActionWarnWith('saveAndExit'); // USNAT uses saveAndExit for accepting/rejecting all
await app.forSDKToBeFinished();
await assertUUIDsDontChangeAfterReloadingMessages();
await expect(app.gdprConsentStatusLabel).toHaveText('consentedAll');
Expand All @@ -107,7 +113,9 @@ describe('SourcepointSDK', () => {
it('Rejecting All, works', async () => {
await launchApp();
await app.rejectAll(); // GDPR
await app.assertActionWarnWith('rejectAll');
await app.rejectAll(); // USNAT
await app.assertActionWarnWith('saveAndExit'); // USNAT uses saveAndExit for accepting/rejecting all
await app.forSDKToBeFinished();
await assertUUIDsDontChangeAfterReloadingMessages();
await expect(app.gdprConsentStatusLabel).toHaveText('rejectedAll');
Expand Down
4 changes: 3 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
consentManager.current?.getUserData().then(setUserData);
});

consentManager.current?.onAction((actionType) => console.log(actionType));
consentManager.current?.onAction(({ actionType }) =>
console.warn(`action: ${actionType}`)
);

consentManager.current?.onError((description) => {
setSDKStatus(SDKStatus.Errored);
Expand All @@ -90,7 +92,7 @@
return () => {
consentManager.current?.dispose();
};
}, []);

Check warning on line 95 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'authId'. Either include it or remove the dependency array

const onLoadMessagePress = useCallback(() => {
consentManager.current?.loadMessage({ authId });
Expand Down
25 changes: 25 additions & 0 deletions ios/RNSourcepointActionType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// RNSourcepointActionType.swift
// sourcepoint-react-native-cmp
//
// Created by Andre Herculano on 31/5/24.
//

import Foundation
import ConsentViewController

enum RNSourcepointActionType: String, Codable {
case acceptAll, rejectAll, showPrivacyManager, saveAndExit, dismiss, pmCancel, unknown

init(from actionType: SPActionType){
switch actionType {
case .AcceptAll: self = .acceptAll
case .RejectAll: self = .rejectAll
case .SaveAndExit: self = .saveAndExit
case .ShowPrivacyManager: self = .showPrivacyManager
case .Dismiss: self = .dismiss
case .PMCancel: self = .pmCancel
default: self = .unknown
}
}
}
4 changes: 0 additions & 4 deletions ios/RNSourcepointCmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ RCT_EXTERN_METHOD(supportedEvents)
return YES;
}

/// TODO: check if this really can be here or need fixing in the SDK
/// https://reactnative.dev/docs/native-modules-ios
/// https://github.com/OneSignal/react-native-onesignal/issues/749
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
Expand Down
4 changes: 1 addition & 3 deletions ios/RNSourcepointCmp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import React
}

func loadMessage(_ params: SPLoadMessageParams) {
print("calling loadMessage with: ", params.authId as Any)
consentManager?.loadMessage(forAuthId: params.authId, pubData: nil)
}

Expand All @@ -67,11 +66,10 @@ extension RNSourcepointCmp: SPDelegate {
UIApplication.shared.delegate?.window??.rootViewController
}

// TODO: standardize action names
func onAction(_ action: SPAction, from controller: UIViewController) {
RNSourcepointCmp.shared?.sendEvent(
withName: "onAction",
body: ["actionType": action.type.description]
body: ["actionType": RNSourcepointActionType(from: action.type).rawValue]
)
}

Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
import type { Spec, SPCampaigns, SPUserData, LoadMessageParams } from './types';
import type {
Spec,
SPCampaigns,
SPUserData,
LoadMessageParams,
SPActionType,
} from './types';

const LINKING_ERROR =
`The package '@sourcepoint/react-native-cmp' doesn't seem to be linked. Make sure: \n\n` +
Expand Down Expand Up @@ -61,7 +67,7 @@ export class SPConsentManager implements Spec {
RNSourcepointCmp.loadUSNatPrivacyManager(pmId);
}

onAction(callback: (action: string) => void): void {
onAction(callback: (body: { actionType: SPActionType }) => void): void {
this.emitter.removeAllListeners('onAction');
this.emitter.addListener('onAction', callback);
}
Expand Down
13 changes: 11 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export const enum SPCampaignEnvironment {
Stage = 'Stage',
}

export const enum SPActionType {
acceptAll = 'acceptAll',
rejectAll = 'rejectAll',
saveAndExit = 'saveAndExit',
showOptions = 'showOptions',
dismiss = 'dismiss',
pmCancel = 'pmCancel',
unknown = 'unknown',
}

export type SPCampaigns = {
gdpr?: SPCampaign;
usnat?: SPCampaign;
Expand Down Expand Up @@ -103,8 +113,7 @@ export interface Spec extends TurboModule {
loadGDPRPrivacyManager(pmId: string): void;
loadUSNatPrivacyManager(pmId: string): void;

// TODO: change action from string to enum
onAction(callback: (action: string) => void): void;
onAction(callback: (body: { actionType: SPActionType }) => void): void;
onSPUIReady(callback: () => void): void;
onSPUIFinished(callback: () => void): void;
onFinished(callback: () => void): void;
Expand Down
Loading