Skip to content

Commit

Permalink
chore: add more necessary files to be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Oct 14, 2021
1 parent 2b4d5de commit 3db4ebd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/botonic-react/src/experimental/util/objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* given an object and a property, returns the property if exists (recursively), else undefined
* ex:
* let obj = { a: { b: { c: 5 } } }
* getProperty(obj, 'a.b.c'), returns 5
* getProperty(obj, 'a.b.z'), returns undefined
*/
export const getProperty = (obj, property) => {
if (!property) return undefined
const properties = property.split('.')
for (let i = 0; i < properties.length; i++) {
const prop = properties[i]
// eslint-disable-next-line no-prototype-builtins
if (!obj || !obj.hasOwnProperty(prop)) {
return undefined
} else {
obj = obj[prop]
}
}
return obj
}

export function strToBool(string) {
const regex = /^\s*(true|1|on)\s*$/i
string = String(string)
return regex.test(string)
}

export const mapObject = (obj, conversion = ([key, value]) => [key, value]) => {
return (
obj &&
Object.entries(obj)
.map(conversion)
.reduce(function (prev, curr) {
prev[curr[0]] = curr[1]
return prev
}, {})
)
}
58 changes: 58 additions & 0 deletions packages/botonic-react/src/experimental/util/webchat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import merge from 'lodash.merge'
import UAParser from 'ua-parser-js'
import { v4 as uuidv4 } from 'uuid'

import { WEBCHAT } from '../constants'
import { getProperty } from './objects'

/**
* Returns the value of a property defined in bot's theme based on WEBCHAT.CUSTOM_PROPERTIES dictionary.
* It gives preference to nested defined properties (e.g.: header.style) over plain properties (e.g.: headerStyle).
* If property doesn't exist, returns the defaultValue.
*/
export const _getThemeProperty = theme => (
property,
defaultValue = undefined
) => {
for (const [k, v] of Object.entries(WEBCHAT.CUSTOM_PROPERTIES)) {
if (v == property) {
const nestedProperty = getProperty(theme, v)
if (nestedProperty !== undefined) return nestedProperty
const plainProperty = getProperty(theme, k)
if (plainProperty !== undefined) return plainProperty
return defaultValue
}
}
return undefined
}

export const createUser = () => {
const parser = new UAParser()
const ua = parser.getResult()
let name = `${ua.os.name} ${ua.browser.name}`
if (ua.device && ua.device.type) name = `${ua.device.type} ${name}`
return {
id: uuidv4(),
name,
}
}
export const initSession = session => {
if (!session) session = {}
const hasUserId = session.user && session.user.id !== undefined
if (!session.user || Object.keys(session.user).length === 0 || !hasUserId)
session.user = !hasUserId ? merge(session.user, createUser()) : createUser()
return session
}

export const shouldKeepSessionOnReload = ({
initialDevSettings,
devSettings,
}) => !initialDevSettings || (devSettings && devSettings.keepSessionOnReload)

export const getServerErrorMessage = serverConfig => {
if (!serverConfig || !serverConfig.errorMessage) return 'Connection issues'
if (typeof serverConfig.errorMessage === 'function') {
return serverConfig.errorMessage()
}
return serverConfig.errorMessage
}

0 comments on commit 3db4ebd

Please sign in to comment.