138 lines
5.3 KiB
TypeScript
138 lines
5.3 KiB
TypeScript
import { ActionMetaData, BaseActionTypes } from "../actions/GenericAction";
|
|
import { ScreenData } from "../interface/widgets/screenData/ScreenData";
|
|
import { ScreenActionTypes } from "./ScreenActionTypes";
|
|
import { Dispatch, SetStateAction } from "react";
|
|
import { post, get } from "../../../network/NetworkService";
|
|
import { CtaData } from "../interface";
|
|
import { getXTargetHeaderInfo } from "../../../network/ApiClient";
|
|
import { GI } from "../constants/NavigationHandlerConstants";
|
|
import { ScreenState } from "./BaseScreen";
|
|
import { BASE_SCREEN } from "../constants/ScreenNameConstants";
|
|
import { logToSentry } from "../hooks/useSentryLogging";
|
|
import { sendAsGlobalErrorEvent } from "../hooks/useAnalyticsEvent";
|
|
import {
|
|
AnalyticsFlowNameConstant,
|
|
AnalyticsGlobalErrorTypeConstant,
|
|
AnalyticsMethodNameConstant,
|
|
AnalyticsModuleNameConstant,
|
|
} from "../constants/AnalyticsEventsConstant";
|
|
|
|
export const ScreenActionHandler = {
|
|
handleScreenAction: (
|
|
screenMetaData: ActionMetaData,
|
|
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
|
screenData?: ScreenData | null,
|
|
navigation?: any
|
|
) => {
|
|
switch (screenMetaData.actionType) {
|
|
case ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND: {
|
|
return get<ApiResponse<CtaData>>(
|
|
`v3/quotes/${screenMetaData.data.quoteId}`,
|
|
getXTargetHeaderInfo(GI.toLocaleUpperCase())
|
|
)
|
|
.then((response) => {
|
|
const updatedScreenData: ScreenData = {
|
|
...(response.data as ScreenData),
|
|
screenState: ScreenState.LOADED,
|
|
};
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
})
|
|
.catch((error) => {
|
|
logToSentry(
|
|
`No response from api call: ${screenMetaData.actionType} | Error: ${error} | MethodName: handleScreenAction`
|
|
);
|
|
const errorEvent: GlobalErrorData = {
|
|
reason: error.toString(),
|
|
moduleName: AnalyticsModuleNameConstant.GI,
|
|
flowName: AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
|
methodName:
|
|
AnalyticsMethodNameConstant.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
|
|
globalErrorType:
|
|
AnalyticsGlobalErrorTypeConstant.GLOBAL_INTERNAL_ERRORS,
|
|
isAppDowntimeEvent: true,
|
|
};
|
|
sendAsGlobalErrorEvent(errorEvent);
|
|
const updatedScreenData: ScreenData = {
|
|
screenState: ScreenState.ERROR,
|
|
errorMetaData: {
|
|
baseActionType: BaseActionTypes.SCREEN_ACTION,
|
|
metaData: [
|
|
{
|
|
actionType: ScreenActionTypes.SHOW_LOADER,
|
|
},
|
|
{
|
|
data: screenMetaData.data,
|
|
actionType:
|
|
ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
setScreenData(updatedScreenData);
|
|
});
|
|
}
|
|
case ScreenActionTypes.FETCH_QUOTE_V3: {
|
|
return post<ApiResponse<CtaData>>(
|
|
"v3/quotes",
|
|
screenMetaData.data,
|
|
getXTargetHeaderInfo(GI.toLocaleUpperCase())
|
|
)
|
|
.then((response) => {
|
|
console.log("Quote_v3_Call success", response.data);
|
|
if (screenData?.screenState) {
|
|
screenData.screenState = ScreenState.LOADING;
|
|
}
|
|
if (response.data) {
|
|
const cta = response.data as CtaData;
|
|
// By default routing is happening via Base screen hence below navigate sends the cta there to get routed to specific screenName based on Cta
|
|
navigation.navigate(BASE_SCREEN, { ctaData: cta });
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
logToSentry(
|
|
`No response from api call: ${screenMetaData.actionType} | Error: ${error} | MethodName: handleScreenAction}`
|
|
);
|
|
const errorEvent: GlobalErrorData = {
|
|
reason: error.toString(),
|
|
moduleName: AnalyticsModuleNameConstant.GI,
|
|
flowName: AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
|
methodName: AnalyticsMethodNameConstant.FETCH_QUOTE_V3,
|
|
globalErrorType:
|
|
AnalyticsGlobalErrorTypeConstant.GLOBAL_INTERNAL_ERRORS,
|
|
isAppDowntimeEvent: true,
|
|
};
|
|
sendAsGlobalErrorEvent(errorEvent);
|
|
const updatedScreenData: ScreenData = {
|
|
screenState: ScreenState.ERROR,
|
|
errorMetaData: {
|
|
baseActionType: BaseActionTypes.SCREEN_ACTION,
|
|
metaData: [
|
|
{
|
|
actionType: ScreenActionTypes.SHOW_LOADER,
|
|
},
|
|
{
|
|
data: screenMetaData.data,
|
|
actionType: ScreenActionTypes.FETCH_QUOTE_V3,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
});
|
|
}
|
|
case ScreenActionTypes.SHOW_LOADER: {
|
|
const updatedScreenData: ScreenData = {
|
|
...screenData,
|
|
screenState: ScreenState.LOADING,
|
|
};
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
}
|
|
default:
|
|
return;
|
|
}
|
|
},
|
|
};
|