@defra/flood-webchat 0.0.1-beta.2 → 0.0.1-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -1
- package/dist/client.js +1871 -212
- package/dist/client.js.map +1 -1
- package/dist/server.js +181 -240
- package/dist/server.js.map +1 -1
- package/main.scss +4 -3
- package/package.json +6 -2
- package/src/client/components/availability/availability.jsx +44 -46
- package/src/client/components/availability/availability.scss +105 -5
- package/src/client/components/chat/chat.jsx +169 -0
- package/src/client/components/chat/chat.scss +143 -0
- package/src/client/components/errorSummary/error-summary.jsx +34 -0
- package/src/client/components/message/message.jsx +20 -0
- package/src/client/components/panel/panel-footer.jsx +9 -0
- package/src/client/components/panel/panel-header.jsx +43 -0
- package/src/client/components/panel/panel.jsx +133 -0
- package/src/client/components/panel/panel.scss +72 -0
- package/src/client/components/screens/end-chat.jsx +35 -0
- package/src/client/components/screens/end-feedback.jsx +18 -0
- package/src/client/components/screens/feedback.jsx +149 -0
- package/src/client/components/screens/pre-chat.jsx +65 -0
- package/src/client/components/screens/request-chat.jsx +157 -0
- package/src/client/components/screens/settings.jsx +67 -0
- package/src/client/components/screens/unavailable.jsx +23 -0
- package/src/client/hooks/useFocusedElements.js +71 -0
- package/src/client/hooks/useTextareaAutosize.js +13 -0
- package/src/client/index.jsx +15 -1
- package/src/client/lib/check-availability.js +1 -0
- package/src/client/lib/message-notification.js +36 -0
- package/src/client/lib/transform-messages.js +35 -0
- package/src/client/store/AppProvider.jsx +140 -0
- package/src/client/store/actions-map.js +119 -0
- package/src/client/store/constants.js +3 -0
- package/src/client/store/reducer.js +28 -0
- package/src/client/store/useApp.js +5 -0
- package/src/client/store/useChatSdk.js +37 -0
- package/src/server/index.js +15 -6
- package/src/server/lib/client.js +31 -35
- package/src/server/lib/utils.js +18 -9
- package/src/client/lib/external-stores.js +0 -4
- package/src/client/lib/external-sync-store.js +0 -42
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { PanelHeader } from '../panel/panel-header.jsx'
|
|
3
|
+
|
|
4
|
+
export function PreChat ({ onContinue }) {
|
|
5
|
+
return (
|
|
6
|
+
<>
|
|
7
|
+
<PanelHeader />
|
|
8
|
+
|
|
9
|
+
<div className='wc-body'>
|
|
10
|
+
<h3 className='govuk-heading-s' aria-live='polite'>What you can use webchat for</h3>
|
|
11
|
+
<p className='govuk-body-s'>Webchat lets you talk directly to a Floodline adviser.</p>
|
|
12
|
+
|
|
13
|
+
<p className='govuk-body-s'>You can use webchat to get:</p>
|
|
14
|
+
<ul className='govuk-list govuk-list--bullet govuk-body-s'>
|
|
15
|
+
<li>current flood warnings and alerts in your area</li>
|
|
16
|
+
<li>information on the flood warning service</li>
|
|
17
|
+
<li>advice on what to do before, during and after a flood</li>
|
|
18
|
+
</ul>
|
|
19
|
+
|
|
20
|
+
<p className='govuk-body-s'>
|
|
21
|
+
{
|
|
22
|
+
'There are other ways to '
|
|
23
|
+
}
|
|
24
|
+
<a className='govuk-link' href='https://www.gov.uk/sign-up-for-flood-warnings' target='_blank' rel='noreferrer'>
|
|
25
|
+
sign up for flood warnings
|
|
26
|
+
</a>
|
|
27
|
+
{
|
|
28
|
+
' and '
|
|
29
|
+
}
|
|
30
|
+
<a className='govuk-link' href='https://www.fws.environment-agency.gov.uk/app/olr/login' target='_blank' rel='noreferrer'>
|
|
31
|
+
manage your flood warnings account
|
|
32
|
+
</a>.
|
|
33
|
+
</p>
|
|
34
|
+
<p className='govuk-body-s'>
|
|
35
|
+
{'Do not use webchat to '}
|
|
36
|
+
<a
|
|
37
|
+
className='govuk-link'
|
|
38
|
+
href='https://www.gov.uk/report-flood-cause'
|
|
39
|
+
target='_blank'
|
|
40
|
+
rel='noreferrer'
|
|
41
|
+
>
|
|
42
|
+
report a flood
|
|
43
|
+
</a>.
|
|
44
|
+
</p>
|
|
45
|
+
|
|
46
|
+
<button className='govuk-button govuk-!-font-size-16' data-module='govuk-button' onClick={onContinue}>Continue</button>
|
|
47
|
+
|
|
48
|
+
<h3 className='govuk-heading-s'>Other flood information</h3>
|
|
49
|
+
|
|
50
|
+
<p className='govuk-body-s'>Call Floodline for all other flood and flood warning information.</p>
|
|
51
|
+
<p className='govuk-body-s'>
|
|
52
|
+
<strong>Floodline helpline</strong>
|
|
53
|
+
<br />
|
|
54
|
+
Telephone: 0345 988 1188
|
|
55
|
+
<br />
|
|
56
|
+
Textphone: 0345 602 6340
|
|
57
|
+
<br />
|
|
58
|
+
Open 24 hours a day, 7 days a week
|
|
59
|
+
<br />
|
|
60
|
+
<a className='govuk-link' href='https://gov.uk/call-charges' target='_blank' rel='noreferrer'>Find out more about call charges</a>
|
|
61
|
+
</p>
|
|
62
|
+
</div>
|
|
63
|
+
</>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import React, { useRef, useState, useEffect } from 'react'
|
|
2
|
+
import * as uuid from 'uuid'
|
|
3
|
+
|
|
4
|
+
import { classnames } from '../../lib/classnames.js'
|
|
5
|
+
import { PanelHeader } from '../panel/panel-header.jsx'
|
|
6
|
+
import { useApp } from '../../store/useApp.js'
|
|
7
|
+
import { useChatSdk } from '../../store/useChatSdk.js'
|
|
8
|
+
import { ErrorSummary } from '../errorSummary/error-summary.jsx'
|
|
9
|
+
|
|
10
|
+
const QUESTION_MAX_LENGTH = 500
|
|
11
|
+
|
|
12
|
+
export function RequestChat ({ onPreChatScreen }) {
|
|
13
|
+
const { sdk, setCustomerId, setThreadId, setThread } = useApp()
|
|
14
|
+
const { fetchCustomerId, fetchThread } = useChatSdk(sdk)
|
|
15
|
+
|
|
16
|
+
const [errors, setErrors] = useState({})
|
|
17
|
+
const [questionLength, setQuestionLength] = useState(0)
|
|
18
|
+
|
|
19
|
+
const nameRef = useRef()
|
|
20
|
+
const questionRef = useRef()
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (Object.keys(errors).length) {
|
|
24
|
+
document.querySelector('.govuk-error-summary')?.focus()
|
|
25
|
+
}
|
|
26
|
+
}, [errors])
|
|
27
|
+
|
|
28
|
+
const onQuestionChange = e => {
|
|
29
|
+
setQuestionLength(e.target.value.length)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const onRequestChat = async e => {
|
|
33
|
+
e.preventDefault()
|
|
34
|
+
|
|
35
|
+
const errs = {}
|
|
36
|
+
|
|
37
|
+
if (nameRef.current.value.length === 0) {
|
|
38
|
+
errs.name = 'Enter your name'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (questionRef.current.value.length === 0) {
|
|
42
|
+
errs.question = 'Enter your question'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (questionRef.current.value.length > QUESTION_MAX_LENGTH) {
|
|
46
|
+
errs.question = 'Your question must be 500 characters or less'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (Object.keys(errs).length === 0) {
|
|
50
|
+
try {
|
|
51
|
+
const threadId = uuid.v4()
|
|
52
|
+
const customerId = await fetchCustomerId()
|
|
53
|
+
const thread = await fetchThread(threadId)
|
|
54
|
+
|
|
55
|
+
sdk.getCustomer().setName(nameRef.current.value)
|
|
56
|
+
|
|
57
|
+
await thread.startChat(questionRef.current.value || 'Begin conversation')
|
|
58
|
+
|
|
59
|
+
setCustomerId(customerId)
|
|
60
|
+
setThreadId(threadId)
|
|
61
|
+
setThread(thread)
|
|
62
|
+
thread.setCustomField('threadid', threadId)
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.log('[Request Chat Error]', err)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
setErrors(errs)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const isQuestionLengthValid = QUESTION_MAX_LENGTH >= questionLength
|
|
72
|
+
const questionLengthRemaining = QUESTION_MAX_LENGTH - questionLength
|
|
73
|
+
const questionLengthExceeded = questionLength - QUESTION_MAX_LENGTH
|
|
74
|
+
|
|
75
|
+
let questionHint = `You have ${questionLengthRemaining} characters remaining`
|
|
76
|
+
|
|
77
|
+
if (!isQuestionLengthValid) {
|
|
78
|
+
questionHint = `You have ${questionLengthExceeded} characters too many`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<>
|
|
83
|
+
<PanelHeader />
|
|
84
|
+
|
|
85
|
+
<div className='wc-body'>
|
|
86
|
+
<a href='#' className='wc-back-link govuk-back-link' onClick={onPreChatScreen}>
|
|
87
|
+
What you can use webchat for
|
|
88
|
+
</a>
|
|
89
|
+
|
|
90
|
+
<ErrorSummary errors={errors} />
|
|
91
|
+
|
|
92
|
+
<h3 className='govuk-heading-s' aria-live='polite'>
|
|
93
|
+
Your name and question
|
|
94
|
+
</h3>
|
|
95
|
+
|
|
96
|
+
<form>
|
|
97
|
+
<div className={classnames('govuk-form-group', errors.name && 'govuk-form-group--error')}>
|
|
98
|
+
<label className='govuk-label' htmlFor='wc-name'>
|
|
99
|
+
Your name
|
|
100
|
+
</label>
|
|
101
|
+
{errors.name && (
|
|
102
|
+
<p className='govuk-error-message'>
|
|
103
|
+
<span className='govuk-visually-hidden'>Error:</span> {errors.name}
|
|
104
|
+
</p>
|
|
105
|
+
)}
|
|
106
|
+
<input
|
|
107
|
+
ref={nameRef}
|
|
108
|
+
className={classnames('govuk-input', errors.name && 'govuk-input--error')}
|
|
109
|
+
id='wc-name'
|
|
110
|
+
name='name'
|
|
111
|
+
type='text'
|
|
112
|
+
data-testid='request-chat-user-name'
|
|
113
|
+
/>
|
|
114
|
+
</div>
|
|
115
|
+
|
|
116
|
+
<div className='govuk-character-count' data-module='govuk-character-count' data-maxlength='500'>
|
|
117
|
+
<div className={classnames('govuk-form-group', errors.question && 'govuk-form-group--error')}>
|
|
118
|
+
<label className='govuk-label' htmlFor='wc-question'>
|
|
119
|
+
Your question
|
|
120
|
+
</label>
|
|
121
|
+
{errors.question && (
|
|
122
|
+
<p className='govuk-error-message'>
|
|
123
|
+
<span className='govuk-visually-hidden'>Error:</span>
|
|
124
|
+
{errors.question}
|
|
125
|
+
</p>
|
|
126
|
+
)}
|
|
127
|
+
<textarea
|
|
128
|
+
ref={questionRef}
|
|
129
|
+
id='wc-question'
|
|
130
|
+
name='question'
|
|
131
|
+
rows='5'
|
|
132
|
+
aria-describedby='wc-question-info'
|
|
133
|
+
onChange={onQuestionChange}
|
|
134
|
+
className={classnames('govuk-textarea', 'govuk-js-character-count', !isQuestionLengthValid || errors.question ? 'govuk-textarea--error' : '')}
|
|
135
|
+
data-testid='request-chat-user-question'
|
|
136
|
+
/>
|
|
137
|
+
<div id='wc-question-info' className='govuk-hint govuk-character-count__message' style={{ color: `${!isQuestionLengthValid ? '#d4351c' : ''}` }} aria-hidden='true'>
|
|
138
|
+
{questionHint}
|
|
139
|
+
</div>
|
|
140
|
+
<div className='govuk-character-count__sr-status govuk-visually-hidden' aria-live='polite'>
|
|
141
|
+
{questionHint}
|
|
142
|
+
</div>
|
|
143
|
+
</div>
|
|
144
|
+
</div>
|
|
145
|
+
|
|
146
|
+
<button
|
|
147
|
+
className='govuk-button govuk-!-margin-top-1 govuk-!-font-size-16'
|
|
148
|
+
data-module='govuk-button'
|
|
149
|
+
onClick={onRequestChat}
|
|
150
|
+
>
|
|
151
|
+
Request chat
|
|
152
|
+
</button>
|
|
153
|
+
</form>
|
|
154
|
+
</div>
|
|
155
|
+
</>
|
|
156
|
+
)
|
|
157
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
import { PanelHeader } from '../panel/panel-header.jsx'
|
|
3
|
+
|
|
4
|
+
import { useApp } from '../../store/useApp.js'
|
|
5
|
+
|
|
6
|
+
export function Settings ({ onCancel }) {
|
|
7
|
+
const { settings, setSettings } = useApp()
|
|
8
|
+
|
|
9
|
+
const [optionAudio, setOptionAudio] = useState(settings.audio)
|
|
10
|
+
const [optionScroll, setOptionScroll] = useState(settings.scroll)
|
|
11
|
+
|
|
12
|
+
const onSave = e => {
|
|
13
|
+
e.preventDefault()
|
|
14
|
+
|
|
15
|
+
setSettings({ audio: optionAudio, scroll: optionScroll })
|
|
16
|
+
onCancel(e)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<>
|
|
21
|
+
<PanelHeader />
|
|
22
|
+
|
|
23
|
+
<div className='wc-body'>
|
|
24
|
+
<fieldset className='govuk-fieldset govuk-!-margin-bottom-4'>
|
|
25
|
+
<legend className='govuk-fieldset__legend govuk-fieldset__legend'>
|
|
26
|
+
<h3 id='wc-subtitle' className='govuk-heading-s'>Change settings</h3>
|
|
27
|
+
</legend>
|
|
28
|
+
<div className='govuk-checkboxes govuk-checkboxes--small' data-module='govuk-checkboxes'>
|
|
29
|
+
<div className='govuk-checkboxes__item'>
|
|
30
|
+
<input
|
|
31
|
+
id='audio'
|
|
32
|
+
name='audio'
|
|
33
|
+
type='checkbox'
|
|
34
|
+
value='audio'
|
|
35
|
+
className='govuk-checkboxes__input'
|
|
36
|
+
defaultChecked={optionAudio}
|
|
37
|
+
onChange={() => setOptionAudio(!optionAudio)}
|
|
38
|
+
/>
|
|
39
|
+
<label className='govuk-label govuk-checkboxes__label govuk-!-font-size-16' htmlFor='audio'>
|
|
40
|
+
Play a sound when receiving a new message
|
|
41
|
+
</label>
|
|
42
|
+
</div>
|
|
43
|
+
<div className='govuk-checkboxes__item'>
|
|
44
|
+
<input
|
|
45
|
+
id='scroll'
|
|
46
|
+
name='scroll'
|
|
47
|
+
type='checkbox'
|
|
48
|
+
value='scroll'
|
|
49
|
+
className='govuk-checkboxes__input'
|
|
50
|
+
defaultChecked={optionScroll}
|
|
51
|
+
onChange={() => setOptionScroll(!optionScroll)}
|
|
52
|
+
/>
|
|
53
|
+
<label className='govuk-label govuk-checkboxes__label govuk-!-font-size-16' htmlFor='scroll'>
|
|
54
|
+
Scroll automatically to a new message
|
|
55
|
+
</label>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
</fieldset>
|
|
59
|
+
|
|
60
|
+
<div className='govuk-button-group'>
|
|
61
|
+
<a id='settings-save' href='#' className='govuk-button govuk-!-font-size-16' data-module='govuk-button' onClick={onSave}>Save</a>
|
|
62
|
+
<a id='settings-cancel' href='#' className='govuk-link govuk-!-font-size-16' data-module='govuk-button' onClick={onCancel}>Cancel</a>
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
</>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { PanelHeader } from '../panel/panel-header.jsx'
|
|
3
|
+
|
|
4
|
+
export function Unavailable () {
|
|
5
|
+
return (
|
|
6
|
+
<>
|
|
7
|
+
<PanelHeader />
|
|
8
|
+
|
|
9
|
+
<div className='wc-body'>
|
|
10
|
+
<h3 id='wc-subtitle' className='govuk-heading-m'>Webchat is currently not available</h3>
|
|
11
|
+
<p className='govuk-body-s'>Try again later, or call Floodline:</p>
|
|
12
|
+
<p className='govuk-body-s'>
|
|
13
|
+
<strong>Floodline helpline</strong>
|
|
14
|
+
<br />Telephone: 0345 988 1188
|
|
15
|
+
<br />Textphone: 0345 602 6340
|
|
16
|
+
<br />Open 24 hours a day, 7 days a week
|
|
17
|
+
<br /><a className='govuk-link' href='https://gov.uk/call-charges'>Find out more about call charges</a>
|
|
18
|
+
</p>
|
|
19
|
+
<p className='govuk-body-s'>We're running webchat as a trial, it will not always be available.</p>
|
|
20
|
+
</div>
|
|
21
|
+
</>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback } from 'react'
|
|
2
|
+
|
|
3
|
+
const setAriaHidden = bool => {
|
|
4
|
+
for (const node of document.body.children) {
|
|
5
|
+
if (node.id !== 'wc-panel') {
|
|
6
|
+
(bool) ? node.setAttribute('aria-hidden', 'true') : node.removeAttribute('aria-hidden')
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const getFocusableElements = () => {
|
|
12
|
+
const selectors = [
|
|
13
|
+
'#wc-panel a:not([disabled])',
|
|
14
|
+
'#wc-panel button:not([disabled])',
|
|
15
|
+
'#wc-panel select:not([disabled])',
|
|
16
|
+
'#wc-panel input:not([disabled])',
|
|
17
|
+
'#wc-panel textarea:not([disabled])',
|
|
18
|
+
'#wc-panel *[tabindex="0"]:not([disabled])'
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
const elements = document.body.querySelectorAll(selectors.join(','))
|
|
22
|
+
return Array.from(elements).filter(e => !e.closest('[hidden]') && !e.closest('[aria-hidden="true"]'))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const useFocusedElements = screen => {
|
|
26
|
+
const [panelElements, setPanelElements] = useState([])
|
|
27
|
+
|
|
28
|
+
const onKeyDown = useCallback(e => {
|
|
29
|
+
const webchatPanelElement = document.querySelector('#wc-panel')
|
|
30
|
+
|
|
31
|
+
if (e.key === 'Tab') {
|
|
32
|
+
if (webchatPanelElement && !document.activeElement.closest('#wc-panel')) {
|
|
33
|
+
webchatPanelElement.focus()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (e.shiftKey) {
|
|
37
|
+
if (document.activeElement === panelElements[0]) {
|
|
38
|
+
panelElements[panelElements.length - 1].focus()
|
|
39
|
+
e.preventDefault()
|
|
40
|
+
} else if (document.activeElement === document.querySelector('#wc-panel')) {
|
|
41
|
+
panelElements[panelElements.length - 1]?.focus()
|
|
42
|
+
e.preventDefault()
|
|
43
|
+
}
|
|
44
|
+
} else if (document.activeElement === panelElements[panelElements.length - 1]) {
|
|
45
|
+
panelElements[0].focus()
|
|
46
|
+
e.preventDefault()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}, [panelElements])
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
setPanelElements(getFocusableElements())
|
|
53
|
+
}, [screen])
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
setAriaHidden(true)
|
|
57
|
+
|
|
58
|
+
const panelElement = document.querySelector('#wc-panel')
|
|
59
|
+
panelElement.focus()
|
|
60
|
+
|
|
61
|
+
document.addEventListener('keydown', onKeyDown)
|
|
62
|
+
|
|
63
|
+
return () => {
|
|
64
|
+
setAriaHidden()
|
|
65
|
+
document.body.querySelector('.wc-availability__link')?.focus()
|
|
66
|
+
document.removeEventListener('keydown', onKeyDown)
|
|
67
|
+
}
|
|
68
|
+
}, [panelElements])
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { useFocusedElements }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useEffect } from 'react'
|
|
2
|
+
|
|
3
|
+
export const useTextareaAutosize = (textAreaRef, value) => {
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
if (textAreaRef) {
|
|
6
|
+
textAreaRef.style.height = '0px'
|
|
7
|
+
|
|
8
|
+
const scrollHeight = textAreaRef.scrollHeight
|
|
9
|
+
|
|
10
|
+
textAreaRef.style.height = `${scrollHeight}px`
|
|
11
|
+
}
|
|
12
|
+
}, [textAreaRef, value])
|
|
13
|
+
}
|
package/src/client/index.jsx
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import { createRoot } from 'react-dom/client'
|
|
3
|
+
import { ChatSdk } from '@nice-devone/nice-cxone-chat-web-sdk'
|
|
3
4
|
import { Availability } from './components/availability/availability.jsx'
|
|
4
5
|
import { checkAvailability } from './lib/check-availability'
|
|
6
|
+
import { AppProvider } from './store/AppProvider.jsx'
|
|
7
|
+
import { CUSTOMER_ID_STORAGE_KEY } from './store/constants.js'
|
|
5
8
|
|
|
6
9
|
export async function init (container, options) {
|
|
10
|
+
const sdk = new ChatSdk({
|
|
11
|
+
brandId: options.brandId,
|
|
12
|
+
channelId: options.channelId,
|
|
13
|
+
customerId: window.localStorage.getItem(CUSTOMER_ID_STORAGE_KEY) || '',
|
|
14
|
+
environment: options.environment
|
|
15
|
+
})
|
|
16
|
+
|
|
7
17
|
const root = createRoot(container)
|
|
8
18
|
let availability
|
|
9
19
|
try {
|
|
@@ -12,5 +22,9 @@ export async function init (container, options) {
|
|
|
12
22
|
} catch (e) {
|
|
13
23
|
availability = 'UNAVAILABLE'
|
|
14
24
|
}
|
|
15
|
-
root.render(
|
|
25
|
+
root.render(
|
|
26
|
+
<AppProvider sdk={sdk} availability={availability} options={options}>
|
|
27
|
+
<Availability />
|
|
28
|
+
</AppProvider>
|
|
29
|
+
)
|
|
16
30
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const messageNotification = audioUrl => {
|
|
2
|
+
let buffer
|
|
3
|
+
|
|
4
|
+
const context = new (window.AudioContext || window.webkitAudioContext)()
|
|
5
|
+
|
|
6
|
+
if (context.state === 'suspended') {
|
|
7
|
+
const events = ['touchstart', 'touchend', 'mousedown', 'wheel', 'keydown', 'click']
|
|
8
|
+
|
|
9
|
+
const unlock = _e => {
|
|
10
|
+
events.forEach(event => {
|
|
11
|
+
document.body.removeEventListener(event, unlock)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
context.resume()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
events.forEach(event => {
|
|
18
|
+
document.body.addEventListener(event, unlock, false)
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fetch(audioUrl)
|
|
23
|
+
.then(response => response.arrayBuffer())
|
|
24
|
+
.then(data => context.decodeAudioData(data))
|
|
25
|
+
.then(decodedData => {
|
|
26
|
+
buffer = decodedData
|
|
27
|
+
})
|
|
28
|
+
.catch(console.error)
|
|
29
|
+
|
|
30
|
+
return () => {
|
|
31
|
+
const source = context.createBufferSource()
|
|
32
|
+
source.buffer = buffer
|
|
33
|
+
source.connect(context.destination)
|
|
34
|
+
source.start()
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as uuid from 'uuid'
|
|
2
|
+
import { DateTime } from 'luxon'
|
|
3
|
+
|
|
4
|
+
export const transformMessage = message => {
|
|
5
|
+
return {
|
|
6
|
+
id: message.id,
|
|
7
|
+
text: message.messageContent?.text,
|
|
8
|
+
createdAt: new Date(message.createdAt),
|
|
9
|
+
user: message.authorEndUserIdentity?.fullName?.trim() || null,
|
|
10
|
+
assignee: message.authorUser?.firstName || null,
|
|
11
|
+
direction: message.direction
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const transformMessages = messages => messages.map(message => transformMessage(message))
|
|
16
|
+
|
|
17
|
+
export const formatTranscript = messages => {
|
|
18
|
+
const now = DateTime.local()
|
|
19
|
+
now.setZone('Europe/London')
|
|
20
|
+
|
|
21
|
+
let string = `Floodline webchat transcript, ${now.toFormat('HH:mm:ss a, dd LLLL yyyy')}, (ID: ${uuid.v4().split('-')[0]})\n\n`
|
|
22
|
+
|
|
23
|
+
for (const message of messages) {
|
|
24
|
+
const { text, direction, user, assignee, createdAt } = message
|
|
25
|
+
|
|
26
|
+
const author = direction === 'inbound' ? user : `${assignee} (Floodline adviser)`
|
|
27
|
+
const date = DateTime.fromJSDate(new Date(createdAt)).setZone('Europe/London').toFormat('HH:mm:ss a, dd LLLL yyyy')
|
|
28
|
+
|
|
29
|
+
string += `[${date}] ${author}: \n${text}\n\n`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
string = string.replace(/<a\b[^>]*>/i, '').replace(/<\/a>/i, '')
|
|
33
|
+
|
|
34
|
+
return encodeURIComponent(string)
|
|
35
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import React, { createContext, useEffect, useReducer, useMemo } from 'react'
|
|
2
|
+
import { ChatEvent } from '@nice-devone/nice-cxone-chat-web-sdk'
|
|
3
|
+
|
|
4
|
+
import { messageNotification } from '../lib/message-notification.js'
|
|
5
|
+
|
|
6
|
+
import { initialState, reducer } from './reducer.js'
|
|
7
|
+
import { CUSTOMER_ID_STORAGE_KEY, THREAD_ID_STORAGE_KEY, SETTINGS_STORAGE_KEY } from './constants.js'
|
|
8
|
+
|
|
9
|
+
export const AppContext = createContext(initialState)
|
|
10
|
+
|
|
11
|
+
export const AppProvider = ({ sdk, availability, options, children }) => {
|
|
12
|
+
const [state, dispatch] = useReducer(reducer, initialState)
|
|
13
|
+
|
|
14
|
+
const playSound = messageNotification(options.audioUrl)
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* SDK event handlers
|
|
18
|
+
*/
|
|
19
|
+
const onLiveChatRecovered = e => {
|
|
20
|
+
dispatch({ type: 'SET_AGENT', payload: e.detail.data.inboxAssignee })
|
|
21
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.contact.status })
|
|
22
|
+
dispatch({ type: 'SET_UNSEEN_COUNT', payload: e.detail.data.contact.customerStatistics.unseenMessagesCount })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const onAssignedAgentChanged = e => {
|
|
26
|
+
dispatch({ type: 'SET_AGENT', payload: e.detail.data.inboxAssignee })
|
|
27
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const onAgentTypingStarted = () => {
|
|
31
|
+
dispatch({ type: 'SET_AGENT_TYPING', payload: true })
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const onAgentTypingEnded = () => {
|
|
35
|
+
dispatch({ type: 'SET_AGENT_TYPING', payload: false })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const onMessageCreated = e => {
|
|
39
|
+
dispatch({ type: 'SET_MESSAGE', payload: e.detail.data.message })
|
|
40
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
41
|
+
dispatch({ type: 'SET_UNSEEN_COUNT', payload: e.detail.data.case.customerStatistics.unseenMessagesCount })
|
|
42
|
+
|
|
43
|
+
const isAudioOn = JSON.parse(window.localStorage.getItem(SETTINGS_STORAGE_KEY)).audio
|
|
44
|
+
|
|
45
|
+
if (isAudioOn && e.detail.data.message.direction === 'outbound') {
|
|
46
|
+
playSound()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const onContactStatusChanged = e => {
|
|
51
|
+
dispatch({ type: 'SET_AGENT_STATUS', payload: e.detail.data.case.status })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
sdk.onChatEvent('SetConsumerContactCustomFields', () => { console.log('Custom field set') })
|
|
56
|
+
sdk.onChatEvent(ChatEvent.LIVECHAT_RECOVERED, onLiveChatRecovered)
|
|
57
|
+
sdk.onChatEvent(ChatEvent.MESSAGE_CREATED, onMessageCreated)
|
|
58
|
+
sdk.onChatEvent(ChatEvent.AGENT_TYPING_STARTED, onAgentTypingStarted)
|
|
59
|
+
sdk.onChatEvent(ChatEvent.AGENT_TYPING_ENDED, onAgentTypingEnded)
|
|
60
|
+
sdk.onChatEvent(ChatEvent.ASSIGNED_AGENT_CHANGED, onAssignedAgentChanged)
|
|
61
|
+
sdk.onChatEvent(ChatEvent.CONTACT_STATUS_CHANGED, onContactStatusChanged)
|
|
62
|
+
}, [sdk])
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Initialize customerId, threadId and whether the webchat should be open
|
|
66
|
+
*/
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
dispatch({ type: 'SET_AVAILABILITY', payload: availability })
|
|
69
|
+
|
|
70
|
+
setCustomerId(window.localStorage.getItem(CUSTOMER_ID_STORAGE_KEY))
|
|
71
|
+
setThreadId(window.localStorage.getItem(THREAD_ID_STORAGE_KEY))
|
|
72
|
+
setSettings(JSON.parse(window.localStorage.getItem(SETTINGS_STORAGE_KEY)) || state.settings)
|
|
73
|
+
|
|
74
|
+
if (window.location.hash === '#webchat') {
|
|
75
|
+
setChatVisibility(true)
|
|
76
|
+
}
|
|
77
|
+
}, [])
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* State update functions
|
|
81
|
+
*/
|
|
82
|
+
const setChatVisibility = payload => {
|
|
83
|
+
if (!payload) {
|
|
84
|
+
window.location.hash = ''
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
dispatch({ type: 'SET_CHAT_VISIBILITY', payload })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const setCustomerId = customerId => {
|
|
91
|
+
dispatch({ type: 'SET_CUSTOMER_ID', payload: customerId })
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const setThreadId = threadId => {
|
|
95
|
+
dispatch({ type: 'SET_THREAD_ID', payload: threadId })
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const setThread = thread => {
|
|
99
|
+
dispatch({ type: 'SET_THREAD', payload: thread })
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const setMessages = messages => {
|
|
103
|
+
dispatch({ type: 'SET_MESSAGES', payload: messages })
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const setSettings = data => {
|
|
107
|
+
dispatch({ type: 'SET_SETTINGS', payload: data })
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const setUnseenCount = unseenCount => {
|
|
111
|
+
dispatch({ type: 'SET_UNSEEN_COUNT', payload: unseenCount })
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Application-wide state and state functions
|
|
116
|
+
*/
|
|
117
|
+
const store = useMemo(() => ({
|
|
118
|
+
...state,
|
|
119
|
+
sdk,
|
|
120
|
+
setSettings,
|
|
121
|
+
setCustomerId,
|
|
122
|
+
setThreadId,
|
|
123
|
+
setThread,
|
|
124
|
+
setMessages,
|
|
125
|
+
setUnseenCount,
|
|
126
|
+
setChatVisibility,
|
|
127
|
+
onLiveChatRecovered,
|
|
128
|
+
onAssignedAgentChanged,
|
|
129
|
+
onAgentTypingStarted,
|
|
130
|
+
onAgentTypingEnded,
|
|
131
|
+
onMessageCreated,
|
|
132
|
+
onContactStatusChanged
|
|
133
|
+
}))
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<AppContext.Provider value={store}>
|
|
137
|
+
{children}
|
|
138
|
+
</AppContext.Provider>
|
|
139
|
+
)
|
|
140
|
+
}
|