@defra/flood-webchat 0.0.1-beta.3 → 0.0.1-beta.30
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 +1876 -215
- package/dist/client.js.map +1 -1
- package/dist/server.js +181 -240
- package/dist/server.js.map +1 -1
- package/main.scss +11 -6
- package/package.json +6 -2
- package/src/client/components/availability/availability.jsx +59 -49
- package/src/client/components/availability/availability.scss +7 -33
- package/src/client/components/chat/chat.jsx +176 -0
- package/src/client/components/chat/chat.scss +221 -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 +75 -0
- package/src/client/components/panel/panel.jsx +144 -0
- package/src/client/components/panel/panel.scss +112 -0
- package/src/client/components/screens/end-chat.jsx +39 -0
- package/src/client/components/screens/feedback.jsx +23 -0
- package/src/client/components/screens/pre-chat.jsx +62 -0
- package/src/client/components/screens/request-chat.jsx +173 -0
- package/src/client/components/screens/settings.jsx +69 -0
- package/src/client/components/screens/unavailable.jsx +23 -0
- package/src/client/hooks/useFocusedElements.js +81 -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/history.js +13 -0
- package/src/client/lib/message-notification.js +36 -0
- package/src/client/lib/transform-messages.js +34 -0
- package/src/client/scss/_objects.scss +33 -0
- package/src/client/scss/_settings.scss +75 -0
- package/src/client/scss/_tools.scss +33 -0
- package/src/client/scss/_utilities.scss +25 -0
- package/src/client/store/AppProvider.jsx +174 -0
- package/src/client/store/actions-map.js +135 -0
- package/src/client/store/constants.js +3 -0
- package/src/client/store/reducer.js +30 -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,62 @@
|
|
|
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
|
+
<div className='wc-content'>
|
|
11
|
+
<h3 className='wc-heading' aria-live='polite'>What you can use webchat for</h3>
|
|
12
|
+
<p>Webchat lets you talk directly to a Floodline adviser.</p>
|
|
13
|
+
|
|
14
|
+
<p>You can use webchat to get:</p>
|
|
15
|
+
<ul className='wc-list'>
|
|
16
|
+
<li>current flood warnings and alerts in your area</li>
|
|
17
|
+
<li>information on the flood warning service</li>
|
|
18
|
+
<li>advice on what to do before, during and after a flood</li>
|
|
19
|
+
</ul>
|
|
20
|
+
<p>
|
|
21
|
+
There are other ways to
|
|
22
|
+
<a className='govuk-link' href='https://www.gov.uk/sign-up-for-flood-warnings' target='_blank' rel='noreferrer'>
|
|
23
|
+
sign up for flood warnings
|
|
24
|
+
</a>
|
|
25
|
+
and
|
|
26
|
+
<a className='govuk-link' href='https://www.fws.environment-agency.gov.uk/app/olr/login' target='_blank' rel='noreferrer'>
|
|
27
|
+
manage your flood warnings account
|
|
28
|
+
</a>.
|
|
29
|
+
</p>
|
|
30
|
+
<p>
|
|
31
|
+
Do not use webchat to
|
|
32
|
+
<a
|
|
33
|
+
className='govuk-link'
|
|
34
|
+
href='https://www.gov.uk/report-flood-cause'
|
|
35
|
+
target='_blank'
|
|
36
|
+
rel='noreferrer'
|
|
37
|
+
>
|
|
38
|
+
report a flood
|
|
39
|
+
</a>.
|
|
40
|
+
</p>
|
|
41
|
+
|
|
42
|
+
<button className='govuk-button wc-button govuk-!-margin-top-1' data-module='govuk-button' onClick={onContinue}>Continue</button>
|
|
43
|
+
|
|
44
|
+
<h3 className='wc-heading'>Other flood information</h3>
|
|
45
|
+
|
|
46
|
+
<p>Call Floodline for all other flood and flood warning information.</p>
|
|
47
|
+
<p>
|
|
48
|
+
<strong>Floodline helpline</strong>
|
|
49
|
+
<br />
|
|
50
|
+
Telephone: 0345 988 1188
|
|
51
|
+
<br />
|
|
52
|
+
Textphone: 0345 602 6340
|
|
53
|
+
<br />
|
|
54
|
+
Open 24 hours a day, 7 days a week
|
|
55
|
+
<br />
|
|
56
|
+
<a className='govuk-link' href='https://gov.uk/call-charges' target='_blank' rel='noreferrer'>Find out more about call charges</a>
|
|
57
|
+
</p>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
</>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
const [errors, setErrors] = useState({})
|
|
16
|
+
const [questionLength, setQuestionLength] = useState(0)
|
|
17
|
+
const [isButtonDisabled, setButtonDisabled] = useState(false)
|
|
18
|
+
|
|
19
|
+
const nameRef = useRef()
|
|
20
|
+
const questionRef = useRef()
|
|
21
|
+
|
|
22
|
+
const isQuestionLengthValid = QUESTION_MAX_LENGTH >= questionLength
|
|
23
|
+
const questionLengthRemaining = QUESTION_MAX_LENGTH - questionLength
|
|
24
|
+
const questionLengthExceeded = questionLength - QUESTION_MAX_LENGTH
|
|
25
|
+
|
|
26
|
+
let questionHint = `You have ${questionLengthRemaining} characters remaining`
|
|
27
|
+
|
|
28
|
+
if (!isQuestionLengthValid) {
|
|
29
|
+
questionHint = `You have ${questionLengthExceeded} characters too many`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (Object.keys(errors).length) {
|
|
34
|
+
document.querySelector('.govuk-error-summary')?.focus()
|
|
35
|
+
}
|
|
36
|
+
}, [errors])
|
|
37
|
+
|
|
38
|
+
const onQuestionChange = e => {
|
|
39
|
+
setQuestionLength(e.target.value.length)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const buttonLabel = isButtonDisabled ? 'Requesting...' : 'Request Chat'
|
|
43
|
+
|
|
44
|
+
const onRequestChat = async e => {
|
|
45
|
+
e.preventDefault()
|
|
46
|
+
|
|
47
|
+
const errs = {}
|
|
48
|
+
|
|
49
|
+
if (nameRef.current.value.length === 0) {
|
|
50
|
+
errs.name = 'Enter your name'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (questionRef.current.value.length === 0) {
|
|
54
|
+
errs.question = 'Enter your question'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (questionRef.current.value.length > QUESTION_MAX_LENGTH) {
|
|
58
|
+
errs.question = 'Your question must be 500 characters or less'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (Object.keys(errs).length === 0) {
|
|
62
|
+
try {
|
|
63
|
+
setButtonDisabled(true)
|
|
64
|
+
const threadId = uuid.v4()
|
|
65
|
+
const customerId = await fetchCustomerId()
|
|
66
|
+
const thread = await fetchThread(threadId)
|
|
67
|
+
|
|
68
|
+
sdk.getCustomer().setName(nameRef.current.value)
|
|
69
|
+
|
|
70
|
+
await thread.startChat(questionRef.current.value || 'Begin conversation')
|
|
71
|
+
|
|
72
|
+
setCustomerId(customerId)
|
|
73
|
+
setThreadId(threadId)
|
|
74
|
+
setThread(thread)
|
|
75
|
+
thread.setCustomField('threadid', threadId)
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.log('[Request Chat Error]', err)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
setErrors(errs)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<>
|
|
86
|
+
<PanelHeader />
|
|
87
|
+
|
|
88
|
+
<div className='wc-body'>
|
|
89
|
+
<div className='wc-content'>
|
|
90
|
+
<a href='#' className='wc-back-link govuk-back-link' onClick={onPreChatScreen}>
|
|
91
|
+
What you can use webchat for
|
|
92
|
+
</a>
|
|
93
|
+
|
|
94
|
+
<ErrorSummary errors={errors} />
|
|
95
|
+
|
|
96
|
+
<h3 className='wc-heading' aria-live='polite'>
|
|
97
|
+
Your name and question
|
|
98
|
+
</h3>
|
|
99
|
+
|
|
100
|
+
<form>
|
|
101
|
+
<div className={classnames('wc-form-group', 'govuk-form-group', errors.name && 'govuk-form-group--error')}>
|
|
102
|
+
<label className='wc-label govuk-label' htmlFor='wc-name'>
|
|
103
|
+
Your name
|
|
104
|
+
</label>
|
|
105
|
+
{errors.name && (
|
|
106
|
+
<p className='govuk-error-message'>
|
|
107
|
+
<span className='govuk-visually-hidden'>Error:</span> {errors.name}
|
|
108
|
+
</p>
|
|
109
|
+
)}
|
|
110
|
+
<input
|
|
111
|
+
ref={nameRef}
|
|
112
|
+
className={classnames('wc-input', 'govuk-input', errors.name && 'govuk-input--error')}
|
|
113
|
+
id='wc-name'
|
|
114
|
+
name='name'
|
|
115
|
+
type='text'
|
|
116
|
+
data-testid='request-chat-user-name'
|
|
117
|
+
/>
|
|
118
|
+
</div>
|
|
119
|
+
|
|
120
|
+
<div className='wc-form-group govuk-character-count' data-module='govuk-character-count' data-maxlength='500'>
|
|
121
|
+
<div className={classnames('govuk-form-group', errors.question && 'govuk-form-group--error')}>
|
|
122
|
+
<label className='wc-label govuk-label' htmlFor='wc-question'>
|
|
123
|
+
Your question
|
|
124
|
+
</label>
|
|
125
|
+
{errors.question && (
|
|
126
|
+
<p className='govuk-error-message'>
|
|
127
|
+
<span className='govuk-visually-hidden'>Error:</span>
|
|
128
|
+
{errors.question}
|
|
129
|
+
</p>
|
|
130
|
+
)}
|
|
131
|
+
<textarea
|
|
132
|
+
ref={questionRef}
|
|
133
|
+
id='wc-question'
|
|
134
|
+
name='question'
|
|
135
|
+
rows='5'
|
|
136
|
+
aria-describedby='wc-question-info'
|
|
137
|
+
onChange={onQuestionChange}
|
|
138
|
+
className={classnames('wc-textarea', 'govuk-textarea', 'govuk-js-character-count', !isQuestionLengthValid || errors.question ? 'govuk-textarea--error' : '')}
|
|
139
|
+
data-testid='request-chat-user-question'
|
|
140
|
+
/>
|
|
141
|
+
<div
|
|
142
|
+
id='wc-question-info'
|
|
143
|
+
className='wc-hint govuk-hint govuk-char-count__msg'
|
|
144
|
+
style={{ color: `${!isQuestionLengthValid ? '#d4351c' : ''}` }}
|
|
145
|
+
aria-hidden='true'
|
|
146
|
+
>
|
|
147
|
+
{questionHint}
|
|
148
|
+
</div>
|
|
149
|
+
<div className='govuk-character-count__sr-status govuk-visually-hidden' aria-live='polite'>
|
|
150
|
+
{questionHint}
|
|
151
|
+
</div>
|
|
152
|
+
</div>
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
<div className='wc-inset-text govuk-inset-text'>
|
|
156
|
+
By selecting 'Request chat' you agree to the terms of our
|
|
157
|
+
<a href='https://check-for-flooding.service.gov.uk/privacy-notice' target='_blank' rel='noreferrer' className='govuk-link'>privacy notice</a>.
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
<button
|
|
161
|
+
className='wc-button govuk-button govuk-!-margin-top-1'
|
|
162
|
+
data-module='govuk-button'
|
|
163
|
+
onClick={onRequestChat}
|
|
164
|
+
disabled={isButtonDisabled}
|
|
165
|
+
>
|
|
166
|
+
{buttonLabel}
|
|
167
|
+
</button>
|
|
168
|
+
</form>
|
|
169
|
+
</div>
|
|
170
|
+
</div>
|
|
171
|
+
</>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
<div className='wc-content'>
|
|
25
|
+
<fieldset className='govuk-fieldset govuk-!-margin-bottom-4'>
|
|
26
|
+
<legend className='govuk-fieldset__legend govuk-fieldset__legend'>
|
|
27
|
+
<h3 id='wc-subtitle' className='wc-heading'>Change settings</h3>
|
|
28
|
+
</legend>
|
|
29
|
+
<div className='govuk-checkboxes govuk-checkboxes--small' data-module='govuk-checkboxes'>
|
|
30
|
+
<div className='govuk-checkboxes__item'>
|
|
31
|
+
<input
|
|
32
|
+
id='audio'
|
|
33
|
+
name='audio'
|
|
34
|
+
type='checkbox'
|
|
35
|
+
value='audio'
|
|
36
|
+
className='govuk-checkboxes__input'
|
|
37
|
+
defaultChecked={optionAudio}
|
|
38
|
+
onChange={() => setOptionAudio(!optionAudio)}
|
|
39
|
+
/>
|
|
40
|
+
<label className='wc-label govuk-label govuk-checkboxes__label' htmlFor='audio'>
|
|
41
|
+
Play a sound when receiving a new message
|
|
42
|
+
</label>
|
|
43
|
+
</div>
|
|
44
|
+
<div className='govuk-checkboxes__item'>
|
|
45
|
+
<input
|
|
46
|
+
id='scroll'
|
|
47
|
+
name='scroll'
|
|
48
|
+
type='checkbox'
|
|
49
|
+
value='scroll'
|
|
50
|
+
className='govuk-checkboxes__input'
|
|
51
|
+
defaultChecked={optionScroll}
|
|
52
|
+
onChange={() => setOptionScroll(!optionScroll)}
|
|
53
|
+
/>
|
|
54
|
+
<label className='wc-label govuk-label govuk-checkboxes__label' htmlFor='scroll'>
|
|
55
|
+
Scroll automatically to a new message
|
|
56
|
+
</label>
|
|
57
|
+
</div>
|
|
58
|
+
</div>
|
|
59
|
+
</fieldset>
|
|
60
|
+
|
|
61
|
+
<div className='govuk-button-group'>
|
|
62
|
+
<a id='settings-save' href='#' className='wc-button govuk-button' data-module='govuk-button' onClick={onSave}>Save</a>
|
|
63
|
+
<a id='settings-cancel' href='#' className='wc-link govuk-link' data-module='govuk-button' role='button' onClick={onCancel}>Cancel</a>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
</div>
|
|
67
|
+
</>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
@@ -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,81 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback } from 'react'
|
|
2
|
+
|
|
3
|
+
const ariaHidden = 'aria-hidden'
|
|
4
|
+
const dataWcInert = 'data-wc-inert'
|
|
5
|
+
|
|
6
|
+
const setAriaHidden = isInert => {
|
|
7
|
+
for (const node of document.body.children) {
|
|
8
|
+
if (node.id !== 'wc-panel') {
|
|
9
|
+
// We only want to toggle elements that aren't already inert
|
|
10
|
+
if (isInert && !node.getAttribute(ariaHidden)) {
|
|
11
|
+
node.setAttribute(ariaHidden, 'true')
|
|
12
|
+
node.setAttribute(dataWcInert, '')
|
|
13
|
+
} else if (node.hasAttribute(dataWcInert)) {
|
|
14
|
+
node.removeAttribute(ariaHidden)
|
|
15
|
+
node.removeAttribute(dataWcInert)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const getFocusableElements = () => {
|
|
22
|
+
const selectors = [
|
|
23
|
+
'#wc-panel a:not([disabled])',
|
|
24
|
+
'#wc-panel button:not([disabled])',
|
|
25
|
+
'#wc-panel select:not([disabled])',
|
|
26
|
+
'#wc-panel input:not([disabled])',
|
|
27
|
+
'#wc-panel textarea:not([disabled])',
|
|
28
|
+
'#wc-panel *[tabindex="0"]:not([disabled])'
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
const elements = document.body.querySelectorAll(selectors.join(','))
|
|
32
|
+
return Array.from(elements).filter(e => !e.closest('[hidden]') && !e.closest('[aria-hidden="true"]'))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const useFocusedElements = screen => {
|
|
36
|
+
const [panelElements, setPanelElements] = useState([])
|
|
37
|
+
|
|
38
|
+
const onKeyDown = useCallback(e => {
|
|
39
|
+
const webchatPanelElement = document.querySelector('#wc-panel')
|
|
40
|
+
|
|
41
|
+
if (e.key === 'Tab') {
|
|
42
|
+
if (webchatPanelElement && !document.activeElement.closest('#wc-panel')) {
|
|
43
|
+
webchatPanelElement.focus()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (e.shiftKey) {
|
|
47
|
+
if (document.activeElement === panelElements[0]) {
|
|
48
|
+
panelElements[panelElements.length - 1].focus()
|
|
49
|
+
e.preventDefault()
|
|
50
|
+
} else if (document.activeElement === document.querySelector('#wc-panel')) {
|
|
51
|
+
panelElements[panelElements.length - 1]?.focus()
|
|
52
|
+
e.preventDefault()
|
|
53
|
+
}
|
|
54
|
+
} else if (document.activeElement === panelElements[panelElements.length - 1]) {
|
|
55
|
+
panelElements[0].focus()
|
|
56
|
+
e.preventDefault()
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}, [panelElements])
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
setPanelElements(getFocusableElements())
|
|
63
|
+
}, [screen])
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
setAriaHidden(true)
|
|
67
|
+
|
|
68
|
+
const panelElement = document.querySelector('#wc-panel')
|
|
69
|
+
panelElement.focus()
|
|
70
|
+
|
|
71
|
+
document.addEventListener('keydown', onKeyDown)
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
setAriaHidden()
|
|
75
|
+
document.body.querySelector('.wc-availability__link')?.focus()
|
|
76
|
+
document.removeEventListener('keydown', onKeyDown)
|
|
77
|
+
}
|
|
78
|
+
}, [panelElements])
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
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,13 @@
|
|
|
1
|
+
export const historyPushState = () => {
|
|
2
|
+
const url = window.location.href.split('#')[0]
|
|
3
|
+
window.history.pushState({ history: true }, null, `${url}#webchat`)
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const historyReplaceState = () => {
|
|
7
|
+
if (window.history.state?.history) {
|
|
8
|
+
window.history.back()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const url = window.location.href.split('#')[0]
|
|
12
|
+
window.history.replaceState(null, null, url)
|
|
13
|
+
}
|
|
@@ -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,34 @@
|
|
|
1
|
+
import { DateTime } from 'luxon'
|
|
2
|
+
|
|
3
|
+
export const transformMessage = message => {
|
|
4
|
+
return {
|
|
5
|
+
id: message.id,
|
|
6
|
+
text: message.messageContent?.text,
|
|
7
|
+
createdAt: new Date(message.createdAt),
|
|
8
|
+
user: message.authorEndUserIdentity?.fullName?.trim() || null,
|
|
9
|
+
assignee: message.authorUser?.firstName || null,
|
|
10
|
+
direction: message.direction
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const transformMessages = messages => messages.map(message => transformMessage(message))
|
|
15
|
+
|
|
16
|
+
export const formatTranscript = messages => {
|
|
17
|
+
const now = DateTime.local()
|
|
18
|
+
now.setZone('Europe/London')
|
|
19
|
+
|
|
20
|
+
let string = `Floodline webchat transcript, ${now.toFormat('HH:mm:ss a, dd LLLL yyyy')}\n\n`
|
|
21
|
+
|
|
22
|
+
for (const message of messages) {
|
|
23
|
+
const { text, direction, user, assignee, createdAt } = message
|
|
24
|
+
|
|
25
|
+
const author = direction === 'inbound' ? user : `${assignee} (Floodline adviser)`
|
|
26
|
+
const date = DateTime.fromJSDate(new Date(createdAt)).setZone('Europe/London').toFormat('HH:mm:ss a, dd LLLL yyyy')
|
|
27
|
+
|
|
28
|
+
string += `[${date}] ${author}: \n${text}\n\n`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
string = string.replace(/<a\b[^>]*>/i, '').replace(/<\/a>/i, '')
|
|
32
|
+
|
|
33
|
+
return encodeURIComponent(string)
|
|
34
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
%wc-link-button {
|
|
2
|
+
@extend %govuk-link;
|
|
3
|
+
position: relative;
|
|
4
|
+
display: inline-block;
|
|
5
|
+
font-size: 16px;
|
|
6
|
+
color: govuk-colour('black');
|
|
7
|
+
|
|
8
|
+
&:hover {
|
|
9
|
+
color: govuk-colour('black');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
&:visited:not(:hover):not(:focus) {
|
|
13
|
+
color: govuk-colour('black');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
&:active {
|
|
17
|
+
color: govuk-colour('black');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
&:focus {
|
|
21
|
+
box-shadow: none;
|
|
22
|
+
background-color: transparent;
|
|
23
|
+
text-decoration: underline;
|
|
24
|
+
color: govuk-colour('black');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
&:focus-visible {
|
|
28
|
+
background-color: $govuk-focus-colour;
|
|
29
|
+
box-shadow: 0 -2px $govuk-focus-colour, 0 4px govuk-colour('black');
|
|
30
|
+
text-decoration: none;
|
|
31
|
+
color: govuk-colour('black');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
.wc-heading {
|
|
2
|
+
@extend %govuk-heading-m;
|
|
3
|
+
font-size: 19px;
|
|
4
|
+
margin-bottom: 15px;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.wc-list {
|
|
8
|
+
@extend %govuk-list;
|
|
9
|
+
@extend %govuk-list--bullet;
|
|
10
|
+
font-size: 16px;
|
|
11
|
+
line-height: 1.25;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.wc-body p {
|
|
15
|
+
font-size: 16px;
|
|
16
|
+
line-height: 1.25;
|
|
17
|
+
margin-bottom: 15px;
|
|
18
|
+
@extend %govuk-body-s;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.wc-body .govuk-error-message {
|
|
22
|
+
color: govuk-colour('red');
|
|
23
|
+
font-weight: 700;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.wc-link,
|
|
27
|
+
.wc-button,
|
|
28
|
+
.govuk-button-group .wc-button,
|
|
29
|
+
.govuk-button-group .wc-link {
|
|
30
|
+
font-size: 16px;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.wc-label {
|
|
34
|
+
font-size: 16px;
|
|
35
|
+
line-height: 1.25;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.wc-form-group {
|
|
39
|
+
margin-bottom: 15px;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.wc-hint {
|
|
43
|
+
font-size: 16px;
|
|
44
|
+
line-height: 1.25;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.wc-inset-text {
|
|
48
|
+
margin: 0 0 20px;
|
|
49
|
+
padding: 5px 5px 5px 15px;
|
|
50
|
+
font-size: 16px;
|
|
51
|
+
border-left-width: 5px;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.wc-error-summary__title {
|
|
55
|
+
font-size: 19px;
|
|
56
|
+
line-height: 1.25;
|
|
57
|
+
margin-bottom: 15px;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.wc-error-summary__list,
|
|
61
|
+
.wc-error-message {
|
|
62
|
+
font-size: 16px;
|
|
63
|
+
line-height: 1.25;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.wc-input, .wc-textarea {
|
|
67
|
+
font-size: 16px;
|
|
68
|
+
line-height: 1.25;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.wc-back-link {
|
|
72
|
+
font-size: 16px;
|
|
73
|
+
line-height: 1.25;
|
|
74
|
+
margin: 0 0 25px;
|
|
75
|
+
}
|