Files
super-app/App/Container/Navi-Insurance/screen/InsuranceLandingPageScreen.tsx
2024-03-27 15:06:03 +00:00

148 lines
3.8 KiB
TypeScript

import React, { useEffect, useState } from "react";
import {
Animated,
NativeScrollEvent,
NativeSyntheticEvent,
ScrollView,
View,
StyleSheet
} from "react-native";
import {
BaseActionTypes,
GenericActionPayload,
} from "../../../common/actions/GenericAction";
import { CtaData, CtaType } from "../../../common/interface";
import { Widget } from "../../../common/interface/widgets/Widget";
import { ScreenData } from "../../../common/interface/widgets/screenData/ScreenData";
import { NativeDeeplinkNavigatorModule } from "../../../common/native-module/NativeModules";
import Colors from "../../../../assets/colors/colors";
import BaseWidget from "../../../../components/widgets/BaseWidget";
import { ScreenActionTypes } from "../../../common/screen/ScreenActionTypes";
const InsuranceLandingPageScreen = ({
ctaData,
screenData,
handleActions,
}: {
ctaData: CtaData;
screenData: ScreenData | null;
handleActions: (screenPayload?: GenericActionPayload) => void;
}) => {
const [scrollY, setScrollY] = useState(0);
const handleScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
setScrollY(event.nativeEvent.contentOffset.y);
};
const headerBackgroundColor =
scrollY > 84 ? Colors.aliceBlue : Colors.transparent;
const handleClick = (cta?: CtaData) => {
if (!cta) return; // Handle case when cta is undefined or null
try {
switch (cta.type) {
case CtaType.DEEP_LINK:
case CtaType.USE_ROOT_DEEPLINK_NAVIGATOR:
NativeDeeplinkNavigatorModule.navigateToNaviInsuranceDeeplinkNavigator(
JSON.stringify(cta)
);
break;
default:
NativeDeeplinkNavigatorModule.navigateToNaviDeeplinkNavigator(
JSON.stringify(cta)
);
break;
}
} catch (error) {
// #TODO: Handle the error gracefully using Sentry.
console.error("Error while navigating to deep link:", error);
}
};
useEffect(() => {
handleActions({
baseActionType: BaseActionTypes.SCREEN_ACTION,
metaData: [
{
actionType: ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
},
],
});
}, []);
return (
<View style={[styles.container, screenData?.screenStyle]}>
<Animated.View
style={[styles.header, { backgroundColor: headerBackgroundColor }]}
>
{getWidgetViews(
screenData?.screenWidgets?.headerWidgets,
handleActions,
handleClick
)}
</Animated.View>
<ScrollView
contentContainerStyle={styles.content}
scrollEventThrottle={16}
onScroll={handleScroll}
>
{getWidgetViews(
screenData?.screenWidgets?.contentWidgets,
handleActions,
handleClick
)}
</ScrollView>
<View style={styles.footer}>
{getWidgetViews(
screenData?.screenWidgets?.footerWidgets,
handleActions,
handleClick
)}
</View>
</View>
);
};
function getWidgetViews(
widgetList: Widget[] | undefined,
handleActions: (screenActionPayload?: GenericActionPayload) => void,
handleClick?: (ctaData: CtaData) => void
): React.JSX.Element {
return (
<View>
{widgetList?.map((widget, index) => {
return (
<BaseWidget
widget={widget}
handleScreenActions={handleActions}
widgetIndex={index}
key={index}
handleClick={handleClick}
/>
);
})}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
},
header: {
alignItems: "stretch",
position: "absolute",
zIndex: 1,
},
content: {
flexGrow: 1
},
footer: {
alignItems: "stretch"
},
});
export default InsuranceLandingPageScreen;