Skip to content

Commit ea615b4

Browse files
committed
feat: modifies click event types
Uniform everything into `sendClickEvent` method and handles prevented prop.
1 parent 3b78407 commit ea615b4

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/Paywall/index.tsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import PaywallView, {
1111
type NativeProps,
1212
type FormEvent,
1313
type RegisterEvent,
14+
type ClickEvent,
1415
} from '../specs/PaywallViewNativeComponent';
1516
import NativePaywallModule from '../specs/NativePaywallModule';
1617
import type { NativeSyntheticEvent } from 'react-native';
1718

1819
export interface PaywallProps extends Omit<
1920
NativeProps,
2021
'appId' | 'config' | 'texts' | 'styles' | 'variables' |
21-
'onFormSubmit' | 'onRegister'
22+
'onFormSubmit' | 'onRegister' |
23+
'onSubscribeClick' | 'onLoginClick' | 'onDiscoveryLinkClick' | 'onDataPolicyClick'
2224
> {
2325
/**
2426
* Optional unique paywall id. When released, the snippet with the same id
@@ -32,6 +34,11 @@ export interface PaywallProps extends Omit<
3234

3335
onFormSubmit?: DirectEventHandlerWithResult<FormEvent, FieldError[]>;
3436
onRegister?: DirectEventHandlerWithResult<RegisterEvent, FieldError[]>;
37+
38+
onSubscribeClick?: (event: NativeSyntheticEvent<ClickEvent>, prevent: () => void) => void;
39+
onLoginClick?: (event: NativeSyntheticEvent<ClickEvent>, prevent: () => void) => void;
40+
onDiscoveryLinkClick?: (event: NativeSyntheticEvent<ClickEvent>, prevent: () => void) => void;
41+
onDataPolicyClick?: (event: NativeSyntheticEvent<ClickEvent>, prevent: () => void) => void;
3542
}
3643

3744
export interface PaywallState {
@@ -51,6 +58,10 @@ const Paywall = ({
5158
onRelease,
5259
onFormSubmit,
5360
onRegister,
61+
onSubscribeClick,
62+
onLoginClick,
63+
onDiscoveryLinkClick,
64+
onDataPolicyClick,
5465
...rest
5566
}: PaywallProps) => {
5667
const {
@@ -72,6 +83,7 @@ const Paywall = ({
7283
...factoryConfig,
7384
...config,
7485
}), [config, factoryConfig]);
86+
7587
const serializedConfig = useMemo(() => (
7688
JSON.stringify({
7789
...rawConfig,
@@ -92,13 +104,14 @@ const Paywall = ({
92104
const innerRef = useRef(null);
93105

94106
const sendMessage = (
95-
e: NativeSyntheticEvent<FormEvent | RegisterEvent>,
107+
e: NativeSyntheticEvent<FormEvent | RegisterEvent | ClickEvent>,
96108
type: string,
97109
data: any,
98110
) => {
99111
const message = JSON.stringify({
100112
type,
101113
data,
114+
prevented: (e.nativeEvent as any).prevented,
102115
_messageId: e.nativeEvent._messageId
103116
});
104117

@@ -114,6 +127,27 @@ const Paywall = ({
114127
NativePaywallModule.emit('poool:rn:event.' + type, message);
115128
};
116129

130+
const sendClickEvent = async (
131+
event: NativeSyntheticEvent<ClickEvent>,
132+
eventName: string,
133+
onClick: ((e: NativeSyntheticEvent<ClickEvent>, prevent: () => void) => void) | undefined,
134+
) => {
135+
event.persist();
136+
137+
try {
138+
event.nativeEvent.prevented = false;
139+
await onClick?.(event, () => {
140+
console.log(eventName + ' default behavior prevented');
141+
event.nativeEvent.prevented = true;
142+
});
143+
sendMessage(event, eventName + ':resolve', {});
144+
} catch (error) {
145+
sendMessage(event, eventName + ':reject', {
146+
message: (error as Error).message || error,
147+
});
148+
}
149+
}
150+
117151
return (
118152
<PaywallView
119153
ref={innerRef}
@@ -166,6 +200,18 @@ const Paywall = ({
166200
});
167201
}
168202
}}
203+
onSubscribeClick={ async (e) => {
204+
await sendClickEvent(e, 'onSubscribeClick', onSubscribeClick);
205+
}}
206+
onLoginClick={ async (e) => {
207+
await sendClickEvent(e, 'onLoginClick', onLoginClick);
208+
}}
209+
onDataPolicyClick={ async (e) => {
210+
await sendClickEvent(e, 'onDataPolicyClick', onDataPolicyClick);
211+
}}
212+
onDiscoveryLinkClick={ async (e) => {
213+
await sendClickEvent(e, 'onDiscoveryLinkClick', onDiscoveryLinkClick);
214+
}}
169215
/>
170216
);
171217
};

src/specs/PaywallViewNativeComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export interface RegisterEvent {
6464
export interface ClickEvent extends WidgetEvent {
6565
url: string;
6666
button: string;
67+
_messageId: Double;
68+
prevented: boolean;
6769
}
6870

6971
export interface AlternativeClickEvent extends WidgetEvent {

0 commit comments

Comments
 (0)