UNPKG

react-native-unit-components

Version:

Unit React Native components

119 lines 4.49 kB
import React, { useImperativeHandle, useRef, useState } from 'react'; import { Dimensions } from 'react-native'; import { UnitMessage, UnitRequestRefreshEventTypes } from '../../messages/webMessages/unitMessages'; import { PageMessage } from '../../messages/webMessages/pageMessage'; import { WebComponent } from '../../webComponent/WebComponent'; import { getActivityParams, getActivityScript, injectFiltersChanged, injectRefreshEventIfNeeded } from './UNActivityComponent.utils'; import { RESPONSE_KEYS } from '../../messages/webMessages/onLoadMessage'; import { ActivityMessage } from '../../messages/webMessages/activityMessage'; import { BottomSheetRenderingType } from '../../types/internal/bottomSheet.types'; import { PresentationMode, WebComponentType } from '../../types/internal/webComponent.types'; import { BottomSheetNativeMessage } from '../../messages/nativeMessages/bottomSheetMessage'; import { withReduxStoreAndRefForwarding } from '../../helpers/store/helpers'; import { setEvent } from '../../slices/SharedEventsSlice'; import { useDispatch } from 'react-redux'; import { useListenerToEvent } from '../../hooks/useListenerToEvent'; import { UNBaseView } from '../../nativeComponents/UNBaseView'; const DEFAULT_HEIGHT = Dimensions.get('window').height * 0.5; const UNActivityComponent = /*#__PURE__*/React.forwardRef(function UNActivityComponent(props, activityRef) { const dispatch = useDispatch(); const webRef = useRef(null); const [defaultHeight, setDefaultHeight] = useState(); const requestRefresh = data => { injectRefreshEventIfNeeded(webRef.current, data); }; useImperativeHandle(activityRef, () => ({ refresh() { requestRefresh({ type: UnitRequestRefreshEventTypes.REQUEST_REFRESH_EVENT, refEvent: undefined, dependencies: [WebComponentType.activity.valueOf()], resourceId: '' }); } })); const dispatchActivityFiltersChanged = query => { injectFiltersChanged(webRef.current, query); }; useListenerToEvent({ busEventKey: UnitMessage.UNIT_REQUEST_REFRESH, action: requestRefresh }); useListenerToEvent({ busEventKey: ActivityMessage.UNIT_ACTIVITY_FILTERS_CHANGED, action: dispatchActivityFiltersChanged }); const handleUnitOnLoad = response => { if (!props.onLoad) { return; } if (RESPONSE_KEYS.errors in response) { props.onLoad(response); return; } if (RESPONSE_KEYS.authorizations in response && RESPONSE_KEYS.transactions in response) { // ActivityOnLoadResponse; const activityOnLoad = { data: { [RESPONSE_KEYS.authorizations]: response[RESPONSE_KEYS.authorizations].data, [RESPONSE_KEYS.transactions]: response[RESPONSE_KEYS.transactions].data } }; props.onLoad(activityOnLoad); return; } console.error('On Load Error: unexpected response type'); return; }; const handleWebViewMessage = message => { switch (message.type) { case UnitMessage.UNIT_REQUEST_RENDERING: { const slotData = { componentName: WebComponentType.activity, componentResourceId: props.accountId, requestRenderingEvent: message.details }; const data = { type: BottomSheetRenderingType.Slot, data: slotData }; dispatch(setEvent({ key: BottomSheetNativeMessage.REQUEST_RENDERING, data })); break; } case UnitMessage.UNIT_ON_LOAD: handleUnitOnLoad(message.details); break; case PageMessage.PAGE_HEIGHT: { const currentHeight = message.details.height; currentHeight === 0 && setDefaultHeight(DEFAULT_HEIGHT); break; } } }; const style = defaultHeight ? { height: defaultHeight } : { flex: 1 }; return /*#__PURE__*/React.createElement(UNBaseView, { style: style, onLoadError: handleUnitOnLoad }, /*#__PURE__*/React.createElement(WebComponent, { ref: webRef, type: WebComponentType.activity, presentationMode: PresentationMode.Inherit, params: getActivityParams(props), onMessage: message => handleWebViewMessage(message), nestedScrollEnabled: true, theme: props.theme, language: props.language, script: getActivityScript() })); }); export default withReduxStoreAndRefForwarding(UNActivityComponent); //# sourceMappingURL=UNActivityComponent.js.map