-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add more necessary files to be changed
- Loading branch information
1 parent
2b4d5de
commit 3db4ebd
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, {}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |