320 lines
10 KiB
TypeScript
320 lines
10 KiB
TypeScript
import { Dispatch, SetStateAction } from "react";
|
|
import {
|
|
SumInsuredRequestData,
|
|
updateSumInsuredData,
|
|
} from "../../../Container/Navi-Insurance/network/QuotePageApi";
|
|
import {
|
|
ActionMetaData,
|
|
GenericActionPayload,
|
|
TargetWidgetPayload,
|
|
} from "../../actions/GenericAction";
|
|
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 { FinalPatchCallRequestBody } from "../../interface/widgets/widgetData/FooterWithCardWidgetData";
|
|
import { NativeDeeplinkNavigatorModule } from "../../native-module/NativeModules";
|
|
import { ScreenState } from "../../screen/BaseScreen";
|
|
import { getQuoteIdFromCta } from "../../utilities/CtaParamsUtils";
|
|
import { updateValueByKeyPath } from "../../utilities/MiscUtils";
|
|
import { parseValue } from "../../utilities/SerializerUtil";
|
|
import { WidgetActionTypes } from "./WidgetActionTypes";
|
|
import {
|
|
getStringPreference,
|
|
setStringPreference,
|
|
} from "../../utilities/SharedPreferenceUtils";
|
|
|
|
const WidgetActionHandler = {
|
|
handleWidgetAction: (
|
|
widgetMetaData: ActionMetaData,
|
|
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
|
setErrorMetaData:
|
|
| Dispatch<SetStateAction<ActionMetaData[] | null>>
|
|
| undefined,
|
|
screenData?: ScreenData | null,
|
|
ctaData?: CtaData,
|
|
navigation?: any,
|
|
) => {
|
|
switch (widgetMetaData.actionType) {
|
|
case WidgetActionTypes.UPDATE_WIDGET_DATA: {
|
|
if (!!screenData) {
|
|
const updatedScreenData = { ...screenData } as ScreenData;
|
|
widgetMetaData?.data?.forEach(
|
|
(targetWidgetPayload: any, _: number) => {
|
|
if (
|
|
(targetWidgetPayload as TargetWidgetPayload).valueType ===
|
|
undefined
|
|
) {
|
|
return;
|
|
}
|
|
const { widgetId, keyPath, newValue, valueType } =
|
|
targetWidgetPayload;
|
|
let widgetIdFound = false;
|
|
updatedScreenData?.screenWidgets?.contentWidgets?.forEach(
|
|
widget => {
|
|
if (widget.widgetId === widgetId && keyPath === "") {
|
|
if (newValue === "false") {
|
|
widget.widgetVisibility = false;
|
|
return;
|
|
}
|
|
widget.widgetVisibility = true;
|
|
}
|
|
if (widget?.widgetId === widgetId && !!keyPath) {
|
|
updateValueByKeyPath(
|
|
widget.widgetData,
|
|
keyPath,
|
|
parseValue(newValue, valueType),
|
|
);
|
|
setScreenData(updatedScreenData);
|
|
widgetIdFound = true;
|
|
return;
|
|
}
|
|
},
|
|
);
|
|
!widgetIdFound
|
|
? updatedScreenData?.screenWidgets?.footerWidgets?.forEach(
|
|
widget => {
|
|
if (widget.widgetId === widgetId && keyPath === "") {
|
|
if (newValue === "false") {
|
|
widget.widgetVisibility = false;
|
|
return;
|
|
}
|
|
widget.widgetVisibility = true;
|
|
}
|
|
if (widget?.widgetId === widgetId && !!keyPath) {
|
|
updateValueByKeyPath(
|
|
widget.widgetData,
|
|
keyPath,
|
|
parseValue(newValue, valueType),
|
|
);
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
}
|
|
},
|
|
)
|
|
: {};
|
|
!widgetIdFound
|
|
? updatedScreenData?.screenWidgets?.headerWidgets?.forEach(
|
|
widget => {
|
|
if (widget.widgetId === widgetId && keyPath === "") {
|
|
if (newValue === "false") {
|
|
widget.widgetVisibility = false;
|
|
return;
|
|
}
|
|
widget.widgetVisibility = true;
|
|
}
|
|
if (widget?.widgetId === widgetId && !!keyPath) {
|
|
updateValueByKeyPath(
|
|
widget.widgetData,
|
|
keyPath,
|
|
parseValue(newValue, valueType),
|
|
);
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
}
|
|
},
|
|
)
|
|
: {};
|
|
},
|
|
);
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
|
|
case WidgetActionTypes.PATCH_QUOTE_V2: {
|
|
let quoteId = getQuoteIdFromCta(ctaData);
|
|
const requestData: SumInsuredRequestData = widgetMetaData.data;
|
|
if (!!quoteId) {
|
|
getQuoteIdFromCache().then((value) => {
|
|
if (!!value) {
|
|
quoteId = value;
|
|
return updateSumInsuredData(requestData, quoteId!!);
|
|
}
|
|
return;
|
|
});
|
|
return;
|
|
} else {
|
|
return updateSumInsuredData(requestData, quoteId!!);
|
|
}
|
|
}
|
|
|
|
case WidgetActionTypes.FINAL_PATCH_CALL: {
|
|
setScreenData({
|
|
...screenData,
|
|
screenState: ScreenState.OVERLAY,
|
|
});
|
|
invalidatePostApiData();
|
|
let quoteId = getQuoteIdFromCta(ctaData);
|
|
const requestData: SumInsuredRequestData = (
|
|
widgetMetaData?.data as FinalPatchCallRequestBody
|
|
).requestData;
|
|
const nextPageCta: CtaData = (
|
|
widgetMetaData?.data as FinalPatchCallRequestBody
|
|
).nextPageCta;
|
|
if (!quoteId) {
|
|
getQuoteIdFromCache().then((value) => {
|
|
if (!!value) {
|
|
quoteId = value;
|
|
return updateSumInsuredData(requestData, quoteId!!)
|
|
.then((response) => {
|
|
handleResponseData(nextPageCta, setScreenData, screenData);
|
|
})
|
|
.catch((error) => {
|
|
handleErrorData(
|
|
error,
|
|
setScreenData,
|
|
widgetMetaData,
|
|
AnalyticsMethodNameConstant.FINAL_PATCH_CALL,
|
|
screenData
|
|
);
|
|
});
|
|
}
|
|
return;
|
|
});
|
|
} else {
|
|
return updateSumInsuredData(requestData, quoteId!!)
|
|
.then((response) => {
|
|
handleResponseData(nextPageCta, setScreenData, screenData);
|
|
})
|
|
.catch((error) => {
|
|
handleErrorData(
|
|
error,
|
|
setScreenData,
|
|
widgetMetaData,
|
|
AnalyticsMethodNameConstant.FINAL_PATCH_CALL,
|
|
screenData
|
|
);
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
case WidgetActionTypes.SHOW_LOADER: {
|
|
const updatedScreenData: ScreenData = {
|
|
...screenData,
|
|
screenState: ScreenState.LOADING,
|
|
};
|
|
setScreenData(updatedScreenData);
|
|
return;
|
|
}
|
|
case WidgetActionTypes.ANALYTIC_ACTION: {
|
|
widgetMetaData?.analyticsEventProperties && sendAsAnalyticsEvent(widgetMetaData?.analyticsEventProperties)
|
|
return
|
|
}
|
|
|
|
default: {
|
|
return;
|
|
}
|
|
}
|
|
},
|
|
|
|
getTargetWidgetActionPayload: (
|
|
value: any | undefined | null,
|
|
actionPayloadList: GenericActionPayload | undefined,
|
|
): GenericActionPayload | undefined => {
|
|
if (!actionPayloadList) {
|
|
return undefined;
|
|
}
|
|
|
|
let updatedActionPayload: GenericActionPayload = {
|
|
...actionPayloadList,
|
|
metaData: actionPayloadList.metaData?.map(actionPayload => {
|
|
let updatedList = actionPayload?.data?.map(
|
|
(targetWidgetPayload: any) => {
|
|
if (
|
|
(targetWidgetPayload as TargetWidgetPayload).valueType ===
|
|
undefined
|
|
) {
|
|
return targetWidgetPayload;
|
|
}
|
|
let newValue = value;
|
|
if (
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
!Array.isArray(value) &&
|
|
targetWidgetPayload?.actionId &&
|
|
value[targetWidgetPayload.actionId]
|
|
) {
|
|
newValue = value[targetWidgetPayload.actionId];
|
|
}
|
|
return {
|
|
...targetWidgetPayload,
|
|
newValue: newValue,
|
|
};
|
|
},
|
|
);
|
|
const widgetActionMeta: ActionMetaData = {
|
|
actionType: actionPayload.actionType,
|
|
data: updatedList,
|
|
};
|
|
return widgetActionMeta;
|
|
}),
|
|
};
|
|
return updatedActionPayload;
|
|
},
|
|
};
|
|
|
|
const getQuoteIdFromCache = async () => {
|
|
return await getStringPreference("QUOTE_ID", "string");
|
|
};
|
|
|
|
const invalidatePostApiData = () => {
|
|
return setStringPreference("POST_API_CALLED", "false");
|
|
};
|
|
|
|
const handleResponseData = (
|
|
nextPageCta: CtaData,
|
|
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
|
screenData?: ScreenData | null
|
|
) => {
|
|
const updatedNextPageCta: CtaData = {
|
|
...nextPageCta,
|
|
finish: nextPageCta.finish ? nextPageCta.finish : false,
|
|
};
|
|
NativeDeeplinkNavigatorModule.navigateToNaviDeeplinkNavigator(
|
|
JSON.stringify(updatedNextPageCta)
|
|
);
|
|
setScreenData({
|
|
...screenData,
|
|
screenState: ScreenState.LOADED,
|
|
});
|
|
return;
|
|
};
|
|
|
|
const handleErrorData = (
|
|
error: any,
|
|
setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
|
|
widgetMetaData: ActionMetaData,
|
|
methodName: string,
|
|
screenData?: ScreenData | null
|
|
) => {
|
|
logToSentry(
|
|
`No response from api call: ${widgetMetaData.actionType} | Error: ${error} | MethodName: handleWidgetAction}`
|
|
);
|
|
const errorEvent: GlobalErrorData = {
|
|
reason: `${error.message}, axiosError: ${error.axiosCode}`,
|
|
statusCode: error.statusCode,
|
|
moduleName: AnalyticsModuleNameConstant.GI,
|
|
flowName: AnalyticsFlowNameConstant.GI_RN_QUOTE,
|
|
methodName: methodName,
|
|
globalErrorType: AnalyticsGlobalErrorTypeConstant.GLOBAL_INTERNAL_ERRORS,
|
|
isAppDowntimeEvent: true,
|
|
};
|
|
sendAsGlobalErrorEvent(errorEvent);
|
|
setScreenData({
|
|
...screenData,
|
|
screenState: ScreenState.LOADED,
|
|
});
|
|
return;
|
|
};
|
|
|
|
export default WidgetActionHandler;
|