import React from 'react'; import FontAwesome from 'react-native-vector-icons/FontAwesome'; import {CODES} from 'ordering-components/native' export const flatArray = (arr: any) => [].concat(...arr) /** * Function to return the traduction depending of a key 't' * @param {string} key for traduction */ export const getTraduction = (key: string, t: any) => { const keyList: any = { // Add the key and traduction that you need below ERROR_ORDER_WITHOUT_CART: 'The order was placed without a cart', ERROR_INVALID_COUPON: "The coupon doesn't exist", ERROR_IVALID_COUPON_MINIMUM: 'You must have more products in your cart to use the coupon', ERROR_ADD_PRODUCT_VERY_FAR_FOR_PICKUP: 'The business is too far for order type pickup', ERROR_PLACE_PAY_WITH_CARD2: 'An error occurred while trying to pay by card', ERROR_ADD_PRODUCT_BUSINESS_CLOSED: 'The business is closed at the moment', INTERNAL_ERROR: 'Server Error, please wait, we are working to fix it', ERROR_NOT_FOUND_BUSINESSES: 'No businesses found near your location', YOU_DO_NOT_HAVE_PERMISSION: 'You do not have permission', INVALID_CODE: 'Invalid verify code', STRIPE_ERROR: 'Payment service error. Try again later.', ERROR_AUTH_TWILIO_DISABLED: 'Auth error, twilio is disabled', ERROR_CART_SELECT_PAYMETHOD: 'An error occurred with selected pay method', ERROR_YOU_HAVE_ACTIVE_CART: 'You can\'t reorder this cart', ERROR_YOU_HAVE_NOT_CART: 'Cart not found', ERROR_PLACE_PAY_WITH_REDIRECT: 'An error occurred while trying to pay by redirect', ERROR_PLACE_PAY_WITH_CARD1: 'An error occurred while trying to pay by card', ERROR_PLACE_PAY_WITH_PAYPAL_CAPTURE: 'An error occurred while trying to pay by PayPal', ERROR_ADD_PRODUCT_VERY_FAR_FOR_DELIVERY: 'Error adding product, very far for delivery', ERROR_PRODUCT_NOT_FOUND: 'Error with the product' } return keyList[key] ? t(key, keyList[key]) : t(key) } /** * Function to convert delivery time in minutes * @param {string} time business delivery time */ export const convertHoursToMinutes = (time: any) => { if (!time) return '0min' const [hour, minute] = time.split(':') const result = (parseInt(hour, 10) * 60) + parseInt(minute, 10) return `${result}min` } export const getIconCard = (brand: string, size: number) => { const value = brand?.toLowerCase() switch (value) { case 'visa': return ( FontAwesome && ) case 'mastercard': return ( FontAwesome && ) case 'amex': return ( FontAwesome && ) case 'discover': return ( FontAwesome && ) case 'jcb': return ( FontAwesome && ) case 'diners-club': return ( FontAwesome && ) default: return ( FontAwesome && ) } } /** * Function to return a static google maps image based in location * @param {object} param object with latitude and logitude */ export const getGoogleMapImage = ({ lat, lng }: any, apiKey: string) => { return `https://maps.googleapis.com/maps/api/staticmap?size=500x190¢er=${lat},${lng}&zoom=17&scale=2&maptype=roadmap&&markers=icon:https://res.cloudinary.com/ditpjbrmz/image/upload/f_auto,q_auto,w_45,q_auto:best,q_auto:best/v1564675872/marker-customer_kvxric.png%7Ccolor:white%7C${lat},${lng}&key=${apiKey}` } /** * List of fields with correct order */ export const fieldsToSort = ['name', 'middle_name', 'lastname', 'second_lastname', 'email']; /** * Function to return a array sorted by certain fields * @param fields Array with right order * @param array Array to sort */ export const sortInputFields = ({ fields, values }: any) => { let fieldsBase = fields; const fieldsSorted: any = []; const fieldsArray = Array.isArray(values) ? values : values && Object.values(values); if (!fieldsBase) { fieldsBase = fieldsToSort } fieldsBase.forEach((f: any) => { fieldsArray && fieldsArray.forEach((field: any) => { if (f === field.code) { fieldsSorted.push(field) } }) }); return fieldsSorted; } export const transformCountryCode = (countryCode : number) => { const code = CODES.find((code : any) => code.phoneCode === countryCode) return code?.countryCode } /** * Function to check if a number is decimal or not * @param {*} value number to check if decimal or not * @param {*} parser function fallback when is decimal * @returns string */ export const verifyDecimals = (value: number, parser: any) => { if (value % 1 === 0) { return value } else { return parser(value) } } /** * Function to transform degree to radian * @param {number} value for transform * */ export const convertToRadian = (value: number) => { return value * Math.PI / 180 } /** * Function to distance between two locations * @param lat1 Lat for first location * @param lon1 Lon for first location * @param lat2 Lat for second location * @param lon2 Lon for second location */ export const getDistance = (lat1: any, lon1: any, lat2: any, lon2: any) => { const R = 6371 // km const dLat = convertToRadian(lat2 - lat1) const dLon = convertToRadian(lon2 - lon1) const curLat1 = convertToRadian(lat1) const curLat2 = convertToRadian(lat2) const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(curLat1) * Math.cos(curLat2) const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) return R * c }