UNPKG

react-native-unit-components

Version:

Unit React Native components

76 lines 2.73 kB
import React, { useRef, useState } from 'react'; import { Dimensions } from 'react-native'; import { PageMessage } from '../../messages/webMessages/pageMessage'; import { WebComponent } from '../../webComponent/WebComponent'; import { PresentationMode, WebComponentType } from '../../types/internal/webComponent.types'; import { withReduxStore } from '../../helpers/store/helpers'; import { getMultipleCardsParams, getMultipleCardsScript } from './UNMultipleCardsComponent.utils'; import { UnitMessage } from '../../messages/webMessages/unitMessages'; import { RESPONSE_KEYS } from '../../messages/webMessages/onLoadMessage'; import { MultipleCardsMessage } from '../../messages/webMessages/multipleCardsMessage'; import { UNBaseView } from '../../nativeComponents/UNBaseView'; const DEFAULT_HEIGHT = Dimensions.get('window').height * 0.5; const UNMultipleCardsComponent = props => { const webRef = useRef(null); const [defaultHeight, setDefaultHeight] = useState(); const cardClicked = card => { if (props.onCardClicked) { props.onCardClicked(card); } }; const handleUnitOnLoad = response => { if (!props.onLoad) { return; } if (RESPONSE_KEYS.errors in response) { props.onLoad(response); return; } if (RESPONSE_KEYS.cards in response) { // MultipleCardsOnLoadResponse; props.onLoad(response[RESPONSE_KEYS.cards]); return; } console.error('On Load Error: unexpected response type'); return; }; const handleWebViewMessage = message => { if (!message || !message.details) return; switch (message.type) { case UnitMessage.UNIT_ON_LOAD: handleUnitOnLoad(message.details); break; case PageMessage.PAGE_HEIGHT: { const currentHeight = message.details.height; currentHeight === 0 && setDefaultHeight(DEFAULT_HEIGHT); break; } case MultipleCardsMessage.UNIT_MULTIPLE_CARDS_CARD_CLICKED: { cardClicked(message.details); } } }; const style = defaultHeight ? { height: defaultHeight } : { flex: 1 }; return /*#__PURE__*/React.createElement(UNBaseView, { style: style, onLoadError: handleUnitOnLoad }, /*#__PURE__*/React.createElement(WebComponent, { ref: webRef, type: WebComponentType.multipleCards, presentationMode: PresentationMode.Inherit, params: getMultipleCardsParams(props), script: getMultipleCardsScript(), onMessage: message => handleWebViewMessage(message), nestedScrollEnabled: true, theme: props.theme, language: props.language })); }; export default withReduxStore(UNMultipleCardsComponent); //# sourceMappingURL=UNMultipleCardsComponent.js.map