TP-79772 | RN ScreenActionHandler Code cleanup (#12629)
Co-authored-by: Prajjaval Verma <prajjaval.verma@navi.com>
This commit is contained in:
committed by
GitHub
parent
81b1a739d6
commit
557e0b8d55
@@ -0,0 +1,39 @@
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import { getXTargetHeaderInfo } from "../../../../network/ApiClient";
|
||||
import { get } from "../../../../network/NetworkService";
|
||||
import { ActionMetaData } from "../../../common/actions/GenericAction";
|
||||
import {
|
||||
AnalyticsFlowNameConstant,
|
||||
AnalyticsMethodNameConstant,
|
||||
GI,
|
||||
} from "../../../common/constants";
|
||||
import { CtaData } from "../../../common/interface";
|
||||
import { ScreenData } from "../../../common/interface/widgets/screenData/ScreenData";
|
||||
import {
|
||||
handleErrorData,
|
||||
handleResponseData,
|
||||
} from "../../../common/screen/ScreenActionHandler";
|
||||
import { ScreenActionTypes } from "../../../common/screen/ScreenActionTypes";
|
||||
export const getMarketBenefitComparePageData = async (
|
||||
screenMetaData: ActionMetaData,
|
||||
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
||||
) => {
|
||||
const url = "market-benefit-compare";
|
||||
return get<ApiResponse<CtaData>>(
|
||||
url,
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
)
|
||||
.then(response => {
|
||||
handleResponseData(setScreenData, response);
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsFlowNameConstant.GI_RN_BENEFIT_COMPARE,
|
||||
AnalyticsMethodNameConstant.MARKET_BENEFIT_COMPARE_LIST,
|
||||
ScreenActionTypes.FETCH_BENEFIT_COMPARE_LIST,
|
||||
);
|
||||
});
|
||||
};
|
||||
@@ -1,13 +1,126 @@
|
||||
import { AxiosRequestConfig } from "axios";
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import { getXTargetHeaderInfo } from "../../../../network/ApiClient";
|
||||
import { get, post, patch } from "../../../../network/NetworkService";
|
||||
import { GZIP } from "../../../../network/NetworkConstant";
|
||||
import { get, patch, post } from "../../../../network/NetworkService";
|
||||
import {
|
||||
ActionMetaData,
|
||||
BaseActionTypes,
|
||||
} from "../../../common/actions/GenericAction";
|
||||
import {
|
||||
AnalyticsFlowNameConstant,
|
||||
AnalyticsMethodNameConstant,
|
||||
AnalyticsModuleNameConstant,
|
||||
BASE_SCREEN,
|
||||
} from "../../../common/constants";
|
||||
import { GI } from "../../../common/constants/NavigationHandlerConstants";
|
||||
import {
|
||||
sendAsAnalyticsEvent,
|
||||
sendAsGlobalErrorEvent,
|
||||
} from "../../../common/hooks/useAnalyticsEvent";
|
||||
import { logToSentry } from "../../../common/hooks/useSentryLogging";
|
||||
import { CtaData } from "../../../common/interface";
|
||||
import { ScreenData } from "../../../common/interface/widgets/screenData/ScreenData";
|
||||
|
||||
import { ScreenState } from "../../../common/screen/BaseScreen";
|
||||
import {
|
||||
handleErrorData,
|
||||
handleResponseData,
|
||||
} from "../../../common/screen/ScreenActionHandler";
|
||||
import { ScreenActionTypes } from "../../../common/screen/ScreenActionTypes";
|
||||
import { getErrorTypeFromStatusCode } from "../../../common/utilities/ErrorUtils";
|
||||
export interface SumInsuredRequestData {
|
||||
sumInsured: string;
|
||||
planId?: string;
|
||||
}
|
||||
|
||||
export const createQuote = async (
|
||||
screenData: ScreenData | null | undefined,
|
||||
screenMetaData: ActionMetaData,
|
||||
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
||||
navigation: any,
|
||||
) => {
|
||||
const url = "v4/quotes";
|
||||
const data = screenMetaData.data;
|
||||
const requestConfig: AxiosRequestConfig = {
|
||||
headers: {
|
||||
"Accept-Encoding": GZIP,
|
||||
"X-Target": GI.toLocaleUpperCase(),
|
||||
},
|
||||
};
|
||||
return post<ApiResponse<CtaData>>(url, screenMetaData.data, requestConfig)
|
||||
.then(response => {
|
||||
if (screenData?.screenState) {
|
||||
setScreenData({
|
||||
...screenData,
|
||||
screenState: ScreenState.LOADING,
|
||||
});
|
||||
}
|
||||
if (response) {
|
||||
const updatedScreenData: ScreenData = {
|
||||
...(response.data as ScreenData),
|
||||
screenState: ScreenState.LOADED,
|
||||
};
|
||||
// If redirectionCta is present and screenWidgets is not present then navigate to Base screen to get routed to specific screenName based on Cta
|
||||
const { redirectionCta, analyticsEvent } =
|
||||
updatedScreenData.screenMetaData || {};
|
||||
|
||||
// If redirectionCta exists and screenWidgets is absent, navigate to Base screen
|
||||
if (redirectionCta && !updatedScreenData.screenWidgets) {
|
||||
handleRedirection(navigation, redirectionCta);
|
||||
} else {
|
||||
if (analyticsEvent) {
|
||||
sendAsAnalyticsEvent(analyticsEvent);
|
||||
}
|
||||
setScreenData(updatedScreenData);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
||||
AnalyticsMethodNameConstant.FETCH_QUOTE_V4,
|
||||
ScreenActionTypes.FETCH_QUOTE_V4,
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const handleRedirection = (navigation: any, redirectionCta: any) => {
|
||||
if (redirectionCta.analyticsEventProperties) {
|
||||
sendAsAnalyticsEvent(redirectionCta.analyticsEventProperties);
|
||||
}
|
||||
navigation.navigate(BASE_SCREEN, { ctaData: redirectionCta });
|
||||
};
|
||||
|
||||
export const getQuotePageData = async (
|
||||
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
||||
screenMetaData: ActionMetaData,
|
||||
) => {
|
||||
let quoteId = screenMetaData.data.quoteId;
|
||||
const requestConfig: AxiosRequestConfig = {
|
||||
headers: {
|
||||
"X-Target": GI.toLocaleUpperCase(),
|
||||
},
|
||||
};
|
||||
const url = `v3/quotes/${quoteId}`;
|
||||
return get<ApiResponse<CtaData>>(url, requestConfig)
|
||||
.then(response => {
|
||||
handleResponseData(setScreenData, response);
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
||||
AnalyticsMethodNameConstant.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
|
||||
ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
export const updateSumInsuredData = async (
|
||||
data: SumInsuredRequestData,
|
||||
quoteId: string,
|
||||
@@ -19,3 +132,31 @@ export const updateSumInsuredData = async (
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
);
|
||||
};
|
||||
|
||||
export const fetchComparisonPlanList = async (
|
||||
screenMetaData: ActionMetaData,
|
||||
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
||||
) => {
|
||||
return post<ApiResponse<CtaData>>(
|
||||
"quotes/compare-plans",
|
||||
screenMetaData.data,
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
)
|
||||
.then(response => {
|
||||
const updatedScreenData: ScreenData = {
|
||||
...(response.data as ScreenData),
|
||||
screenState: ScreenState.LOADED,
|
||||
};
|
||||
setScreenData(updatedScreenData);
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
||||
AnalyticsMethodNameConstant.COMPARE_PLAN_LIST,
|
||||
ScreenActionTypes.FETCH_COMPARE_PLAN_LIST,
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
2
App/Container/Navi-Insurance/network/index.ts
Normal file
2
App/Container/Navi-Insurance/network/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./MarketBenefitComparePageApi";
|
||||
export * from "./QuotePageApi";
|
||||
@@ -18,6 +18,7 @@ export const AnalyticsEventPropertyConstants = {
|
||||
|
||||
export const AnalyticsFlowNameConstant = {
|
||||
GI_RN_QUOTE: "GiRnQuote",
|
||||
GI_RN_BENEFIT_COMPARE: "GiRnBenefitCompare",
|
||||
REACT_NATIVE: "ReactNative",
|
||||
};
|
||||
|
||||
|
||||
@@ -1,28 +1,20 @@
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import {
|
||||
getAcceptHeaderInfo,
|
||||
getXTargetHeaderInfo,
|
||||
} from "../../../network/ApiClient";
|
||||
import { GZIP } from "../../../network/NetworkConstant";
|
||||
import { get, post } from "../../../network/NetworkService";
|
||||
import { mergeHeaders } from "../../../network/NetworkUtils";
|
||||
createQuote,
|
||||
fetchComparisonPlanList,
|
||||
getMarketBenefitComparePageData,
|
||||
getQuotePageData,
|
||||
} from "../../Container/Navi-Insurance/network";
|
||||
import { ActionMetaData, BaseActionTypes } from "../actions/GenericAction";
|
||||
import { BASE_SCREEN, GI } from "../constants";
|
||||
import {
|
||||
AnalyticsFlowNameConstant,
|
||||
AnalyticsGlobalErrorTypeConstant,
|
||||
AnalyticsMethodNameConstant,
|
||||
AnalyticsModuleNameConstant,
|
||||
} from "../constants/AnalyticsEventsConstant";
|
||||
import {
|
||||
sendAsAnalyticsEvent,
|
||||
sendAsGlobalErrorEvent,
|
||||
} from "../hooks/useAnalyticsEvent";
|
||||
import { logToSentry } from "../hooks/useSentryLogging";
|
||||
import { CtaData } from "../interface";
|
||||
import { ScreenData } from "../interface/widgets/screenData/ScreenData";
|
||||
import { ScreenState } from "./BaseScreen";
|
||||
import { ScreenActionTypes } from "./ScreenActionTypes";
|
||||
import {
|
||||
AnalyticsModuleNameConstant,
|
||||
AnalyticsFlowNameConstant,
|
||||
} from "../constants";
|
||||
import { sendAsGlobalErrorEvent } from "../hooks/useAnalyticsEvent";
|
||||
import { logToSentry } from "../hooks/useSentryLogging";
|
||||
import { getErrorTypeFromStatusCode } from "../utilities/ErrorUtils";
|
||||
|
||||
export const ScreenActionHandler = {
|
||||
@@ -34,146 +26,21 @@ export const ScreenActionHandler = {
|
||||
) => {
|
||||
switch (screenMetaData.actionType) {
|
||||
case ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND: {
|
||||
let quoteId = screenMetaData.data.quoteId;
|
||||
return get<ApiResponse<CtaData>>(
|
||||
`v3/quotes/${quoteId}`,
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
)
|
||||
.then(response => {
|
||||
handleResponseData(setScreenData, response);
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsMethodNameConstant.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
|
||||
ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND,
|
||||
);
|
||||
});
|
||||
}
|
||||
case ScreenActionTypes.FETCH_QUOTE_V3: {
|
||||
return post<ApiResponse<CtaData>>(
|
||||
"v3/quotes",
|
||||
screenMetaData.data,
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
)
|
||||
.then(response => {
|
||||
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
|
||||
if (!!cta.analyticsEventProperties) {
|
||||
sendAsAnalyticsEvent(cta.analyticsEventProperties);
|
||||
}
|
||||
navigation.navigate(BASE_SCREEN, { ctaData: cta });
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsMethodNameConstant.FETCH_QUOTE_V3,
|
||||
ScreenActionTypes.FETCH_QUOTE_V3,
|
||||
);
|
||||
});
|
||||
return getQuotePageData(setScreenData, screenMetaData);
|
||||
}
|
||||
case ScreenActionTypes.FETCH_COMPARE_PLAN_LIST: {
|
||||
return post<ApiResponse<CtaData>>(
|
||||
"quotes/compare-plans",
|
||||
screenMetaData.data,
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
)
|
||||
.then(response => {
|
||||
const updatedScreenData: ScreenData = {
|
||||
...(response.data as ScreenData),
|
||||
screenState: ScreenState.LOADED,
|
||||
};
|
||||
setScreenData(updatedScreenData);
|
||||
return;
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsMethodNameConstant.COMPARE_PLAN_LIST,
|
||||
ScreenActionTypes.FETCH_COMPARE_PLAN_LIST,
|
||||
);
|
||||
});
|
||||
return fetchComparisonPlanList(screenMetaData, setScreenData);
|
||||
}
|
||||
case ScreenActionTypes.FETCH_QUOTE_V4: {
|
||||
const acceptHeader = getAcceptHeaderInfo(GZIP);
|
||||
const xTargetHeader = getXTargetHeaderInfo(GI.toLocaleUpperCase());
|
||||
return post<ApiResponse<CtaData>>(
|
||||
"v4/quotes",
|
||||
screenMetaData.data,
|
||||
mergeHeaders({ configs: [acceptHeader, xTargetHeader] }),
|
||||
)
|
||||
.then(response => {
|
||||
if (screenData?.screenState) {
|
||||
setScreenData({
|
||||
...screenData,
|
||||
screenState: ScreenState.LOADING,
|
||||
});
|
||||
}
|
||||
if (response) {
|
||||
const updatedScreenData: ScreenData = {
|
||||
...(response.data as ScreenData),
|
||||
screenState: ScreenState.LOADED,
|
||||
};
|
||||
// If redirectionCta is present and screenWidgets is not present then navigate to Base screen to get routed to specific screenName based on Cta
|
||||
if (
|
||||
!!updatedScreenData.screenMetaData?.redirectionCta &&
|
||||
!updatedScreenData.screenWidgets
|
||||
) {
|
||||
const cta = updatedScreenData.screenMetaData?.redirectionCta;
|
||||
// By default routing is happening via Base screen hence below navigate sends the cta there to get routed to specific screenName based on Cta
|
||||
if (!!cta.analyticsEventProperties) {
|
||||
sendAsAnalyticsEvent(cta.analyticsEventProperties);
|
||||
}
|
||||
navigation.navigate(BASE_SCREEN, { ctaData: cta });
|
||||
} else {
|
||||
if (updatedScreenData?.screenMetaData?.analyticsEvent) {
|
||||
sendAsAnalyticsEvent(
|
||||
updatedScreenData.screenMetaData.analyticsEvent,
|
||||
);
|
||||
}
|
||||
setScreenData(updatedScreenData);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsMethodNameConstant.FETCH_QUOTE_V4,
|
||||
ScreenActionTypes.FETCH_QUOTE_V4,
|
||||
);
|
||||
});
|
||||
return createQuote(
|
||||
screenData,
|
||||
screenMetaData,
|
||||
setScreenData,
|
||||
navigation,
|
||||
);
|
||||
}
|
||||
case ScreenActionTypes.FETCH_BENEFIT_COMPARE_LIST: {
|
||||
return get<ApiResponse<CtaData>>(
|
||||
"market-benefit-compare",
|
||||
getXTargetHeaderInfo(GI.toLocaleUpperCase()),
|
||||
)
|
||||
.then(response => {
|
||||
handleResponseData(setScreenData, response);
|
||||
return;
|
||||
})
|
||||
.catch(error => {
|
||||
handleErrorData(
|
||||
error,
|
||||
setScreenData,
|
||||
screenMetaData,
|
||||
AnalyticsMethodNameConstant.MARKET_BENEFIT_COMPARE_LIST,
|
||||
ScreenActionTypes.FETCH_BENEFIT_COMPARE_LIST,
|
||||
);
|
||||
});
|
||||
return getMarketBenefitComparePageData(screenMetaData, setScreenData);
|
||||
}
|
||||
case ScreenActionTypes.SHOW_LOADER: {
|
||||
const updatedScreenData: ScreenData = {
|
||||
@@ -189,7 +56,7 @@ export const ScreenActionHandler = {
|
||||
},
|
||||
};
|
||||
|
||||
const handleResponseData = (
|
||||
export const handleResponseData = (
|
||||
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
||||
response: any,
|
||||
) => {
|
||||
@@ -198,13 +65,13 @@ const handleResponseData = (
|
||||
screenState: ScreenState.LOADED,
|
||||
};
|
||||
setScreenData(updatedScreenData);
|
||||
return;
|
||||
};
|
||||
|
||||
const handleErrorData = (
|
||||
export const handleErrorData = (
|
||||
error: any,
|
||||
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
||||
screenMetaData: ActionMetaData,
|
||||
flowName: string,
|
||||
methodName: string,
|
||||
actionType: string,
|
||||
) => {
|
||||
@@ -216,7 +83,7 @@ const handleErrorData = (
|
||||
source: screenMetaData.screenName || "",
|
||||
statusCode: error.statusCode,
|
||||
moduleName: AnalyticsModuleNameConstant.GI,
|
||||
flowName: AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
||||
flowName,
|
||||
methodName: methodName,
|
||||
globalErrorType: getErrorTypeFromStatusCode(error.statusCode || -1),
|
||||
isAppDowntimeEvent: false,
|
||||
@@ -238,5 +105,4 @@ const handleErrorData = (
|
||||
},
|
||||
};
|
||||
setScreenData(updatedScreenData);
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
import {
|
||||
AnalyticsEventNameConstants,
|
||||
AnalyticsFlowNameConstant,
|
||||
AnalyticsGlobalErrorTypeConstant,
|
||||
AnalyticsMethodNameConstant,
|
||||
AnalyticsModuleNameConstant,
|
||||
} from "../../constants/AnalyticsEventsConstant";
|
||||
@@ -29,10 +28,10 @@ import {
|
||||
getQuoteIdFromCta,
|
||||
getQuoteIdFromScreenMetaData,
|
||||
} from "../../utilities/CtaParamsUtils";
|
||||
import { getErrorTypeFromStatusCode } from "../../utilities/ErrorUtils";
|
||||
import { updateValueByKeyPath } from "../../utilities/MiscUtils";
|
||||
import { parseValue } from "../../utilities/SerializerUtil";
|
||||
import { WidgetActionTypes } from "./WidgetActionTypes";
|
||||
import { getErrorTypeFromStatusCode } from "../../utilities/ErrorUtils";
|
||||
|
||||
const WidgetActionHandler = {
|
||||
handleWidgetAction: (
|
||||
|
||||
@@ -1,36 +1,35 @@
|
||||
|
||||
interface QuoteOfferRequest {
|
||||
sumInsured?: string,
|
||||
term?: Term,
|
||||
pinCode?: string,
|
||||
asset?: Asset[],
|
||||
cover?: Cover[],
|
||||
quoteId?: string | null,
|
||||
buyerName?: string,
|
||||
preQuoteId?: string | null,
|
||||
policyToBeCopied?: string,
|
||||
applicationType?: string
|
||||
sumInsured?: string;
|
||||
term?: Term;
|
||||
pinCode?: string;
|
||||
asset?: Asset[];
|
||||
cover?: Cover[];
|
||||
quoteId?: string | null;
|
||||
buyerName?: string;
|
||||
preQuoteId?: string | null;
|
||||
policyToBeCopied?: string;
|
||||
applicationType?: string;
|
||||
}
|
||||
|
||||
interface Term {
|
||||
unit?: string,
|
||||
value?: string
|
||||
unit?: string;
|
||||
value?: string;
|
||||
}
|
||||
|
||||
interface Asset {
|
||||
age?: string,
|
||||
proposerRelationShip?: string,
|
||||
existingDisease?: boolean,
|
||||
name?: string,
|
||||
tagName?: string,
|
||||
dob?: string,
|
||||
id?: string,
|
||||
gender?: string,
|
||||
assetId?: string
|
||||
age?: string;
|
||||
proposerRelationShip?: string;
|
||||
existingDisease?: boolean;
|
||||
name?: string;
|
||||
tagName?: string;
|
||||
dob?: string;
|
||||
id?: string;
|
||||
gender?: string;
|
||||
assetId?: string;
|
||||
}
|
||||
|
||||
interface Cover {
|
||||
coverId?: string,
|
||||
coverVersion?: string,
|
||||
selectedOption?: string
|
||||
coverId?: string;
|
||||
coverVersion?: string;
|
||||
selectedOption?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user