import { User } from './interface'; import { forEach, split, startCase } from 'lodash'; // @dynamic export class Utils { static timer; static guid() { const s4 = () => { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); }; return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); } static getUser(): User { const userConfig = localStorage.getItem('aeapUser'); const user = userConfig ? JSON.parse(userConfig) : null; if (user) { delete user.applications; user.shortUserName = user.firstName.substring(0, 1).toUpperCase() + user.lastName.substring(0, 1).toUpperCase(); } return user; } static getAppConfig(): any { const userConfig = localStorage.getItem('aeapUser'); const tempUseConfig = userConfig ? JSON.parse(userConfig) : null; const appConfig = tempUseConfig ? tempUseConfig.applications[0] : null; return appConfig; } static getTranslation(): any { const userConfig: { applications: [any] } = JSON.parse(localStorage.getItem('aeapUser')); let translation = {}; if (userConfig) { const defaultApp: { i18n: ''; applicationName: '' } = userConfig.applications[0]; const i18n = defaultApp.i18n; translation = i18n['translations']; const appName = Utils.getHeaderTitle(); translation['header'] = appName === 'Login' || appName === '' ? translation[defaultApp.applicationName] : appName; } return translation; } static debounce(callback, wait: number, argument: any[]): void { clearTimeout(Utils.timer); Utils.timer = setTimeout(() => callback.apply(null, argument), wait); } static getRouterLink(url: string): string { const pathName = window.location.pathname.split('/')[1]; return `${pathName}/${url}`; } static getHeaderTitle(): string { const path = window.location.pathname; const appName = startCase(split(path, '/', 2)[1]); return appName; } static showError(fn, error) { console.log(`${fn} :: ${error}`); } static addLog(logType = "", fnName:string, error: any) { const debugMode = localStorage.DEBUG_MODE ? localStorage.DEBUG_MODE.toLowerCase() : ''; if (debugMode === "error" && logType.toLowerCase() === "error") { console.log(`${fnName}:`, error); } if (debugMode === "warning" && logType.toLowerCase() === "warning") { console.warn(`${fnName}:`, error ); } } static convertToMini(num): string { let returner = ''; if (num === 0) { return num; } if (num <= 999) { returner = num; } if (num > 999 && num < 1000000) { returner = (num / 1000).toFixed(1) + 'K'; // convert to K for number from > 1000 < 1 million } else if (num >= 1000000 && num < 1000000000) { returner = (num / 1000000).toFixed(1) + 'M'; // convert to M for number from > 1 million } else if (num >= 1000000000) { returner = (num / 1000000000).toFixed(1) + 'B'; // convert to M for number from > 1 million } else if (num < 900) { const decimal = /^[-+]?[0-9]+\.[0-9]+$/; if (num?.match(decimal)) { returner = (num / 1)?.toFixed(1); } else { returner = num; } } if (returner?.includes('.0')) { const symbol = returner.replace(/[0-9]/g, '').split('.')[1]; returner = `${parseInt(returner, 0).toFixed(0)}${symbol}`; } return returner; } static convertToMiniCp(num): string { let returner = ''; if (num === 0) { return num; } if (num < 999) { returner = num; } if (num > 999 && num < 1000000) { returner = (num / 1000).toFixed(1) + 'K'; // convert to K for number from > 1000 < 1 million } else if (num >= 1000000) { returner = (num / 1000000).toFixed(1) + 'M'; // convert to M for number from > 1 million } else if (num >= 1000000000) { returner = (num / 1000000000).toFixed(1) + 'B'; // convert to B for number from > 1 Billion } return returner; } static getCurrentDate(): string { const date = new Date(); const day = date.getDate(); const month = date.getMonth() + 1; const year = date.getFullYear(); return `${month}/${day}/${year}`; } static isMobile() { if (/mobile/i.test(navigator.userAgent) && !/ipad|tablet/i.test(navigator.userAgent)) { return true; } else { return false; } } static returnConvertedAndFormattedNumber(data, formatDigits, isFormatted) { if (data === undefined || data === null) { return ''; } const fixedValue = Number(data).toFixed(formatDigits); if (isFormatted) { return Number(fixedValue).toLocaleString('en-GB'); } else { return fixedValue; } } static getCurrentMonth(): string { const date = new Date(); const month = date.getMonth() + 1; const year = date.getFullYear(); return `${month}/${year}`; } static getCurrentTime(timeData) { let edge; let currentTime; for (edge = 0; edge < timeData.edges.length; edge++) { if (timeData.edges[edge].node.measureValues[0] === '1') { currentTime = timeData.edges[edge].node.dimensionMembers[0].name; } } return currentTime; } static getErrorHandledMessage(error, i18n): string { if(error.graphQLErrors?.length > 0) { forEach(error.graphQLErrors, (error) => { console.log(error.message); }); let message = i18n && i18n['error-codes'][error.graphQLErrors[0].extensions?.code] ? i18n['error-codes'][error.graphQLErrors[0].extensions.code]: 'contact-admin'; error.graphQLErrors[0].extensions?.parameters?.forEach((param, index) => { message = message.replace('{'+ index +'}', param) }) return message; } else { return i18n[error]; } } static getDeleteToastConfig(i18n){ return { data: { buttons: [{ name: i18n['delete'], action: config => console.log(config, 'buttons toast click event') }], links: [{ name: i18n['cancel'], action: config => console.log(config, 'multi toast link click event') }], infoText: { show: true, text: i18n['confirm-delete-message'], }, }, isModal: false, style: 'multi-button-toast-primary-background', position: { top: false, bottom: true, left: false, right: false}, type: 'multiButtonToast', } } }