-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathIterableEmbeddedView.tsx
More file actions
115 lines (111 loc) · 3.78 KB
/
IterableEmbeddedView.tsx
File metadata and controls
115 lines (111 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { useMemo } from 'react';
import { IterableEmbeddedViewType } from '../enums/IterableEmbeddedViewType';
import type { IterableEmbeddedComponentProps } from '../types/IterableEmbeddedComponentProps';
import { IterableEmbeddedBanner } from './IterableEmbeddedBanner';
import { IterableEmbeddedCard } from './IterableEmbeddedCard';
import { IterableEmbeddedNotification } from './IterableEmbeddedNotification';
/**
* The props for the IterableEmbeddedView component.
*/
export interface IterableEmbeddedViewProps
extends IterableEmbeddedComponentProps {
/** The type of view to render. */
viewType: IterableEmbeddedViewType;
}
/**
*
* @param viewType - The type of view to render.
* @param message - The message to render.
* @param config - The config for the IterableEmbeddedView component, most likely used to style the view.
* @param onButtonClick - The function to call when a button is clicked.
* @param onMessageClick - The function to call when the message is clicked.
* @returns The IterableEmbeddedView component.
*
* This component is used to render the following pre-created, customizable
* message displays included with Iterables RN SDK: cards, banners, and
* notifications.
*
* @example
* ```tsx
* import {
* IterableAction,
* IterableEmbeddedView,
* IterableEmbeddedViewType,
* type IterableEmbeddedMessage,
* type IterableEmbeddedMessageElementsButton,
* } from '@iterable/react-native-sdk';
*
* // See `IterableEmbeddedViewType` for available view types.
* const viewType = IterableEmbeddedViewType.Card;
*
* // Messages usually come from the embedded manager. `placementIds` is `number[] | null`
* // (use `null` to load messages for all placements), for example:
* // Iterable.embeddedManager.getMessages([101, 102]).then((messages) => { ... });
* const message: IterableEmbeddedMessage = {
* metadata: {
* messageId: 'test-message-123',
* placementId: 101,
* },
* elements: {
* title: 'Test Title',
* body: 'Test Body',
* buttons: [
* {
* id: 'button-1',
* title: 'Button 1',
* action: new IterableAction('openUrl', 'https://example.com/one'),
* },
* {
* id: 'button-2',
* title: 'Button 2',
* action: new IterableAction('openUrl', 'https://example.com/two'),
* },
* ],
* },
* };
*
* // The config is used to style the component.
* // See `IterableEmbeddedViewConfig` for available config options.
* const config = { backgroundColor: '#FFFFFF', borderRadius: 8 };
*
* // `onButtonClick` will be called when a button is clicked.
* // This callback allows you to add custom logic in addition to the SDK's default handling.
* const onButtonClick = (button: IterableEmbeddedMessageElementsButton) => {
* console.log('Button clicked', button.id, button.title, button.action);
* };
*
* // `onMessageClick` will be called when the message is clicked anywhere outside of a button.
* // If a default action is set, it will be handled prior to this callback.
* const onMessageClick = () => {
* console.log('Message clicked');
* };
*
* return (
* <IterableEmbeddedView
* viewType={viewType}
* message={message}
* config={config}
* onButtonClick={onButtonClick}
* onMessageClick={onMessageClick}
* />
* );
* ```
*/
export const IterableEmbeddedView = ({
viewType,
...props
}: IterableEmbeddedViewProps) => {
const Cmp = useMemo(() => {
switch (viewType) {
case IterableEmbeddedViewType.Card:
return IterableEmbeddedCard;
case IterableEmbeddedViewType.Notification:
return IterableEmbeddedNotification;
case IterableEmbeddedViewType.Banner:
return IterableEmbeddedBanner;
default:
return null;
}
}, [viewType]);
return Cmp ? <Cmp {...props} /> : null;
};