|
1 | | -import { View, Text } from 'react-native'; |
| 1 | +import { |
| 2 | + Image, |
| 3 | + PixelRatio, |
| 4 | + Pressable, |
| 5 | + Text, |
| 6 | + TouchableOpacity, |
| 7 | + View, |
| 8 | + type TextStyle, |
| 9 | + type ViewStyle, |
| 10 | +} from 'react-native'; |
| 11 | + |
| 12 | +import { IterableLogoGrey } from '../../../core/assets'; |
| 13 | +import { IterableEmbeddedViewType } from '../../enums'; |
| 14 | +import { useEmbeddedView } from '../../hooks/useEmbeddedView'; |
2 | 15 | import type { IterableEmbeddedComponentProps } from '../../types/IterableEmbeddedComponentProps'; |
| 16 | +import { IMAGE_HEIGHT, styles } from './IterableEmbeddedCard.styles'; |
| 17 | + |
| 18 | +/** |
| 19 | + * TODO: Add default action click handler. See IterableEmbeddedView for functionality. |
| 20 | + */ |
3 | 21 |
|
4 | 22 | export const IterableEmbeddedCard = ({ |
5 | 23 | config, |
6 | 24 | message, |
7 | 25 | onButtonClick = () => {}, |
| 26 | + onMessageClick = () => {}, |
8 | 27 | }: IterableEmbeddedComponentProps) => { |
9 | | - console.log(`🚀 > IterableEmbeddedCard > config:`, config); |
10 | | - console.log(`🚀 > IterableEmbeddedCard > message:`, message); |
11 | | - console.log(`🚀 > IterableEmbeddedCard > onButtonClick:`, onButtonClick); |
| 28 | + const { |
| 29 | + handleButtonClick, |
| 30 | + handleMessageClick, |
| 31 | + media, |
| 32 | + parsedStyles, |
| 33 | + } = useEmbeddedView(IterableEmbeddedViewType.Card, { |
| 34 | + message, |
| 35 | + config, |
| 36 | + onButtonClick, |
| 37 | + onMessageClick, |
| 38 | + }); |
| 39 | + const buttons = message?.elements?.buttons ?? []; |
12 | 40 |
|
13 | 41 | return ( |
14 | | - <View> |
15 | | - <Text>IterableEmbeddedCard</Text> |
16 | | - </View> |
| 42 | + <Pressable onPress={() => handleMessageClick()}> |
| 43 | + <View |
| 44 | + style={[ |
| 45 | + styles.container, |
| 46 | + { |
| 47 | + backgroundColor: parsedStyles.backgroundColor, |
| 48 | + borderColor: parsedStyles.borderColor, |
| 49 | + borderRadius: parsedStyles.borderCornerRadius, |
| 50 | + borderWidth: parsedStyles.borderWidth, |
| 51 | + } as ViewStyle, |
| 52 | + ]} |
| 53 | + > |
| 54 | + <View |
| 55 | + style={[ |
| 56 | + styles.mediaContainer, |
| 57 | + media.shouldShow ? {} : styles.mediaContainerNoImage, |
| 58 | + ]} |
| 59 | + > |
| 60 | + <Image |
| 61 | + source={ |
| 62 | + media.shouldShow |
| 63 | + ? { |
| 64 | + uri: media.url as string, |
| 65 | + height: PixelRatio.getPixelSizeForLayoutSize(IMAGE_HEIGHT), |
| 66 | + } |
| 67 | + : |
| 68 | + IterableLogoGrey |
| 69 | + } |
| 70 | + style={ |
| 71 | + media.shouldShow |
| 72 | + ? styles.mediaImage |
| 73 | + : styles.mediaImagePlaceholder |
| 74 | + } |
| 75 | + alt={media.caption as string} |
| 76 | + /> |
| 77 | + </View> |
| 78 | + <View style={styles.bodyContainer}> |
| 79 | + <View style={styles.textContainer}> |
| 80 | + <Text |
| 81 | + style={[ |
| 82 | + styles.title, |
| 83 | + { color: parsedStyles.titleTextColor } as TextStyle, |
| 84 | + ]} |
| 85 | + > |
| 86 | + {message.elements?.title} |
| 87 | + </Text> |
| 88 | + <Text |
| 89 | + style={[ |
| 90 | + styles.body, |
| 91 | + { color: parsedStyles.bodyTextColor } as TextStyle, |
| 92 | + ]} |
| 93 | + > |
| 94 | + {message.elements?.body} |
| 95 | + </Text> |
| 96 | + </View> |
| 97 | + {buttons.length > 0 && ( |
| 98 | + <View style={styles.buttonContainer}> |
| 99 | + {buttons.map((button, index) => { |
| 100 | + const backgroundColor = |
| 101 | + index === 0 |
| 102 | + ? parsedStyles.primaryBtnBackgroundColor |
| 103 | + : parsedStyles.secondaryBtnBackgroundColor; |
| 104 | + const textColor = |
| 105 | + index === 0 |
| 106 | + ? parsedStyles.primaryBtnTextColor |
| 107 | + : parsedStyles.secondaryBtnTextColor; |
| 108 | + return ( |
| 109 | + <TouchableOpacity |
| 110 | + style={[styles.button, { backgroundColor } as ViewStyle]} |
| 111 | + onPress={() => handleButtonClick(button)} |
| 112 | + key={button.id} |
| 113 | + > |
| 114 | + <Text |
| 115 | + style={[ |
| 116 | + styles.buttonText, |
| 117 | + { color: textColor } as TextStyle, |
| 118 | + ]} |
| 119 | + > |
| 120 | + {button.title} |
| 121 | + </Text> |
| 122 | + </TouchableOpacity> |
| 123 | + ); |
| 124 | + })} |
| 125 | + </View> |
| 126 | + )} |
| 127 | + </View> |
| 128 | + </View> |
| 129 | + </Pressable> |
17 | 130 | ); |
18 | 131 | }; |
0 commit comments