Skip to content

Commit 153a7ec

Browse files
committed
feat: add input field for placement IDs in Embedded component and update message retrieval logic
1 parent 7f043c8 commit 153a7ec

3 files changed

Lines changed: 94 additions & 19 deletions

File tree

example/src/components/Embedded/Embedded.styles.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { StyleSheet } from 'react-native';
2-
import { button, buttonText, container, hr, link } from '../../constants';
2+
import { button, buttonText, container, hr, link, input, title, subtitle } from '../../constants';
3+
import { neutrals, utilityColors, backgroundColors } from '../../constants/styles/colors';
34

45
const styles = StyleSheet.create({
56
button,
7+
buttonDisabled: {
8+
backgroundColor: neutrals.grey25,
9+
opacity: 0.6,
10+
},
611
buttonText,
7-
container: { ...container, paddingHorizontal: 0 },
12+
buttonTextDisabled: {
13+
color: neutrals.grey50,
14+
},
15+
container,
816
embeddedSection: {
917
display: 'flex',
1018
flexDirection: 'column',
@@ -21,11 +29,33 @@ const styles = StyleSheet.create({
2129
flexDirection: 'row',
2230
},
2331
hr,
32+
inputContainer: {
33+
marginVertical: 10,
34+
},
2435
link,
36+
subtitle: { ...subtitle, textAlign: 'center' },
2537
text: { textAlign: 'center' },
38+
textInput: input,
39+
title: { ...title, textAlign: 'center' },
2640
utilitySection: {
2741
paddingHorizontal: 16,
2842
},
43+
warningContainer: {
44+
backgroundColor: backgroundColors.backgroundWarningSubtle,
45+
borderLeftColor: utilityColors.warning100,
46+
borderLeftWidth: 4,
47+
borderRadius: 5,
48+
marginBottom: 12,
49+
marginHorizontal: 16,
50+
marginTop: 0,
51+
padding: 12,
52+
},
53+
warningText: {
54+
color: utilityColors.warning100,
55+
fontSize: 14,
56+
fontWeight: '600',
57+
textAlign: 'center',
58+
},
2959
});
3060

3161
export default styles;

example/src/components/Embedded/Embedded.tsx

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ScrollView, Text, TouchableOpacity, View } from 'react-native';
1+
import { ScrollView, Text, TextInput, TouchableOpacity, View } from 'react-native';
22
import { useCallback, useState } from 'react';
33
import {
44
Iterable,
@@ -10,10 +10,21 @@ import { SafeAreaView } from 'react-native-safe-area-context';
1010
import styles from './Embedded.styles';
1111

1212
export const Embedded = () => {
13+
const [placementIdsInput, setPlacementIdsInput] = useState<string>('');
1314
const [embeddedMessages, setEmbeddedMessages] = useState<
1415
IterableEmbeddedMessage[]
1516
>([]);
1617

18+
// Parse placement IDs from input
19+
const parsedPlacementIds = placementIdsInput
20+
.split(',')
21+
.map((id) => id.trim())
22+
.filter((id) => id !== '')
23+
.map((id) => parseInt(id, 10))
24+
.filter((id) => !isNaN(id));
25+
26+
const idsToFetch = parsedPlacementIds.length > 0 ? parsedPlacementIds : null;
27+
1728
const syncEmbeddedMessages = useCallback(() => {
1829
Iterable.embeddedManager.syncMessages();
1930
}, []);
@@ -32,12 +43,20 @@ export const Embedded = () => {
3243
Iterable.embeddedManager.endSession();
3344
}, []);
3445

35-
const getEmbeddedMessages = useCallback((ids: number[] | null = null) => {
36-
Iterable.embeddedManager.getMessages(ids).then((messages: IterableEmbeddedMessage[]) => {
46+
const getEmbeddedMessages = useCallback(() => {
47+
// Don't fetch if no IDs
48+
if (parsedPlacementIds.length === 0) {
49+
console.log('No placement IDs entered, button should be disabled');
50+
return;
51+
}
52+
53+
console.log('Fetching messages for placement IDs:', idsToFetch);
54+
55+
Iterable.embeddedManager.getMessages(idsToFetch).then((messages: IterableEmbeddedMessage[]) => {
3756
setEmbeddedMessages(messages);
3857
console.log(messages);
3958
});
40-
}, []);
59+
}, [idsToFetch, parsedPlacementIds.length]);
4160

4261
const startEmbeddedImpression = useCallback(
4362
(message: IterableEmbeddedMessage) => {
@@ -73,15 +92,18 @@ export const Embedded = () => {
7392

7493
return (
7594
<SafeAreaView style={styles.container}>
76-
<Text style={styles.text}>EMBEDDED</Text>
95+
<Text style={styles.title}>Embedded</Text>
96+
{!Iterable.embeddedManager.isEnabled && (
97+
<View style={styles.warningContainer}>
98+
<Text style={styles.warningText}>
99+
⚠️ Embedded messaging is disabled. Please enable it in your Iterable config.
100+
</Text>
101+
</View>
102+
)}
103+
<Text style={styles.subtitle}>
104+
Enter placement IDs to fetch embedded messages
105+
</Text>
77106
<View style={styles.utilitySection}>
78-
<Text style={styles.text}>
79-
Does embedded class exist? {Iterable.embeddedManager ? 'Yes' : 'No'}
80-
</Text>
81-
<Text style={styles.text}>
82-
Is embedded manager enabled?{' '}
83-
{Iterable.embeddedManager.isEnabled ? 'Yes' : 'No'}
84-
</Text>
85107
<TouchableOpacity style={styles.button} onPress={syncEmbeddedMessages}>
86108
<Text style={styles.buttonText}>Sync messages</Text>
87109
</TouchableOpacity>
@@ -91,9 +113,34 @@ export const Embedded = () => {
91113
<TouchableOpacity style={styles.button} onPress={endEmbeddedSession}>
92114
<Text style={styles.buttonText}>End session</Text>
93115
</TouchableOpacity>
94-
<TouchableOpacity style={styles.button} onPress={() => getEmbeddedMessages()}>
95-
<Text style={styles.buttonText}>Get messages</Text>
96-
</TouchableOpacity>
116+
<View style={styles.inputContainer}>
117+
<Text style={styles.text}>
118+
Placement IDs (comma-separated):
119+
</Text>
120+
<TextInput
121+
style={styles.textInput}
122+
placeholder="e.g., 1, 2, 3"
123+
placeholderTextColor="#999"
124+
value={placementIdsInput}
125+
onChangeText={setPlacementIdsInput}
126+
keyboardType="numbers-and-punctuation"
127+
/>
128+
<TouchableOpacity
129+
style={[
130+
styles.button,
131+
parsedPlacementIds.length === 0 && styles.buttonDisabled
132+
]}
133+
onPress={getEmbeddedMessages}
134+
disabled={parsedPlacementIds.length === 0}
135+
>
136+
<Text style={[
137+
styles.buttonText,
138+
parsedPlacementIds.length === 0 && styles.buttonTextDisabled
139+
]}>
140+
Get messages for placement ids
141+
</Text>
142+
</TouchableOpacity>
143+
</View>
97144
</View>
98145
<View style={styles.hr} />
99146
<ScrollView>

example/src/hooks/useIterableApp.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ export const IterableAppProvider: FunctionComponent<
161161
retryBackoff: IterableRetryBackoff.linear,
162162
};
163163

164-
config.enableEmbeddedMessaging = true;
165-
166164
config.onJwtError = (authFailure) => {
167165
console.log('onJwtError', authFailure);
168166

0 commit comments

Comments
 (0)