@defra/flood-webchat 0.0.1-beta.50 → 0.0.1-beta.52
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/package.json +1 -1
- package/docs/assets/create-a-new-release.png +0 -0
- package/docs/assets/example-pre-release.png +0 -0
- package/docs/assets/example-release.png +0 -0
- package/docs/development-guide.md +0 -5
- package/docs/how-to-publish.md +0 -41
- package/jest.config.mjs +0 -29
- package/src/client/components/availability/availability.jsx +0 -130
- package/src/client/components/availability/availability.scss +0 -77
- package/src/client/components/chat/chat.jsx +0 -233
- package/src/client/components/chat/chat.scss +0 -260
- package/src/client/components/errorSummary/error-summary.jsx +0 -34
- package/src/client/components/live-region.jsx +0 -55
- package/src/client/components/message/message.jsx +0 -21
- package/src/client/components/panel/panel-footer.jsx +0 -9
- package/src/client/components/panel/panel-header.jsx +0 -75
- package/src/client/components/panel/panel.jsx +0 -163
- package/src/client/components/panel/panel.scss +0 -112
- package/src/client/components/screens/end-chat.jsx +0 -77
- package/src/client/components/screens/feedback.jsx +0 -65
- package/src/client/components/screens/pre-chat.jsx +0 -62
- package/src/client/components/screens/request-chat.jsx +0 -183
- package/src/client/components/screens/settings.jsx +0 -97
- package/src/client/components/screens/unavailable.jsx +0 -23
- package/src/client/components/skip-link.jsx +0 -42
- package/src/client/hooks/useFocusedElements.js +0 -80
- package/src/client/hooks/useTextareaAutosize.js +0 -13
- package/src/client/index.jsx +0 -33
- package/src/client/lib/agent-status-headline.js +0 -17
- package/src/client/lib/check-availability.js +0 -8
- package/src/client/lib/classnames.js +0 -1
- package/src/client/lib/history.js +0 -13
- package/src/client/lib/message-notification.js +0 -30
- package/src/client/lib/transform-messages.js +0 -47
- package/src/client/scss/_objects.scss +0 -33
- package/src/client/scss/_settings.scss +0 -101
- package/src/client/scss/_tools.scss +0 -33
- package/src/client/scss/_utilities.scss +0 -25
- package/src/client/store/AppProvider.jsx +0 -183
- package/src/client/store/actions-map.js +0 -152
- package/src/client/store/constants.js +0 -3
- package/src/client/store/reducer.js +0 -31
- package/src/client/store/useApp.js +0 -5
- package/src/client/store/useChatSdk.js +0 -37
- package/src/server/index.js +0 -60
- package/src/server/lib/client.js +0 -88
- package/src/server/lib/utils.js +0 -23
- package/webpack.config.mjs +0 -30
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { transformMessages, transformMessage } from '../lib/transform-messages'
|
|
2
|
-
import { CUSTOMER_ID_STORAGE_KEY, THREAD_ID_STORAGE_KEY, SETTINGS_STORAGE_KEY } from './constants'
|
|
3
|
-
|
|
4
|
-
const setSettings = (state, payload) => {
|
|
5
|
-
if (payload) {
|
|
6
|
-
window.localStorage.setItem(SETTINGS_STORAGE_KEY, JSON.stringify(payload))
|
|
7
|
-
} else {
|
|
8
|
-
window.localStorage.removeItem(SETTINGS_STORAGE_KEY)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
return {
|
|
12
|
-
...state,
|
|
13
|
-
settings: payload
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const setCustomerId = (state, payload) => {
|
|
18
|
-
if (payload) {
|
|
19
|
-
window.localStorage.setItem(CUSTOMER_ID_STORAGE_KEY, payload)
|
|
20
|
-
} else {
|
|
21
|
-
window.localStorage.removeItem(CUSTOMER_ID_STORAGE_KEY)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
...state,
|
|
26
|
-
customerId: payload
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const setThreadId = (state, payload) => {
|
|
31
|
-
if (payload) {
|
|
32
|
-
window.localStorage.setItem(THREAD_ID_STORAGE_KEY, payload)
|
|
33
|
-
} else {
|
|
34
|
-
window.localStorage.removeItem(THREAD_ID_STORAGE_KEY)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
...state,
|
|
39
|
-
threadId: payload
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const setChatVisibility = (state, payload) => {
|
|
44
|
-
return {
|
|
45
|
-
...state,
|
|
46
|
-
isChatOpen: payload
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const setAvailability = (state, payload) => {
|
|
51
|
-
return {
|
|
52
|
-
...state,
|
|
53
|
-
availability: payload
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const setThread = (state, payload) => {
|
|
58
|
-
return {
|
|
59
|
-
...state,
|
|
60
|
-
thread: payload
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const setMessages = (state, payload) => {
|
|
65
|
-
return {
|
|
66
|
-
...state,
|
|
67
|
-
messages: transformMessages(payload).reverse()
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const setMessage = (state, payload) => {
|
|
72
|
-
return {
|
|
73
|
-
...state,
|
|
74
|
-
message: transformMessage(payload),
|
|
75
|
-
messages: [...state.messages, transformMessage(payload)]
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const setAgent = (state, payload) => {
|
|
80
|
-
return {
|
|
81
|
-
...state,
|
|
82
|
-
agent: payload
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const setAgentIsTyping = (state, payload) => {
|
|
87
|
-
return {
|
|
88
|
-
...state,
|
|
89
|
-
isAgentTyping: payload
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const setAgentStatus = (state, payload) => {
|
|
94
|
-
return {
|
|
95
|
-
...state,
|
|
96
|
-
agentStatus: payload
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const setUnseenCount = (state, payload) => {
|
|
101
|
-
return {
|
|
102
|
-
...state,
|
|
103
|
-
unseenCount: payload
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const setInstigatorId = (state, payload) => {
|
|
108
|
-
return {
|
|
109
|
-
...state,
|
|
110
|
-
instigatorId: payload
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const setLiveRegionText = (state, payload) => {
|
|
115
|
-
return {
|
|
116
|
-
...state,
|
|
117
|
-
liveRegionText: payload
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const toggleIsMobile = (state, payload) => {
|
|
122
|
-
return {
|
|
123
|
-
...state,
|
|
124
|
-
isMobile: payload
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const toggleIsKeyboard = (state, payload) => {
|
|
129
|
-
return {
|
|
130
|
-
...state,
|
|
131
|
-
isKeyboard: payload
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export const actionsMap = {
|
|
136
|
-
SET_CHAT_VISIBILITY: setChatVisibility,
|
|
137
|
-
SET_AVAILABILITY: setAvailability,
|
|
138
|
-
SET_SETTINGS: setSettings,
|
|
139
|
-
SET_CUSTOMER_ID: setCustomerId,
|
|
140
|
-
SET_THREAD_ID: setThreadId,
|
|
141
|
-
SET_THREAD: setThread,
|
|
142
|
-
SET_MESSAGE: setMessage,
|
|
143
|
-
SET_MESSAGES: setMessages,
|
|
144
|
-
SET_AGENT: setAgent,
|
|
145
|
-
SET_AGENT_TYPING: setAgentIsTyping,
|
|
146
|
-
SET_AGENT_STATUS: setAgentStatus,
|
|
147
|
-
SET_UNSEEN_COUNT: setUnseenCount,
|
|
148
|
-
SET_INSTIGATOR_ID: setInstigatorId,
|
|
149
|
-
SET_LIVE_REGION_TEXT: setLiveRegionText,
|
|
150
|
-
TOGGLE_IS_MOBILE: toggleIsMobile,
|
|
151
|
-
TOGGLE_IS_KEYBOARD: toggleIsKeyboard
|
|
152
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { actionsMap } from './actions-map'
|
|
2
|
-
|
|
3
|
-
export const initialState = {
|
|
4
|
-
availability: null,
|
|
5
|
-
customerId: null,
|
|
6
|
-
threadId: null,
|
|
7
|
-
thread: null,
|
|
8
|
-
messages: [],
|
|
9
|
-
unseenCount: 0,
|
|
10
|
-
instigatorId: null,
|
|
11
|
-
agent: null,
|
|
12
|
-
agentStatus: null,
|
|
13
|
-
isAgentTyping: false,
|
|
14
|
-
isChatOpen: false,
|
|
15
|
-
isMobile: window.matchMedia('(max-width: 640px)').matches,
|
|
16
|
-
isKeyboard: false,
|
|
17
|
-
settings: { audio: true, scroll: true }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const reducer = (state, action) => {
|
|
21
|
-
const { type, payload } = action
|
|
22
|
-
|
|
23
|
-
const fn = actionsMap[type]
|
|
24
|
-
|
|
25
|
-
if (fn) {
|
|
26
|
-
const actionFunction = fn.bind(this, state, payload)
|
|
27
|
-
return actionFunction()
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return state
|
|
31
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
export const useChatSdk = sdk => {
|
|
2
|
-
const connect = async () => sdk.authorize()
|
|
3
|
-
|
|
4
|
-
const fetchCustomerId = async () => {
|
|
5
|
-
const response = await connect()
|
|
6
|
-
return response?.consumerIdentity.idOnExternalPlatform
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const fetchThread = async threadId => {
|
|
10
|
-
await connect()
|
|
11
|
-
return sdk.getThread(threadId)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const fetchMessages = async (thread, threadId) => {
|
|
15
|
-
await connect()
|
|
16
|
-
|
|
17
|
-
const recovered = await thread.recover(threadId)
|
|
18
|
-
|
|
19
|
-
const allMessages = []
|
|
20
|
-
let fetchedMessages = recovered.messages
|
|
21
|
-
|
|
22
|
-
while (fetchedMessages.length) {
|
|
23
|
-
fetchedMessages.map(msg => allMessages.push(msg))
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
const response = await thread.loadMoreMessages()
|
|
27
|
-
fetchedMessages = response.data.messages
|
|
28
|
-
} catch (err) {
|
|
29
|
-
fetchedMessages = []
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return allMessages
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return { connect, fetchCustomerId, fetchThread, fetchMessages }
|
|
37
|
-
}
|
package/src/server/index.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const { authenticate, getApiBaseUrl, getIsOpen, getActivity } = require('./lib/client.js')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns webchat availability
|
|
5
|
-
* @param options {object}
|
|
6
|
-
* @param options.clientId {string}
|
|
7
|
-
* @param options.clientSecret {string}
|
|
8
|
-
* @param options.accessKey {string}
|
|
9
|
-
* @param options.accessSecret {string}
|
|
10
|
-
* @param options.skillEndpoint {string}
|
|
11
|
-
* @param options.hoursEndpoint {string}
|
|
12
|
-
* @param options.authenticationUri {string}
|
|
13
|
-
* @param options.wellKnownUri {string}
|
|
14
|
-
* @param options.maxQueueCount {string}
|
|
15
|
-
* @returns {Promise<{date: Date, availability: (string)}>}
|
|
16
|
-
*/
|
|
17
|
-
module.exports = async function getAvailability ({
|
|
18
|
-
clientId,
|
|
19
|
-
clientSecret,
|
|
20
|
-
accessKey,
|
|
21
|
-
accessSecret,
|
|
22
|
-
maxQueueCount,
|
|
23
|
-
skillEndpoint,
|
|
24
|
-
hoursEndpoint,
|
|
25
|
-
wellKnownUri = 'https://cxone.niceincontact.com/.well-known/cxone-configuration',
|
|
26
|
-
authenticationUri = 'https://cxone.niceincontact.com/auth/token'
|
|
27
|
-
}) {
|
|
28
|
-
const authorisation = 'Basic ' + Buffer.from(`${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret)}`).toString('base64')
|
|
29
|
-
|
|
30
|
-
// Cache authentication and re-authenticate when needed (lasts 1 hour?)
|
|
31
|
-
const { tenantId, token, tokenType } = await authenticate({
|
|
32
|
-
authenticationUri,
|
|
33
|
-
authorisation,
|
|
34
|
-
accessKey,
|
|
35
|
-
accessSecret
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const apiBaseUrl = await getApiBaseUrl({ wellKnownUri, tenantId })
|
|
39
|
-
|
|
40
|
-
const [{ hasCapacity, hasAgentsAvailable }, isOpen] = await Promise.all([
|
|
41
|
-
getActivity({ baseUrl: apiBaseUrl, tokenType, token, skillEndpoint, maxQueueCount }),
|
|
42
|
-
getIsOpen({ baseUrl: apiBaseUrl, token, tokenType, hoursEndpoint })
|
|
43
|
-
])
|
|
44
|
-
|
|
45
|
-
const isAvailable = isOpen && hasAgentsAvailable && hasCapacity
|
|
46
|
-
const isExistingOnly = isOpen && hasAgentsAvailable && !hasCapacity
|
|
47
|
-
|
|
48
|
-
let availability = 'UNAVAILABLE'
|
|
49
|
-
|
|
50
|
-
if (isAvailable) {
|
|
51
|
-
availability = 'AVAILABLE'
|
|
52
|
-
} else if (isExistingOnly) {
|
|
53
|
-
availability = 'EXISTING'
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
date: new Date(),
|
|
58
|
-
availability
|
|
59
|
-
}
|
|
60
|
-
}
|
package/src/server/lib/client.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
const querystring = require('querystring')
|
|
2
|
-
const axios = require('axios')
|
|
3
|
-
const jwtdecode = require('jwt-decode')
|
|
4
|
-
const { isWithinHours } = require('./utils.js')
|
|
5
|
-
|
|
6
|
-
const contentType = 'application/x-www-form-urlencoded'
|
|
7
|
-
|
|
8
|
-
const authenticate = async ({ authenticationUri, authorisation, accessKey, accessSecret }) => {
|
|
9
|
-
const url = new URL(authenticationUri)
|
|
10
|
-
const body = querystring.stringify({
|
|
11
|
-
grant_type: 'password',
|
|
12
|
-
username: accessKey,
|
|
13
|
-
password: accessSecret
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
const auth = await axios.post(url.href, body, {
|
|
17
|
-
signal: AbortSignal.timeout(3000),
|
|
18
|
-
headers: {
|
|
19
|
-
Host: url.host,
|
|
20
|
-
'Content-Type': contentType,
|
|
21
|
-
Authorization: authorisation
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
token: auth.data.access_token,
|
|
27
|
-
tokenType: auth.data.token_type,
|
|
28
|
-
tenantId: jwtdecode(auth.data.id_token)?.tenantId
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const getApiBaseUrl = async ({ wellKnownUri, tenantId }) => {
|
|
33
|
-
const url = new URL(wellKnownUri)
|
|
34
|
-
url.searchParams.set('tenantId', tenantId)
|
|
35
|
-
|
|
36
|
-
const { data } = await axios.get(url.href, {
|
|
37
|
-
headers: {
|
|
38
|
-
Host: url.host
|
|
39
|
-
},
|
|
40
|
-
signal: AbortSignal.timeout(3000)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
return data.api_endpoint
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const getActivity = async ({ tokenType, token, baseUrl, skillEndpoint, maxQueueCount }) => {
|
|
47
|
-
const url = new URL(skillEndpoint, baseUrl)
|
|
48
|
-
|
|
49
|
-
const skill = await axios.get(url.href, {
|
|
50
|
-
signal: AbortSignal.timeout(3000),
|
|
51
|
-
headers: {
|
|
52
|
-
Host: url.host,
|
|
53
|
-
Authorization: `${tokenType} ${token}`,
|
|
54
|
-
'Content-Type': contentType
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
const activity = skill.data.skillActivity[0]
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
hasCapacity: activity.queueCount < maxQueueCount,
|
|
62
|
-
hasAgentsAvailable: activity.agentsAvailable >= 1
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const getIsOpen = async ({ baseUrl, token, tokenType, hoursEndpoint }) => {
|
|
67
|
-
const url = new URL(hoursEndpoint, baseUrl)
|
|
68
|
-
|
|
69
|
-
const hours = await axios.get(url.href, {
|
|
70
|
-
signal: AbortSignal.timeout(3000),
|
|
71
|
-
headers: {
|
|
72
|
-
Host: url.host,
|
|
73
|
-
Authorization: `${tokenType} ${token}`,
|
|
74
|
-
'Content-Type': contentType
|
|
75
|
-
}
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
const days = hours.data.resultSet.hoursOfOperationProfiles[0].days
|
|
79
|
-
|
|
80
|
-
return isWithinHours(days)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
module.exports = {
|
|
84
|
-
authenticate,
|
|
85
|
-
getApiBaseUrl,
|
|
86
|
-
getIsOpen,
|
|
87
|
-
getActivity
|
|
88
|
-
}
|
package/src/server/lib/utils.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const { DateTime } = require('luxon')
|
|
2
|
-
|
|
3
|
-
const getHour = time => Number(time.split(':')[0])
|
|
4
|
-
|
|
5
|
-
const isWithinHours = (days, date) => {
|
|
6
|
-
const now = date ? DateTime.fromISO(date) : DateTime.local()
|
|
7
|
-
now.setZone('Europe/London')
|
|
8
|
-
|
|
9
|
-
const newDate = date ? new Date(date) : new Date()
|
|
10
|
-
|
|
11
|
-
const today = newDate.toLocaleDateString('en-GB', { weekday: 'long' })
|
|
12
|
-
const dateParts = newDate.toLocaleDateString('en-GB').split('/')
|
|
13
|
-
|
|
14
|
-
const todaysAvailability = days.find(item => item.day === today)
|
|
15
|
-
const todaysDateTimeOpen = DateTime.local(Number(dateParts[2]), Number(dateParts[1]), Number(dateParts[0]), getHour(todaysAvailability.openTime))
|
|
16
|
-
const todaysDateTimeClose = DateTime.local(Number(dateParts[2]), Number(dateParts[1]), Number(dateParts[0]), getHour(todaysAvailability.closeTime))
|
|
17
|
-
|
|
18
|
-
return now.diff(todaysDateTimeOpen).milliseconds >= 0 && now.diff(todaysDateTimeClose).milliseconds <= 0
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = {
|
|
22
|
-
isWithinHours
|
|
23
|
-
}
|
package/webpack.config.mjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
|
|
3
|
-
import nodeExternals from 'webpack-node-externals'
|
|
4
|
-
|
|
5
|
-
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
|
6
|
-
|
|
7
|
-
export default {
|
|
8
|
-
entry: {
|
|
9
|
-
client: path.join(__dirname, 'src/client/index.jsx'),
|
|
10
|
-
server: path.join(__dirname, 'src/server/index.js')
|
|
11
|
-
},
|
|
12
|
-
output: {
|
|
13
|
-
path: path.resolve(__dirname, 'dist'),
|
|
14
|
-
library: {
|
|
15
|
-
type: 'commonjs2'
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
target: 'node',
|
|
19
|
-
externals: [nodeExternals()],
|
|
20
|
-
module: {
|
|
21
|
-
rules: [
|
|
22
|
-
{
|
|
23
|
-
test: /\.jsx?$/i,
|
|
24
|
-
use: [
|
|
25
|
-
'babel-loader'
|
|
26
|
-
]
|
|
27
|
-
}
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
}
|