@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.
Files changed (39) hide show
  1. package/README.md +83 -1
  2. package/dist/client.js +1607 -212
  3. package/dist/client.js.map +1 -1
  4. package/dist/server.js +181 -240
  5. package/dist/server.js.map +1 -1
  6. package/main.scss +4 -3
  7. package/package.json +6 -2
  8. package/src/client/components/availability/availability.jsx +44 -46
  9. package/src/client/components/availability/availability.scss +105 -5
  10. package/src/client/components/chat/chat.jsx +169 -0
  11. package/src/client/components/chat/chat.scss +143 -0
  12. package/src/client/components/errorSummary/error-summary.jsx +34 -0
  13. package/src/client/components/message/message.jsx +20 -0
  14. package/src/client/components/panel/panel-footer.jsx +9 -0
  15. package/src/client/components/panel/panel-header.jsx +41 -0
  16. package/src/client/components/panel/panel.jsx +119 -0
  17. package/src/client/components/panel/panel.scss +72 -0
  18. package/src/client/components/screens/end-chat.jsx +17 -0
  19. package/src/client/components/screens/pre-chat.jsx +65 -0
  20. package/src/client/components/screens/request-chat.jsx +156 -0
  21. package/src/client/components/screens/settings.jsx +67 -0
  22. package/src/client/components/screens/unavailable.jsx +23 -0
  23. package/src/client/hooks/useFocusedElements.js +71 -0
  24. package/src/client/hooks/useTextareaAutosize.js +13 -0
  25. package/src/client/index.jsx +15 -1
  26. package/src/client/lib/check-availability.js +1 -0
  27. package/src/client/lib/message-notification.js +36 -0
  28. package/src/client/lib/transform-messages.js +35 -0
  29. package/src/client/store/AppProvider.jsx +139 -0
  30. package/src/client/store/actions-map.js +119 -0
  31. package/src/client/store/constants.js +3 -0
  32. package/src/client/store/reducer.js +28 -0
  33. package/src/client/store/useApp.js +5 -0
  34. package/src/client/store/useChatSdk.js +37 -0
  35. package/src/server/index.js +15 -6
  36. package/src/server/lib/client.js +31 -35
  37. package/src/server/lib/utils.js +18 -9
  38. package/src/client/lib/external-stores.js +0 -4
  39. package/src/client/lib/external-sync-store.js +0 -42
@@ -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 uri = 'https://cxone.niceincontact.com/auth/token'
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 config = {
16
+ const auth = await axios.post(url.href, body, {
12
17
  signal: AbortSignal.timeout(3000),
13
18
  headers: {
14
- Host: 'eu1.niceincontact.com',
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 getHost = async ({ tenantId }) => {
36
- const uri = `https://cxone.niceincontact.com/.well-known/cxone-configuration?tenantId=${tenantId}`
32
+ const getApiBaseUrl = async ({ wellKnownUri, tenantId }) => {
33
+ const url = new URL(wellKnownUri)
34
+ url.searchParams.set('tenantId', tenantId)
37
35
 
38
- const config = {
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 `api-${api.data.area}.niceincontact.com`
43
+ return data.api_endpoint
45
44
  }
46
45
 
47
- const getActivity = async ({ tokenType, token, host, skillEndpoint, maxQueueCount }) => {
48
- const config = {
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 ({ host, token, tokenType, hoursEndpoint }) => {
69
- const config = {
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: 'api-l36.niceincontact.com',
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
- getHost,
85
+ getApiBaseUrl,
90
86
  getIsOpen,
91
87
  getActivity
92
88
  }
@@ -1,12 +1,21 @@
1
- const isWithinHours = days => {
2
- const now = new Date()
3
- const name = now.toLocaleDateString('en-GB', { weekday: 'long' })
4
- const day = days.find(d => d.day.toLowerCase() === name.toLowerCase())
5
- const date = now.toLocaleDateString('en-GB').split('/')
6
- const open = `${date[2]}-${date[1]}-${date[0]}T${day.openTime}.000Z`
7
- const close = `${date[2]}-${date[1]}-${date[0]}T${day.closeTime}.000Z`
8
-
9
- return now.getTime() >= Date.parse(open) && now.getTime() <= Date.parse(close)
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,4 +0,0 @@
1
- import ExternalSyncStore from './external-sync-store'
2
-
3
- export const useWebchatOpenState = ExternalSyncStore.create(false)
4
- export const useMessageThread = ExternalSyncStore.create([])
@@ -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
- }