From 43c3d2c1b333f909bb52292ceb3cf2228ccf324c Mon Sep 17 00:00:00 2001 From: timothy-janssen <43193763+timothy-janssen@users.noreply.github.com> Date: Wed, 16 Jan 2019 10:16:25 -0800 Subject: [PATCH 1/3] Add hook for window method applicationParse If applicationParse is an available method in the context of the application, it will be called before the messages are sent to setState and displayed to the user. --- src/containers/Chat/index.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/containers/Chat/index.js b/src/containers/Chat/index.js index 0ae9b135..f197b6d0 100644 --- a/src/containers/Chat/index.js +++ b/src/containers/Chat/index.js @@ -20,10 +20,44 @@ import Input from 'components/Input' import './style.scss' const MAX_GET_MEMORY_TIME = 10 * 1000 // in ms +const MAX_APP_PARSE_TIME = 3 * 1000 // in ms const FAILED_TO_GET_MEMORY = 'Could not get memory from webchatMethods.getMemory :' +const FAILED_TO_PARSE = 'Application could not parse the response from backend : ' const WRONG_MEMORY_FORMAT = 'Wrong memory format, expecting : { "memory": , "merge": }' +const getApplicationParse = messages => { + return new Promise(resolve => { + if (!window.webchatMethods || !window.webchatMethods.applicationParse) { + return resolve() + } + // so that we process the message in all cases + setTimeout(resolve, MAX_APP_PARSE_TIME) + try { + const applicationParseResponse = window.webchatMethods.applicationParse(messages) + if (!applicationParseResponse) { + return resolve() + } + if (applicationParseResponse.then && typeof applicationParseResponse.then === 'function') { + // the function returned a Promise + applicationParseResponse + .then(applicationParse => resolve()) + .catch(err => { + console.error(FAILED_TO_PARSE) + console.error(err) + resolve() + }) + } else { + resolve() + } + } catch (err) { + console.error(FAILED_TO_PARSE) + console.error(err) + resolve() + } + }) +} + @connect( state => ({ token: state.conversation.token, @@ -61,6 +95,7 @@ class Chat extends Component { const { messages, show } = nextProps if (messages !== this.state.messages) { + getApplicationParse(messages) this.setState({ messages }, () => { const { getLastMessage } = this.props if (getLastMessage) { From 8a78f3f31293cac9babd22ebdf618ade72d2dd4b Mon Sep 17 00:00:00 2001 From: timothy-janssen <43193763+timothy-janssen@users.noreply.github.com> Date: Wed, 16 Jan 2019 11:30:20 -0800 Subject: [PATCH 2/3] update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 97c2c64d..e8ffdd11 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "clean:lib": "rm -rf lib", "clean:dist": "rm -rf dist", - "build": "npm run clean:dist && NODE_ENV=production npm run build:webpack", + "build": "npm run clean:dist && SET NODE_ENV=development npm run build:webpack", "build:webpack": "node bin/build.js", "build:js": "babel src -d lib", "start": "npm run clean:dist && node bin/build-dev", From 0c39eb96743a5c307dc5bfcefdf6fc5999df1d75 Mon Sep 17 00:00:00 2001 From: timothy-janssen <43193763+timothy-janssen@users.noreply.github.com> Date: Thu, 14 Mar 2019 10:09:10 -0700 Subject: [PATCH 3/3] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e8ffdd11..97c2c64d 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "clean:lib": "rm -rf lib", "clean:dist": "rm -rf dist", - "build": "npm run clean:dist && SET NODE_ENV=development npm run build:webpack", + "build": "npm run clean:dist && NODE_ENV=production npm run build:webpack", "build:webpack": "node bin/build.js", "build:js": "babel src -d lib", "start": "npm run clean:dist && node bin/build-dev",