@outerjoyn/react-native 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -0
- package/package.json +57 -0
- package/src/NativeSDK.ts +164 -0
- package/src/OuterJoynProvider.tsx +828 -0
- package/src/OuterJoynSDK.ts +261 -0
- package/src/OuterJoynSheet.tsx +218 -0
- package/src/PromotionsView.tsx +76 -0
- package/src/components/BalanceSummary.tsx +267 -0
- package/src/components/CheckoutPointsApply.tsx +787 -0
- package/src/components/LoyaltyBanner.tsx +281 -0
- package/src/components/LoyaltyProfileSection.tsx +273 -0
- package/src/components/PostCheckoutSavePrompt.tsx +369 -0
- package/src/components/index.ts +30 -0
- package/src/core/ErrorBoundary.tsx +51 -0
- package/src/core/analytics.ts +264 -0
- package/src/core/api-client.ts +253 -0
- package/src/core/colors.ts +20 -0
- package/src/core/event-stream.ts +229 -0
- package/src/core/i18n.ts +666 -0
- package/src/core/offline.ts +148 -0
- package/src/core/types.ts +214 -0
- package/src/core/version.ts +2 -0
- package/src/html.ts +154 -0
- package/src/index.ts +102 -0
- package/src/screens/AccountScreen.tsx +701 -0
- package/src/screens/ConnectScreen.tsx +1499 -0
- package/src/screens/ExchangeScreen.tsx +706 -0
- package/src/screens/RedeemScreen.tsx +683 -0
- package/src/screens/index.ts +21 -0
- package/src/types.ts +116 -0
- package/src/useOuterJoyn.ts +45 -0
- package/src/widgets.tsx +226 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OuterJoynSDK — Imperative API (Tier 4)
|
|
3
|
+
*
|
|
4
|
+
* Provides a non-React imperative interface for presenting loyalty widgets.
|
|
5
|
+
* Under the hood, bridges to React via module-level subscriptions read by
|
|
6
|
+
* `<OuterJoynImperativeSheets />`.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* ```tsx
|
|
10
|
+
* // Mount once in App.tsx
|
|
11
|
+
* import { OuterJoynImperativeSheets } from '@outerjoyn/react-native';
|
|
12
|
+
* <OuterJoynImperativeSheets />
|
|
13
|
+
*
|
|
14
|
+
* // Call from anywhere
|
|
15
|
+
* import { OuterJoynSDK } from '@outerjoyn/react-native';
|
|
16
|
+
* OuterJoynSDK.configure({ sessionToken: 'ojsdk_live_...' });
|
|
17
|
+
* const result = await OuterJoynSDK.present('pay', { orderTotalCents: 4999 });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import React, { useEffect, useState } from 'react';
|
|
22
|
+
import type { AppearanceConfig } from './core/types';
|
|
23
|
+
import type { PayResult, ConnectResult, ExchangeResult, RedeemResult } from './types';
|
|
24
|
+
|
|
25
|
+
// =============================================================================
|
|
26
|
+
// Types
|
|
27
|
+
// =============================================================================
|
|
28
|
+
|
|
29
|
+
export type ImperativeWidgetType = 'pay' | 'connect' | 'exchange' | 'redeem' | 'account';
|
|
30
|
+
|
|
31
|
+
export interface ImperativeSDKConfig {
|
|
32
|
+
sessionToken: string;
|
|
33
|
+
apiBaseUrl?: string;
|
|
34
|
+
appearance?: AppearanceConfig;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ImperativePayOptions {
|
|
38
|
+
orderTotalCents: number;
|
|
39
|
+
currencyCode?: string;
|
|
40
|
+
merchantOrderId?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type ImperativePresentOptions = ImperativePayOptions | Record<string, any>;
|
|
44
|
+
|
|
45
|
+
export type ImperativePresentResult = PayResult | ConnectResult | ExchangeResult | RedeemResult | void;
|
|
46
|
+
|
|
47
|
+
interface PendingPresentation {
|
|
48
|
+
widget: ImperativeWidgetType;
|
|
49
|
+
options: ImperativePresentOptions;
|
|
50
|
+
resolve: (result: ImperativePresentResult) => void;
|
|
51
|
+
reject: (error: Error) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// =============================================================================
|
|
55
|
+
// Module-level state (singleton bridge between imperative calls and React)
|
|
56
|
+
// =============================================================================
|
|
57
|
+
|
|
58
|
+
let _config: ImperativeSDKConfig | null = null;
|
|
59
|
+
let _pending: PendingPresentation | null = null;
|
|
60
|
+
let _presenting = false;
|
|
61
|
+
const _listeners = new Set<() => void>();
|
|
62
|
+
|
|
63
|
+
function notifyListeners() {
|
|
64
|
+
_listeners.forEach((fn) => fn());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// =============================================================================
|
|
68
|
+
// OuterJoynSDK — static imperative API
|
|
69
|
+
// =============================================================================
|
|
70
|
+
|
|
71
|
+
export class OuterJoynSDK {
|
|
72
|
+
static readonly version = '1.0.0';
|
|
73
|
+
|
|
74
|
+
/** Configure the SDK. Must be called before `present()`. */
|
|
75
|
+
static configure(config: ImperativeSDKConfig): void {
|
|
76
|
+
if (!config.sessionToken) {
|
|
77
|
+
throw new Error('OuterJoynSDK.configure: sessionToken is required');
|
|
78
|
+
}
|
|
79
|
+
_config = {
|
|
80
|
+
apiBaseUrl: 'https://api.outerjoyn.com',
|
|
81
|
+
...config,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Current configuration (read-only). */
|
|
86
|
+
static get config(): Readonly<ImperativeSDKConfig> | null {
|
|
87
|
+
return _config;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Whether a widget is currently being presented. */
|
|
91
|
+
static get isPresenting(): boolean {
|
|
92
|
+
return _presenting;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Present a widget. Returns a Promise that resolves with the widget's result
|
|
97
|
+
* or rejects on error/cancellation.
|
|
98
|
+
*
|
|
99
|
+
* Requires `<OuterJoynImperativeSheets />` to be mounted in your React tree.
|
|
100
|
+
*/
|
|
101
|
+
static present(
|
|
102
|
+
widget: ImperativeWidgetType,
|
|
103
|
+
options: ImperativePresentOptions = {},
|
|
104
|
+
): Promise<ImperativePresentResult> {
|
|
105
|
+
if (!_config) {
|
|
106
|
+
return Promise.reject(
|
|
107
|
+
new Error('OuterJoynSDK.present: call configure() first'),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (_presenting) {
|
|
111
|
+
return Promise.reject(
|
|
112
|
+
new Error('OuterJoynSDK.present: another widget is already presenting'),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return new Promise<ImperativePresentResult>((resolve, reject) => {
|
|
117
|
+
_pending = { widget, options, resolve, reject };
|
|
118
|
+
_presenting = true;
|
|
119
|
+
notifyListeners();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Dismiss the currently presenting widget. */
|
|
124
|
+
static dismiss(): void {
|
|
125
|
+
if (_pending) {
|
|
126
|
+
_pending.reject(new Error('Dismissed by user'));
|
|
127
|
+
_pending = null;
|
|
128
|
+
}
|
|
129
|
+
_presenting = false;
|
|
130
|
+
notifyListeners();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
// Internal: used by <OuterJoynImperativeSheets />
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
/** @internal Subscribe to state changes. Returns unsubscribe function. */
|
|
138
|
+
static _subscribe(listener: () => void): () => void {
|
|
139
|
+
_listeners.add(listener);
|
|
140
|
+
return () => _listeners.delete(listener);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** @internal Read current pending presentation. */
|
|
144
|
+
static _getPending(): PendingPresentation | null {
|
|
145
|
+
return _pending;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** @internal Called by the React bridge when a widget completes. */
|
|
149
|
+
static _resolve(result: ImperativePresentResult): void {
|
|
150
|
+
if (_pending) {
|
|
151
|
+
_pending.resolve(result);
|
|
152
|
+
_pending = null;
|
|
153
|
+
}
|
|
154
|
+
_presenting = false;
|
|
155
|
+
notifyListeners();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @internal Called by the React bridge when a widget errors. */
|
|
159
|
+
static _reject(error: Error): void {
|
|
160
|
+
if (_pending) {
|
|
161
|
+
_pending.reject(error);
|
|
162
|
+
_pending = null;
|
|
163
|
+
}
|
|
164
|
+
_presenting = false;
|
|
165
|
+
notifyListeners();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// =============================================================================
|
|
170
|
+
// <OuterJoynImperativeSheets /> — React bridge component
|
|
171
|
+
// =============================================================================
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Mount this component once in your app's root to enable the imperative
|
|
175
|
+
* `OuterJoynSDK.present()` API. It renders nothing when idle.
|
|
176
|
+
*
|
|
177
|
+
* ```tsx
|
|
178
|
+
* import { OuterJoynImperativeSheets } from '@outerjoyn/react-native';
|
|
179
|
+
*
|
|
180
|
+
* export default function App() {
|
|
181
|
+
* return (
|
|
182
|
+
* <>
|
|
183
|
+
* <YourApp />
|
|
184
|
+
* <OuterJoynImperativeSheets />
|
|
185
|
+
* </>
|
|
186
|
+
* );
|
|
187
|
+
* }
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export function OuterJoynImperativeSheets(): React.ReactElement | null {
|
|
191
|
+
const [state, setState] = useState<{
|
|
192
|
+
widget: ImperativeWidgetType | null;
|
|
193
|
+
options: ImperativePresentOptions;
|
|
194
|
+
}>({ widget: null, options: {} });
|
|
195
|
+
|
|
196
|
+
useEffect(() => {
|
|
197
|
+
function sync() {
|
|
198
|
+
const pending = OuterJoynSDK._getPending();
|
|
199
|
+
if (pending) {
|
|
200
|
+
setState({ widget: pending.widget, options: pending.options });
|
|
201
|
+
} else {
|
|
202
|
+
setState({ widget: null, options: {} });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Sync initial state
|
|
206
|
+
sync();
|
|
207
|
+
return OuterJoynSDK._subscribe(sync);
|
|
208
|
+
}, []);
|
|
209
|
+
|
|
210
|
+
if (!state.widget || !OuterJoynSDK.config) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Lazy-require to avoid circular dependency issues at module load time.
|
|
215
|
+
// These are the same sheet components exported from ./widgets.
|
|
216
|
+
const { PaySheet, ConnectSheet, ExchangeSheet, RedeemSheet, AccountSheet } =
|
|
217
|
+
require('./widgets') as typeof import('./widgets');
|
|
218
|
+
|
|
219
|
+
const config = OuterJoynSDK.config;
|
|
220
|
+
const sharedProps = {
|
|
221
|
+
sessionToken: config.sessionToken,
|
|
222
|
+
apiBaseUrl: config.apiBaseUrl,
|
|
223
|
+
visible: true,
|
|
224
|
+
onClose: () => OuterJoynSDK.dismiss(),
|
|
225
|
+
onError: (err: any) => OuterJoynSDK._reject(err instanceof Error ? err : new Error(err?.message ?? String(err))),
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
switch (state.widget) {
|
|
229
|
+
case 'pay': {
|
|
230
|
+
const opts = state.options as ImperativePayOptions;
|
|
231
|
+
return React.createElement(PaySheet, {
|
|
232
|
+
...sharedProps,
|
|
233
|
+
orderTotal: opts.orderTotalCents,
|
|
234
|
+
currencyCode: opts.currencyCode,
|
|
235
|
+
onSuccess: (result: PayResult) => OuterJoynSDK._resolve(result),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
case 'connect':
|
|
239
|
+
return React.createElement(ConnectSheet, {
|
|
240
|
+
...sharedProps,
|
|
241
|
+
onSuccess: (result: ConnectResult) => OuterJoynSDK._resolve(result),
|
|
242
|
+
});
|
|
243
|
+
case 'exchange':
|
|
244
|
+
return React.createElement(ExchangeSheet, {
|
|
245
|
+
...sharedProps,
|
|
246
|
+
onSuccess: (result: ExchangeResult) => OuterJoynSDK._resolve(result),
|
|
247
|
+
});
|
|
248
|
+
case 'redeem':
|
|
249
|
+
return React.createElement(RedeemSheet, {
|
|
250
|
+
...sharedProps,
|
|
251
|
+
onSuccess: (result: RedeemResult) => OuterJoynSDK._resolve(result),
|
|
252
|
+
});
|
|
253
|
+
case 'account':
|
|
254
|
+
return React.createElement(AccountSheet, {
|
|
255
|
+
...sharedProps,
|
|
256
|
+
onSuccess: () => OuterJoynSDK._resolve(undefined),
|
|
257
|
+
});
|
|
258
|
+
default:
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import React, { useCallback, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Modal,
|
|
4
|
+
View,
|
|
5
|
+
StyleSheet,
|
|
6
|
+
TouchableOpacity,
|
|
7
|
+
Text,
|
|
8
|
+
ActivityIndicator,
|
|
9
|
+
} from 'react-native';
|
|
10
|
+
import { WebView } from 'react-native-webview';
|
|
11
|
+
import type { WebViewMessage, SDKError } from './types';
|
|
12
|
+
import { generateWidgetHTML } from './html';
|
|
13
|
+
|
|
14
|
+
interface SheetProps {
|
|
15
|
+
visible: boolean;
|
|
16
|
+
sessionToken: string;
|
|
17
|
+
apiBaseUrl?: string;
|
|
18
|
+
theme?: any;
|
|
19
|
+
module: string;
|
|
20
|
+
widgetOptions?: Record<string, any>;
|
|
21
|
+
onSuccess?: (result: any) => void;
|
|
22
|
+
onError?: (error: SDKError) => void;
|
|
23
|
+
onClose?: () => void;
|
|
24
|
+
/** Sheet title shown in the header */
|
|
25
|
+
title?: string;
|
|
26
|
+
/** Height as percentage of screen (0-1). Default 0.85 */
|
|
27
|
+
heightRatio?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Base bottom-sheet component that renders an OuterJoyn SDK widget inside a WebView.
|
|
32
|
+
*
|
|
33
|
+
* This follows the same pattern as Plaid Link on mobile: a Modal wraps a WebView
|
|
34
|
+
* that loads the CDN-hosted SDK bundle. Communication flows through postMessage
|
|
35
|
+
* (the SDK's postmessage.ts already detects `window.ReactNativeWebView`).
|
|
36
|
+
*/
|
|
37
|
+
export function OuterJoynSheet({
|
|
38
|
+
visible,
|
|
39
|
+
sessionToken,
|
|
40
|
+
apiBaseUrl,
|
|
41
|
+
theme,
|
|
42
|
+
module,
|
|
43
|
+
widgetOptions,
|
|
44
|
+
onSuccess,
|
|
45
|
+
onError,
|
|
46
|
+
onClose,
|
|
47
|
+
title,
|
|
48
|
+
heightRatio = 0.85,
|
|
49
|
+
}: SheetProps) {
|
|
50
|
+
const webViewRef = useRef<WebView>(null);
|
|
51
|
+
const [loading, setLoading] = useState(true);
|
|
52
|
+
|
|
53
|
+
const html = generateWidgetHTML(
|
|
54
|
+
module,
|
|
55
|
+
{ sessionToken, apiBaseUrl, theme },
|
|
56
|
+
widgetOptions,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const handleMessage = useCallback(
|
|
60
|
+
(event: any) => {
|
|
61
|
+
try {
|
|
62
|
+
const msg: WebViewMessage = JSON.parse(event.nativeEvent.data);
|
|
63
|
+
switch (msg.type) {
|
|
64
|
+
case 'sdk_ready':
|
|
65
|
+
setLoading(false);
|
|
66
|
+
break;
|
|
67
|
+
case 'sdk_success':
|
|
68
|
+
onSuccess?.(msg.result);
|
|
69
|
+
break;
|
|
70
|
+
case 'sdk_error':
|
|
71
|
+
onError?.(msg.error);
|
|
72
|
+
break;
|
|
73
|
+
case 'sdk_close':
|
|
74
|
+
onClose?.();
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
// Ignore non-JSON messages
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
[onSuccess, onError, onClose],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const handleClose = useCallback(() => {
|
|
85
|
+
onClose?.();
|
|
86
|
+
}, [onClose]);
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<Modal
|
|
90
|
+
visible={visible}
|
|
91
|
+
animationType="slide"
|
|
92
|
+
transparent
|
|
93
|
+
onRequestClose={handleClose}
|
|
94
|
+
>
|
|
95
|
+
<View style={styles.overlay}>
|
|
96
|
+
<View
|
|
97
|
+
style={[styles.sheet, { height: `${heightRatio * 100}%` as any }]}
|
|
98
|
+
>
|
|
99
|
+
<View style={styles.safeArea}>
|
|
100
|
+
{/* Header */}
|
|
101
|
+
<View style={styles.header}>
|
|
102
|
+
<View style={styles.handle} />
|
|
103
|
+
{title && <Text style={styles.title}>{title}</Text>}
|
|
104
|
+
<TouchableOpacity
|
|
105
|
+
onPress={handleClose}
|
|
106
|
+
style={styles.closeButton}
|
|
107
|
+
>
|
|
108
|
+
<Text style={styles.closeText}>{'✕'}</Text>
|
|
109
|
+
</TouchableOpacity>
|
|
110
|
+
</View>
|
|
111
|
+
|
|
112
|
+
{/* WebView */}
|
|
113
|
+
<View style={styles.webViewContainer}>
|
|
114
|
+
{loading && (
|
|
115
|
+
<View style={styles.loadingOverlay}>
|
|
116
|
+
<ActivityIndicator size="large" color="#6366f1" />
|
|
117
|
+
</View>
|
|
118
|
+
)}
|
|
119
|
+
<WebView
|
|
120
|
+
ref={webViewRef}
|
|
121
|
+
source={{ html }}
|
|
122
|
+
style={styles.webView}
|
|
123
|
+
onMessage={handleMessage}
|
|
124
|
+
javaScriptEnabled
|
|
125
|
+
domStorageEnabled
|
|
126
|
+
originWhitelist={['*']}
|
|
127
|
+
scrollEnabled
|
|
128
|
+
bounces={false}
|
|
129
|
+
showsVerticalScrollIndicator={false}
|
|
130
|
+
onShouldStartLoadWithRequest={(request) => {
|
|
131
|
+
if (
|
|
132
|
+
request.url === 'about:blank' ||
|
|
133
|
+
request.url.includes('sdk.outerjoyn.com') ||
|
|
134
|
+
request.url.includes('api.outerjoyn.com')
|
|
135
|
+
) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}}
|
|
140
|
+
/>
|
|
141
|
+
</View>
|
|
142
|
+
</View>
|
|
143
|
+
</View>
|
|
144
|
+
</View>
|
|
145
|
+
</Modal>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const styles = StyleSheet.create({
|
|
150
|
+
overlay: {
|
|
151
|
+
flex: 1,
|
|
152
|
+
backgroundColor: 'rgba(0,0,0,0.5)',
|
|
153
|
+
justifyContent: 'flex-end',
|
|
154
|
+
},
|
|
155
|
+
sheet: {
|
|
156
|
+
backgroundColor: '#fff',
|
|
157
|
+
borderTopLeftRadius: 20,
|
|
158
|
+
borderTopRightRadius: 20,
|
|
159
|
+
overflow: 'hidden',
|
|
160
|
+
},
|
|
161
|
+
safeArea: {
|
|
162
|
+
flex: 1,
|
|
163
|
+
},
|
|
164
|
+
header: {
|
|
165
|
+
flexDirection: 'row',
|
|
166
|
+
alignItems: 'center',
|
|
167
|
+
justifyContent: 'center',
|
|
168
|
+
paddingHorizontal: 16,
|
|
169
|
+
paddingVertical: 12,
|
|
170
|
+
borderBottomWidth: 1,
|
|
171
|
+
borderBottomColor: '#e5e7eb',
|
|
172
|
+
},
|
|
173
|
+
handle: {
|
|
174
|
+
position: 'absolute',
|
|
175
|
+
top: 8,
|
|
176
|
+
width: 36,
|
|
177
|
+
height: 4,
|
|
178
|
+
borderRadius: 2,
|
|
179
|
+
backgroundColor: '#d1d5db',
|
|
180
|
+
},
|
|
181
|
+
title: {
|
|
182
|
+
fontSize: 17,
|
|
183
|
+
fontWeight: '600',
|
|
184
|
+
color: '#111827',
|
|
185
|
+
},
|
|
186
|
+
closeButton: {
|
|
187
|
+
position: 'absolute',
|
|
188
|
+
right: 16,
|
|
189
|
+
width: 32,
|
|
190
|
+
height: 32,
|
|
191
|
+
borderRadius: 16,
|
|
192
|
+
backgroundColor: '#f3f4f6',
|
|
193
|
+
alignItems: 'center',
|
|
194
|
+
justifyContent: 'center',
|
|
195
|
+
},
|
|
196
|
+
closeText: {
|
|
197
|
+
fontSize: 16,
|
|
198
|
+
color: '#6b7280',
|
|
199
|
+
},
|
|
200
|
+
webViewContainer: {
|
|
201
|
+
flex: 1,
|
|
202
|
+
},
|
|
203
|
+
webView: {
|
|
204
|
+
flex: 1,
|
|
205
|
+
backgroundColor: 'transparent',
|
|
206
|
+
},
|
|
207
|
+
loadingOverlay: {
|
|
208
|
+
position: 'absolute',
|
|
209
|
+
top: 0,
|
|
210
|
+
left: 0,
|
|
211
|
+
right: 0,
|
|
212
|
+
bottom: 0,
|
|
213
|
+
backgroundColor: '#fff',
|
|
214
|
+
alignItems: 'center',
|
|
215
|
+
justifyContent: 'center',
|
|
216
|
+
zIndex: 1,
|
|
217
|
+
},
|
|
218
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React, { useCallback, useState } from 'react';
|
|
2
|
+
import { View, StyleSheet, ActivityIndicator } from 'react-native';
|
|
3
|
+
import { WebView } from 'react-native-webview';
|
|
4
|
+
import type { PromotionsViewProps, WebViewMessage } from './types';
|
|
5
|
+
import { generatePromotionsHTML } from './html';
|
|
6
|
+
|
|
7
|
+
/** Inline promotions display (not a modal -- renders directly in the view hierarchy) */
|
|
8
|
+
export function PromotionsView({
|
|
9
|
+
sessionToken,
|
|
10
|
+
apiBaseUrl,
|
|
11
|
+
theme,
|
|
12
|
+
arrayId,
|
|
13
|
+
layout = 'carousel',
|
|
14
|
+
height = 300,
|
|
15
|
+
}: PromotionsViewProps) {
|
|
16
|
+
const [loading, setLoading] = useState(true);
|
|
17
|
+
const html = generatePromotionsHTML(
|
|
18
|
+
{ sessionToken, apiBaseUrl, theme },
|
|
19
|
+
arrayId,
|
|
20
|
+
layout,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const handleMessage = useCallback((event: any) => {
|
|
24
|
+
try {
|
|
25
|
+
const msg: WebViewMessage = JSON.parse(event.nativeEvent.data);
|
|
26
|
+
if (msg.type === 'sdk_ready') {
|
|
27
|
+
setLoading(false);
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
// Ignore
|
|
31
|
+
}
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<View style={[styles.container, { height }]}>
|
|
36
|
+
{loading && (
|
|
37
|
+
<View style={styles.loading}>
|
|
38
|
+
<ActivityIndicator size="small" color="#6366f1" />
|
|
39
|
+
</View>
|
|
40
|
+
)}
|
|
41
|
+
<WebView
|
|
42
|
+
source={{ html }}
|
|
43
|
+
style={styles.webView}
|
|
44
|
+
onMessage={handleMessage}
|
|
45
|
+
javaScriptEnabled
|
|
46
|
+
domStorageEnabled
|
|
47
|
+
originWhitelist={['*']}
|
|
48
|
+
scrollEnabled
|
|
49
|
+
showsVerticalScrollIndicator={false}
|
|
50
|
+
/>
|
|
51
|
+
</View>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const styles = StyleSheet.create({
|
|
56
|
+
container: {
|
|
57
|
+
width: '100%' as any,
|
|
58
|
+
overflow: 'hidden',
|
|
59
|
+
borderRadius: 12,
|
|
60
|
+
},
|
|
61
|
+
webView: {
|
|
62
|
+
flex: 1,
|
|
63
|
+
backgroundColor: 'transparent',
|
|
64
|
+
},
|
|
65
|
+
loading: {
|
|
66
|
+
position: 'absolute',
|
|
67
|
+
top: 0,
|
|
68
|
+
left: 0,
|
|
69
|
+
right: 0,
|
|
70
|
+
bottom: 0,
|
|
71
|
+
alignItems: 'center',
|
|
72
|
+
justifyContent: 'center',
|
|
73
|
+
backgroundColor: '#f9fafb',
|
|
74
|
+
zIndex: 1,
|
|
75
|
+
},
|
|
76
|
+
});
|