112 lines
3.1 KiB
TypeScript
112 lines
3.1 KiB
TypeScript
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
import { BUY_INSURANCE_SCREEN, COMPARE_PLAN_SCREEN, QUOTE_OFFER_SCREEN } from "../constants/ScreenNameConstants";
|
|
import { ScreenData } from "../interface/widgets/screenData/ScreenData";
|
|
import { NetworkConnectorModule } from "../../common/native-module/NativeModules";
|
|
import { BUILD_CONFIG_DETAILS } from "../constants/StringConstant";
|
|
import { CtaData } from "../interface";
|
|
|
|
export const getScreenDataFromCache = async (
|
|
key: string
|
|
): Promise<ScreenData | null> => {
|
|
try {
|
|
const value = await AsyncStorage.getItem(key);
|
|
if (!!value) {
|
|
// The key exists in AsyncStorage
|
|
const deserializedObject = JSON.parse(value);
|
|
try {
|
|
return deserializedObject as ScreenData;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
console.error("Error checking AsyncStorage:", error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
export const saveScreenDataInCache = async (
|
|
key: string | null,
|
|
data: ScreenData | null
|
|
) => {
|
|
if (!!key && !!data) {
|
|
const serializedObject = JSON.stringify(data);
|
|
await AsyncStorage.setItem(key, serializedObject);
|
|
} else {
|
|
console.error("key or data was invalid");
|
|
}
|
|
};
|
|
|
|
export const getCacheKey = (
|
|
screenName: string | null | undefined,
|
|
screenKey: string | null | undefined
|
|
) => {
|
|
if (!!screenKey && !!screenName) {
|
|
return screenName + "#" + screenKey;
|
|
} else {
|
|
return screenName;
|
|
}
|
|
};
|
|
|
|
export const setBuildConfigDetails = async () => {
|
|
let buildConfigData: string | undefined
|
|
await NetworkConnectorModule.getBuildConfigDetails().then((response: string) => {
|
|
buildConfigData = response
|
|
});
|
|
if (buildConfigData) {
|
|
await AsyncStorage.setItem(BUILD_CONFIG_DETAILS, buildConfigData);
|
|
}
|
|
}
|
|
|
|
export const getBuildConfigDetails = async (
|
|
): Promise<BuildConfigDetails | null> => {
|
|
const value = await AsyncStorage.getItem(BUILD_CONFIG_DETAILS);
|
|
try {
|
|
if (!!value) {
|
|
// The key exists in AsyncStorage
|
|
const buildConfigData = JSON.parse(value);
|
|
try {
|
|
return buildConfigData as BuildConfigDetails;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
// Sentry log -> Error checking BuildConfigDetails
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const isScreenWhiteListedForCaching = (
|
|
screenName: string | null | undefined
|
|
) => {
|
|
return !screensWithCachingDisabled.includes(screenName || "");
|
|
};
|
|
|
|
export const screensWithCachingDisabled = [BUY_INSURANCE_SCREEN, QUOTE_OFFER_SCREEN, COMPARE_PLAN_SCREEN];
|
|
|
|
|
|
export const invalidateCacheWithUrl = (
|
|
screenUrl: string | null | undefined
|
|
) => {
|
|
return screenUrlCacheList.includes(screenUrl || "");
|
|
};
|
|
|
|
export const screenUrlCacheList = ["gi/insurance_container/pre_quote_journey", "react_native/gi/insurance_container/pre_quote_journey"];
|
|
|
|
|
|
export const shouldInvalidateSavedData = (
|
|
cta: CtaData
|
|
): boolean => {
|
|
cta.parameters?.forEach((item) => {
|
|
if (item.key === 'invalidateCache' && item.value === 'true') {
|
|
return true;
|
|
}
|
|
})
|
|
return false;
|
|
};
|