@defra/flood-webchat 0.0.1-beta.2 → 0.0.1-beta.21
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 +1607 -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 +41 -0
- package/src/client/components/panel/panel.jsx +119 -0
- package/src/client/components/panel/panel.scss +72 -0
- package/src/client/components/screens/end-chat.jsx +17 -0
- package/src/client/components/screens/pre-chat.jsx +65 -0
- package/src/client/components/screens/request-chat.jsx +156 -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 +139 -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
package/src/server/lib/client.js
CHANGED
|
@@ -5,26 +5,23 @@ const { isWithinHours } = require('./utils.js')
|
|
|
5
5
|
|
|
6
6
|
const contentType = 'application/x-www-form-urlencoded'
|
|
7
7
|
|
|
8
|
-
const authenticate = async ({ authorisation, accessKey, accessSecret }) => {
|
|
9
|
-
const
|
|
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
|
+
})
|
|
10
15
|
|
|
11
|
-
const
|
|
16
|
+
const auth = await axios.post(url.href, body, {
|
|
12
17
|
signal: AbortSignal.timeout(3000),
|
|
13
18
|
headers: {
|
|
14
|
-
Host:
|
|
19
|
+
Host: url.host,
|
|
15
20
|
'Content-Type': contentType,
|
|
16
21
|
Authorization: authorisation
|
|
17
22
|
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const body = querystring.stringify({
|
|
21
|
-
grant_type: 'password',
|
|
22
|
-
username: accessKey,
|
|
23
|
-
password: accessSecret
|
|
24
23
|
})
|
|
25
24
|
|
|
26
|
-
const auth = await axios.post(uri, body, config)
|
|
27
|
-
|
|
28
25
|
return {
|
|
29
26
|
token: auth.data.access_token,
|
|
30
27
|
tokenType: auth.data.token_type,
|
|
@@ -32,30 +29,31 @@ const authenticate = async ({ authorisation, accessKey, accessSecret }) => {
|
|
|
32
29
|
}
|
|
33
30
|
}
|
|
34
31
|
|
|
35
|
-
const
|
|
36
|
-
const
|
|
32
|
+
const getApiBaseUrl = async ({ wellKnownUri, tenantId }) => {
|
|
33
|
+
const url = new URL(wellKnownUri)
|
|
34
|
+
url.searchParams.set('tenantId', tenantId)
|
|
37
35
|
|
|
38
|
-
const
|
|
36
|
+
const { data } = await axios.get(url.href, {
|
|
37
|
+
headers: {
|
|
38
|
+
Host: url.host
|
|
39
|
+
},
|
|
39
40
|
signal: AbortSignal.timeout(3000)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const api = await axios.get(uri, config)
|
|
41
|
+
})
|
|
43
42
|
|
|
44
|
-
return
|
|
43
|
+
return data.api_endpoint
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
const getActivity = async ({ tokenType, token,
|
|
48
|
-
const
|
|
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, {
|
|
49
50
|
signal: AbortSignal.timeout(3000),
|
|
50
51
|
headers: {
|
|
51
|
-
Host: host,
|
|
52
|
+
Host: url.host,
|
|
52
53
|
Authorization: `${tokenType} ${token}`,
|
|
53
54
|
'Content-Type': contentType
|
|
54
55
|
}
|
|
55
|
-
}
|
|
56
|
-
const uri = `https://${host}${skillEndpoint}`
|
|
57
|
-
|
|
58
|
-
const skill = await axios.get(uri, config)
|
|
56
|
+
})
|
|
59
57
|
|
|
60
58
|
const activity = skill.data.skillActivity[0]
|
|
61
59
|
|
|
@@ -65,19 +63,17 @@ const getActivity = async ({ tokenType, token, host, skillEndpoint, maxQueueCoun
|
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
const getIsOpen = async ({
|
|
69
|
-
const
|
|
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
70
|
signal: AbortSignal.timeout(3000),
|
|
71
71
|
headers: {
|
|
72
|
-
Host:
|
|
72
|
+
Host: url.host,
|
|
73
73
|
Authorization: `${tokenType} ${token}`,
|
|
74
74
|
'Content-Type': contentType
|
|
75
75
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const uri = `https://${host}${hoursEndpoint}`
|
|
79
|
-
|
|
80
|
-
const hours = await axios.get(uri, config)
|
|
76
|
+
})
|
|
81
77
|
|
|
82
78
|
const days = hours.data.resultSet.hoursOfOperationProfiles[0].days
|
|
83
79
|
|
|
@@ -86,7 +82,7 @@ const getIsOpen = async ({ host, token, tokenType, hoursEndpoint }) => {
|
|
|
86
82
|
|
|
87
83
|
module.exports = {
|
|
88
84
|
authenticate,
|
|
89
|
-
|
|
85
|
+
getApiBaseUrl,
|
|
90
86
|
getIsOpen,
|
|
91
87
|
getActivity
|
|
92
88
|
}
|
package/src/server/lib/utils.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
10
19
|
}
|
|
11
20
|
|
|
12
21
|
module.exports = {
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { useSyncExternalStore } from 'react'
|
|
2
|
-
|
|
3
|
-
export default class ExternalSyncStore {
|
|
4
|
-
constructor (initialValue) {
|
|
5
|
-
this._value = initialValue
|
|
6
|
-
this._listeners = []
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
update (value) {
|
|
10
|
-
this._value = value
|
|
11
|
-
this.emitChange()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
subscribe (listener) {
|
|
15
|
-
this._listeners = [...this._listeners, listener]
|
|
16
|
-
return () => {
|
|
17
|
-
this._listeners = this._listeners.filter(l => l !== listener)
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
emitChange () {
|
|
22
|
-
for (const listener of this._listeners) {
|
|
23
|
-
listener()
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
getSnapshot () {
|
|
28
|
-
return this._value
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
static create (initialValue) {
|
|
32
|
-
const store = new ExternalSyncStore(initialValue)
|
|
33
|
-
const boundSubscribe = store.subscribe.bind(store)
|
|
34
|
-
const boundGetSnapshot = store.getSnapshot.bind(store)
|
|
35
|
-
const boundUpdate = store.update.bind(store)
|
|
36
|
-
|
|
37
|
-
return () => [
|
|
38
|
-
useSyncExternalStore(boundSubscribe, boundGetSnapshot),
|
|
39
|
-
boundUpdate
|
|
40
|
-
]
|
|
41
|
-
}
|
|
42
|
-
}
|