/** BNPL */ import { notification } from "antd" import { MESSAGE } from "../constants" import { convertCurrencyToNumber } from "./parsers" import qs from "query-string" import { CreateOfferResponse, LodgeRequest, OfferType, OrderLine, WindowSize } from "../types/common" import { BNPL_THEME_DEFAULT } from "../pages/home/type" import { PassportThemeConfig, UserProfile } from "../../../dist" export const ORDER_LINE_CATEGORY = { MOBILE_DEVICE: ["Điện thoại"], COMPUTER: ["Máy tính bảng", "Macbook"], ELECTRONIC_DEVICE: ["Đồng hồ", "Âm thanh", "Phụ kiện"], }; /** Get total amount from products */ export const getTotalAmount = (products: { [key: string]: any }) => { const getSum = (total: number, item: any) => { const { price, quantity, selected } = products[item] const priceNum = convertCurrencyToNumber(price) const amount = selected ? priceNum * quantity : 0 return total + amount } return Object.keys(products).reduce((amount, item) => getSum(amount, item), 0) } export const getTotalQuantity = (products: { [key: string]: any }) => { const getSum = (total: number, item: any) => { const { quantity, selected } = products[item] const amount = selected ? quantity : 0 return total + amount } return Object.keys(products).reduce((amount, item) => getSum(amount, item), 0) } export const showNetworkError = () => { notification.error({ message: MESSAGE.NETWORK_CONNECTION_ERROR, }) } /** To check current using browser is Safari or not */ export const isSafari = (): boolean => { const result = /^((?!chrome|android).)*safari/i.test(navigator.userAgent) return result } export const getUrlQueryParams = (searchString: string): any => { return searchString?.length > 1 ? qs.parse(window.location.search) : ""; }; export const calculateResizeWindow = (): WindowSize => { const w = 375 const h = 850 const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : window.screen.width const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : window.screen.height const systemZoom = width / window.screen.availWidth const left = (width - w) / 2 / systemZoom + dualScreenLeft const top = (height - h) / 2 / systemZoom + dualScreenTop const result = { width: w, height: h, top, left, systemZoom, } return result } export const openPopupWindow = (url: string, newWindow?: Window): Window | null => { if (newWindow) { newWindow.location = url return newWindow } else { const { width, height, top, left, systemZoom } = calculateResizeWindow() return window.open( url, "Passport app", ` scrollbars=yes, width=${width}, height=${height / systemZoom}, top=${top}, left=${left} ` ) } } export const createOrderLines = (product: {[key: string]: any}) => { return Object.keys(product).map((item, index) => { const p = product[item] const subTotal = convertCurrencyToNumber(p.price) * p.quantity let category = p.category; for (const [key, values] of Object.entries(ORDER_LINE_CATEGORY)) { if (values.includes(category)) { category = key; break; } } const orderLine: OrderLine = { name: p.name, reference_id: `reference_id_${index}`, image_url: p.img, product_url: p.img, quantity: p.quantity, category: category, unit_price: { value: convertCurrencyToNumber(p.price).toString(), currency: "VND", }, subtotal: { value: subTotal.toString(), currency: "VND", }, measurement_unit: "EA", } return { ...orderLine, } }) } export const createLodgeRequest = ( type: "BNPL" | "OFFER", offerType: OfferType, user?: UserProfile, selectedProductCode?: string[], offerCodes?: string[], theme?: PassportThemeConfig, ): LodgeRequest => { return { type: type, bnpl_order: { reference_id: offerType.reference_id, total_amount: offerType.total_amount, order_lines: offerType.order_lines, payment_recipient: offerType.payment_recipient, is_business: offerType.is_business, }, user: { id: user?.id || "", phone: { phoneNumber: user?.phoneNumber || "", countryCode: user?.countryCode || "", }, name: user?.fullName, email: user?.email, credifyId: user?.credifyId, gender: "male", }, service: { available_offer_codes: (offerCodes || []).length > 0 ? offerCodes : undefined, available_product_codes: selectedProductCode, package_code: "3", ui: { theme: theme, } } } }