|
| 1 | +import { |
| 2 | + Image, |
| 3 | + Text, |
| 4 | + TouchableOpacity, |
| 5 | + View, |
| 6 | + type TextStyle, |
| 7 | + type ViewStyle, |
| 8 | + PixelRatio, |
| 9 | +} from 'react-native'; |
| 10 | + |
| 11 | +import { IterableEmbeddedViewType } from '../../enums'; |
| 12 | +import { useEmbeddedView } from '../../hooks/useEmbeddedView'; |
| 13 | +import type { IterableEmbeddedComponentProps } from '../../types/IterableEmbeddedComponentProps'; |
| 14 | +import { |
| 15 | + styles, |
| 16 | + IMAGE_HEIGHT, |
| 17 | + IMAGE_WIDTH, |
| 18 | +} from './IterableEmbeddedBanner.styles'; |
| 19 | + |
| 20 | +/** |
| 21 | + * TODO: figure out how default action works. |
| 22 | + */ |
| 23 | + |
| 24 | +export const IterableEmbeddedBanner = ({ |
| 25 | + config, |
| 26 | + message, |
| 27 | + onButtonClick = () => {}, |
| 28 | +}: IterableEmbeddedComponentProps) => { |
| 29 | + const { parsedStyles, media, handleButtonClick } = useEmbeddedView( |
| 30 | + IterableEmbeddedViewType.Banner, |
| 31 | + { message, config, onButtonClick } |
| 32 | + ); |
| 33 | + |
| 34 | + const buttons = message.elements?.buttons ?? []; |
| 35 | + |
| 36 | + return ( |
| 37 | + <View |
| 38 | + style={[ |
| 39 | + styles.container, |
| 40 | + { |
| 41 | + backgroundColor: parsedStyles.backgroundColor, |
| 42 | + borderColor: parsedStyles.borderColor, |
| 43 | + borderRadius: parsedStyles.borderCornerRadius, |
| 44 | + borderWidth: parsedStyles.borderWidth, |
| 45 | + } as ViewStyle, |
| 46 | + ]} |
| 47 | + > |
| 48 | + {/* eslint-disable-next-line react-native/no-inline-styles */} |
| 49 | + <View style={[styles.bodyContainer, { gap: media.shouldShow ? 16 : 0 }]}> |
| 50 | + <View style={styles.textContainer}> |
| 51 | + <Text |
| 52 | + style={[ |
| 53 | + styles.title, |
| 54 | + { color: parsedStyles.titleTextColor } as TextStyle, |
| 55 | + ]} |
| 56 | + > |
| 57 | + {message.elements?.title} |
| 58 | + </Text> |
| 59 | + <Text |
| 60 | + style={[ |
| 61 | + styles.body, |
| 62 | + { color: parsedStyles.bodyTextColor } as TextStyle, |
| 63 | + ]} |
| 64 | + > |
| 65 | + {message.elements?.body} |
| 66 | + </Text> |
| 67 | + </View> |
| 68 | + {media.shouldShow && ( |
| 69 | + <View style={styles.mediaContainer}> |
| 70 | + <Image |
| 71 | + source={{ |
| 72 | + uri: media.url as string, |
| 73 | + width: PixelRatio.getPixelSizeForLayoutSize(IMAGE_WIDTH), |
| 74 | + height: PixelRatio.getPixelSizeForLayoutSize(IMAGE_HEIGHT), |
| 75 | + }} |
| 76 | + style={styles.mediaImage} |
| 77 | + alt={media.caption as string} |
| 78 | + /> |
| 79 | + </View> |
| 80 | + )} |
| 81 | + </View> |
| 82 | + {buttons.length > 0 && ( |
| 83 | + <View style={styles.buttonContainer}> |
| 84 | + {buttons.map((button, index) => { |
| 85 | + const backgroundColor = |
| 86 | + index === 0 |
| 87 | + ? parsedStyles.primaryBtnBackgroundColor |
| 88 | + : parsedStyles.secondaryBtnBackgroundColor; |
| 89 | + const textColor = |
| 90 | + index === 0 |
| 91 | + ? parsedStyles.primaryBtnTextColor |
| 92 | + : parsedStyles.secondaryBtnTextColor; |
| 93 | + return ( |
| 94 | + <TouchableOpacity |
| 95 | + style={[styles.button, { backgroundColor } as ViewStyle]} |
| 96 | + onPress={() => handleButtonClick(button)} |
| 97 | + key={button.id} |
| 98 | + > |
| 99 | + <Text |
| 100 | + style={[styles.buttonText, { color: textColor } as TextStyle]} |
| 101 | + > |
| 102 | + {button.title} |
| 103 | + </Text> |
| 104 | + </TouchableOpacity> |
| 105 | + ); |
| 106 | + })} |
| 107 | + </View> |
| 108 | + )} |
| 109 | + </View> |
| 110 | + ); |
| 111 | +}; |
0 commit comments