236 lines
6.9 KiB
TypeScript
236 lines
6.9 KiB
TypeScript
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
import RNFetchBlob from "rn-fetch-blob";
|
|
import { Address, DOCUMENT_TYPE, IDocument, PhoneNumber, TDocumentObj } from "../../screens/caseDetails/interface";
|
|
import { getPrefixBase64Image, LocalStorageKeys, MimeType } from "../../common/Constants";
|
|
import NetInfo from "@react-native-community/netinfo";
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
|
import { stringify } from './stringifyUtils';
|
|
import address from "../form/components/Address";
|
|
import { useWindowDimensions } from 'react-native';
|
|
import { GlobalDocumentMap } from '../../../App';
|
|
import { GenericType } from '../../common/GenericTypes';
|
|
import { CaseDetail } from '../../screens/caseDetails/interface';
|
|
import { logError } from './errorUtils';
|
|
import packageJson from '../../../package.json';
|
|
import { CaseAllocationType } from '../../screens/allCases/interface';
|
|
|
|
|
|
const fs = RNFetchBlob.fs;
|
|
|
|
const RespHeaderContentTypeKey = 'Content-Type';
|
|
const DEFAULT_TEXT = '--';
|
|
const MAX_SHEET_HEIGHT_PERCENTAGE = 50;
|
|
|
|
export const RELATIVE_PATH_PREFIX = 'file://';
|
|
|
|
|
|
export const decideLoadingState = (textData: string): boolean => {
|
|
if (!textData) {
|
|
return true;
|
|
}
|
|
if (textData.includes('NaN') || textData.includes('undefined')) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
export const getCommunicationAddress = (address: Address) => {
|
|
if (!address) {
|
|
return '';
|
|
}
|
|
const { houseNumber, lineOne, lineTwo, locality, street, city, state, pinCode } = address;
|
|
return [houseNumber, lineOne, lineTwo, locality, street, city, state, pinCode].filter(element => element).join(', ');
|
|
}
|
|
|
|
export const storeImageLocallyReturnRelativePath = async (imagePath: string) => {
|
|
if (!imagePath) return;
|
|
|
|
return RNFetchBlob.config({
|
|
fileCache: true
|
|
}).fetch("GET", imagePath)
|
|
.then(resp => {
|
|
return RELATIVE_PATH_PREFIX + resp.path();
|
|
});
|
|
}
|
|
|
|
export const getBase64FromUrl = async (imagePath: string) => {
|
|
let contentType: MimeType;
|
|
return RNFetchBlob.config({
|
|
fileCache: true
|
|
}).fetch("GET", imagePath)
|
|
.then(resp => {
|
|
if (resp) {
|
|
imagePath = resp.path();
|
|
contentType = resp.respInfo?.headers?.[RespHeaderContentTypeKey];
|
|
return resp.readFile("base64");
|
|
}
|
|
})
|
|
.then(base64 => {
|
|
fs.unlink(imagePath);
|
|
if (contentType) {
|
|
return `${getPrefixBase64Image(contentType)}${base64}`;
|
|
}
|
|
return;
|
|
});
|
|
}
|
|
|
|
export const getNetInfo = async () => {
|
|
const netInfo = await NetInfo.fetch();
|
|
return netInfo;
|
|
};
|
|
|
|
export const copyToClipboard = (text: string) => {
|
|
Clipboard.setString(text);
|
|
};
|
|
|
|
export const fetchCopiedText = async () => {
|
|
const text = await Clipboard.getString();
|
|
return text;
|
|
};
|
|
|
|
export const setAsyncStorageItem = async (key: string, value: any) => {
|
|
try {
|
|
const stringifiedValue = JSON.stringify(value);
|
|
await AsyncStorage.setItem(key, stringifiedValue);
|
|
} catch(err) {
|
|
console.error('JSON stringify errored',err);
|
|
}
|
|
return;
|
|
}
|
|
|
|
export const clearAllAsyncStorage = async () => {
|
|
try {
|
|
await AsyncStorage.clear();
|
|
} catch(err) {
|
|
// todo: add click-stream event
|
|
logError(err as Error, 'Error while cleaning AsyncStorage');
|
|
console.error('Error while cleaning AsyncStorage');
|
|
}
|
|
return;
|
|
}
|
|
|
|
export const sanitizeString = (str = '') => {
|
|
return str?.trim() || DEFAULT_TEXT;
|
|
}
|
|
|
|
export function toTileCase(text : string) :string {
|
|
return text?.[0]?.toUpperCase() + text?.substring(1)?.toLowerCase() || text || '';
|
|
}
|
|
|
|
export const memoize = <T = any>(fn: Func<T>) => {
|
|
const cache = new Map();
|
|
const cached = function (this: any, val: T) {
|
|
return cache.has(val)
|
|
? cache.get(val)
|
|
: cache.set(val, fn.call(this, val)) && cache.get(val);
|
|
};
|
|
cached.cache = cache;
|
|
return cached;
|
|
};
|
|
|
|
export function getAddressString(address?: Address): string {
|
|
if (!address){
|
|
return 'Address not found'
|
|
}
|
|
return `${address.pinCode}, ${address.city} \n ${address.lineOne} \n ${address.lineTwo} \n ${toTileCase(address.source)}`
|
|
}
|
|
|
|
export function getPhoneNumberString(phoneNumber?: PhoneNumber): string {
|
|
if (!address){
|
|
return 'PhoneNumber not found'
|
|
}
|
|
return `${phoneNumber?.number} (${phoneNumber?.source})`
|
|
}
|
|
|
|
|
|
export const getDynamicBottomSheetHeightPercentageFn = (headerOffset = 100, rowHeight = 50) => {
|
|
const SCREEN_HEIGHT = useWindowDimensions().height;
|
|
|
|
return ((rowLength = 0) => {
|
|
const dynamicHeight = (((rowLength * rowHeight) + headerOffset) / SCREEN_HEIGHT) * 100;
|
|
|
|
return Math.min(dynamicHeight, MAX_SHEET_HEIGHT_PERCENTAGE);
|
|
})
|
|
}
|
|
|
|
export const saveToGlobalDocumentMap = async (caseId: string, data: TDocumentObj) => {
|
|
if (!caseId) return;
|
|
|
|
GlobalDocumentMap[caseId] = data;
|
|
await setAsyncStorageItem(
|
|
LocalStorageKeys.GLOBAL_DOCUMENT_MAP,
|
|
GlobalDocumentMap,
|
|
);
|
|
};
|
|
|
|
export const allSettled = (promises: Promise<GenericType>[]) =>
|
|
Promise.all(
|
|
promises.map(p => p.then(value => ({ status: 'fulfilled', value})).catch(value => ({ status: 'rejected', value}))),
|
|
);
|
|
|
|
export function isNullOrUndefined(val: any): boolean {
|
|
return val === undefined || val === null
|
|
}
|
|
|
|
export const getLoanAccountNumber = (caseDetail: CaseDetail) => {
|
|
const { loanAccountNumber, loanDetails } = caseDetail ?? {};
|
|
return loanAccountNumber ?? loanDetails?.loanAccountNumber ?? '';
|
|
}
|
|
|
|
export function getAppVersion(): string {
|
|
return packageJson.version;
|
|
}
|
|
|
|
export const getDocumentList = (caseDetails: CaseDetail) => {
|
|
return caseDetails.caseType === CaseAllocationType.ADDRESS_VERIFICATION_CASE ?
|
|
caseDetails.customerInfo?.documents :
|
|
caseDetails.documents;
|
|
}
|
|
|
|
export const findDocumentByDocumentType = (documentList: IDocument[] = [], documentType: DOCUMENT_TYPE) => {
|
|
return documentList?.find((documentItem) => documentItem.type === documentType);
|
|
}
|
|
|
|
export const checkS3Url = async(url: string): Promise<boolean> => {
|
|
try {
|
|
const response = await fetch(url, { method: "HEAD" });
|
|
return response.ok;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function debounce(func : any, timeout = 1000){
|
|
let timer: number;
|
|
return (...args: any) => {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(() => { func.apply(this, args); }, timeout);
|
|
};
|
|
}
|
|
|
|
function memoizeValue<T extends (...args: any[]) => any>(func: T): T {
|
|
const cache: Record<string, any> = {};
|
|
return function(...args: Parameters<T>): ReturnType<T> {
|
|
const key = JSON.stringify(args);
|
|
if (cache[key]) {
|
|
return cache[key];
|
|
}
|
|
const result = func.apply(this, args);
|
|
cache[key] = result;
|
|
return result;
|
|
} as T;
|
|
}
|
|
|
|
export const getKeyByValue = memoizeValue(function(obj: Record<string, string>, value: string) {
|
|
for (let key in obj) {
|
|
const regex = /\{.*?\}/g;
|
|
const str = obj[key].replace(regex, "");
|
|
if (value.includes(str)) {
|
|
return key;
|
|
}
|
|
}
|
|
});
|
|
|
|
export const getEventNameFromAPIKey = (apiKey: string, isSuccess?: boolean) => {
|
|
return `FA_${apiKey}_${isSuccess ? 'SUCCESS' : 'FAILED'}`;
|
|
} |