@ht-sdks/consent-manager 0.0.1

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 (110) hide show
  1. package/LICENSE +45 -0
  2. package/README.md +851 -0
  3. package/commonjs/__tests__/consent-manager-builder/analytics.test.js +250 -0
  4. package/commonjs/__tests__/consent-manager-builder/fetch-destinations.test.js +155 -0
  5. package/commonjs/__tests__/consent-manager-builder/index.todo.js +327 -0
  6. package/commonjs/__tests__/consent-manager-builder/preferences.test.js +129 -0
  7. package/commonjs/__tests__/index.test.js +57 -0
  8. package/commonjs/consent-manager/banner.js +89 -0
  9. package/commonjs/consent-manager/buttons.js +18 -0
  10. package/commonjs/consent-manager/cancel-dialog.js +76 -0
  11. package/commonjs/consent-manager/categories.js +29 -0
  12. package/commonjs/consent-manager/container.js +169 -0
  13. package/commonjs/consent-manager/dialog.js +129 -0
  14. package/commonjs/consent-manager/font-styles.js +10 -0
  15. package/commonjs/consent-manager/index.js +244 -0
  16. package/commonjs/consent-manager/preference-dialog.js +178 -0
  17. package/commonjs/consent-manager-builder/analytics.js +76 -0
  18. package/commonjs/consent-manager-builder/fetch-destinations.js +104 -0
  19. package/commonjs/consent-manager-builder/index.js +330 -0
  20. package/commonjs/consent-manager-builder/preferences.js +82 -0
  21. package/commonjs/index.js +39 -0
  22. package/commonjs/standalone.js +86 -0
  23. package/commonjs/types.js +3 -0
  24. package/esm/__tests__/consent-manager-builder/analytics.test.js +245 -0
  25. package/esm/__tests__/consent-manager-builder/fetch-destinations.test.js +150 -0
  26. package/esm/__tests__/consent-manager-builder/index.todo.js +322 -0
  27. package/esm/__tests__/consent-manager-builder/preferences.test.js +124 -0
  28. package/esm/__tests__/index.test.js +55 -0
  29. package/esm/consent-manager/banner.js +65 -0
  30. package/esm/consent-manager/buttons.js +12 -0
  31. package/esm/consent-manager/cancel-dialog.js +52 -0
  32. package/esm/consent-manager/categories.js +26 -0
  33. package/esm/consent-manager/container.js +162 -0
  34. package/esm/consent-manager/dialog.js +105 -0
  35. package/esm/consent-manager/font-styles.js +8 -0
  36. package/esm/consent-manager/index.js +220 -0
  37. package/esm/consent-manager/preference-dialog.js +154 -0
  38. package/esm/consent-manager-builder/analytics.js +73 -0
  39. package/esm/consent-manager-builder/fetch-destinations.js +98 -0
  40. package/esm/consent-manager-builder/index.js +325 -0
  41. package/esm/consent-manager-builder/preferences.js +73 -0
  42. package/esm/index.js +28 -0
  43. package/esm/standalone.js +58 -0
  44. package/esm/types.js +1 -0
  45. package/package.json +168 -0
  46. package/src/__tests__/consent-manager-builder/analytics.test.ts +279 -0
  47. package/src/__tests__/consent-manager-builder/fetch-destinations.test.ts +89 -0
  48. package/src/__tests__/consent-manager-builder/index.todo.js +386 -0
  49. package/src/__tests__/consent-manager-builder/preferences.test.ts +156 -0
  50. package/src/__tests__/index.test.ts +68 -0
  51. package/src/consent-manager/banner.tsx +227 -0
  52. package/src/consent-manager/buttons.tsx +78 -0
  53. package/src/consent-manager/cancel-dialog.tsx +68 -0
  54. package/src/consent-manager/categories.ts +27 -0
  55. package/src/consent-manager/container.tsx +282 -0
  56. package/src/consent-manager/dialog.tsx +236 -0
  57. package/src/consent-manager/font-styles.tsx +14 -0
  58. package/src/consent-manager/index.tsx +327 -0
  59. package/src/consent-manager/preference-dialog.tsx +371 -0
  60. package/src/consent-manager-builder/analytics.ts +104 -0
  61. package/src/consent-manager-builder/fetch-destinations.ts +50 -0
  62. package/src/consent-manager-builder/index.tsx +426 -0
  63. package/src/consent-manager-builder/preferences.ts +100 -0
  64. package/src/index.ts +42 -0
  65. package/src/standalone.tsx +57 -0
  66. package/src/styled.d.ts +7 -0
  67. package/src/types.ts +151 -0
  68. package/standalone/consent-manager.js +23 -0
  69. package/standalone/consent-manager.js.map +1 -0
  70. package/types/__tests__/consent-manager-builder/analytics.test.d.ts +1 -0
  71. package/types/__tests__/consent-manager-builder/fetch-destinations.test.d.ts +1 -0
  72. package/types/__tests__/consent-manager-builder/index.todo.d.ts +1 -0
  73. package/types/__tests__/consent-manager-builder/preferences.test.d.ts +1 -0
  74. package/types/__tests__/index.test.d.ts +1 -0
  75. package/types/consent-manager/banner.d.ts +21 -0
  76. package/types/consent-manager/buttons.d.ts +13 -0
  77. package/types/consent-manager/cancel-dialog.d.ts +19 -0
  78. package/types/consent-manager/categories.d.ts +3 -0
  79. package/types/consent-manager/container.d.ts +35 -0
  80. package/types/consent-manager/dialog.d.ts +29 -0
  81. package/types/consent-manager/font-styles.d.ts +2 -0
  82. package/types/consent-manager/index.d.ts +35 -0
  83. package/types/consent-manager/preference-dialog.d.ts +32 -0
  84. package/types/consent-manager-builder/analytics.d.ts +14 -0
  85. package/types/consent-manager-builder/fetch-destinations.d.ts +2 -0
  86. package/types/consent-manager-builder/index.d.ts +126 -0
  87. package/types/consent-manager-builder/preferences.d.ts +26 -0
  88. package/types/index.d.ts +7 -0
  89. package/types/src/__tests__/consent-manager-builder/analytics.test.d.ts +1 -0
  90. package/types/src/__tests__/consent-manager-builder/fetch-destinations.test.d.ts +1 -0
  91. package/types/src/__tests__/consent-manager-builder/preferences.test.d.ts +1 -0
  92. package/types/src/__tests__/index.test.d.ts +1 -0
  93. package/types/src/consent-manager/banner.d.ts +21 -0
  94. package/types/src/consent-manager/buttons.d.ts +13 -0
  95. package/types/src/consent-manager/cancel-dialog.d.ts +19 -0
  96. package/types/src/consent-manager/categories.d.ts +3 -0
  97. package/types/src/consent-manager/container.d.ts +35 -0
  98. package/types/src/consent-manager/dialog.d.ts +29 -0
  99. package/types/src/consent-manager/font-styles.d.ts +2 -0
  100. package/types/src/consent-manager/index.d.ts +35 -0
  101. package/types/src/consent-manager/preference-dialog.d.ts +32 -0
  102. package/types/src/consent-manager-builder/analytics.d.ts +14 -0
  103. package/types/src/consent-manager-builder/fetch-destinations.d.ts +2 -0
  104. package/types/src/consent-manager-builder/index.d.ts +126 -0
  105. package/types/src/consent-manager-builder/preferences.d.ts +26 -0
  106. package/types/src/index.d.ts +7 -0
  107. package/types/src/standalone.d.ts +5 -0
  108. package/types/src/types.d.ts +131 -0
  109. package/types/standalone.d.ts +5 -0
  110. package/types/types.d.ts +131 -0
@@ -0,0 +1,227 @@
1
+ import React, { Fragment, PureComponent } from 'react'
2
+ import styled from '@emotion/styled'
3
+ import fontStyles from './font-styles'
4
+ import { ActionsBlockProps } from '../types'
5
+ import { DefaultButton, GreenButton } from './buttons'
6
+
7
+ interface RootProps {
8
+ readonly backgroundColor: string
9
+ readonly textColor: string
10
+ readonly hideCloseButton: boolean
11
+ }
12
+
13
+ interface ContentProps {
14
+ asModal?: boolean
15
+ }
16
+
17
+ const Overlay = styled('div')`
18
+ position: fixed;
19
+ top: 0;
20
+ bottom: 0;
21
+ left: 0;
22
+ right: 0;
23
+ background: #fff;
24
+ opacity: 0.8;
25
+ `
26
+
27
+ const Centered = styled('div')`
28
+ position: fixed;
29
+ top: 50%;
30
+ left: 50%;
31
+ transform: translate(-50%, -50%);
32
+ max-width: 500px;
33
+ @media (max-width: 767px) {
34
+ width: 80vw;
35
+ }
36
+ `
37
+
38
+ const RootCentered = styled('div')<RootProps>`
39
+ ${fontStyles};
40
+ position: relative;
41
+ max-width: 500px;
42
+ padding: 18px;
43
+ padding-right: ${props => (props.hideCloseButton ? '18px' : '40px')};
44
+ background: ${props => props.backgroundColor};
45
+ color: ${props => props.textColor};
46
+ text-align: center;
47
+ font-size: 14px;
48
+ line-height: 1.3;
49
+ `
50
+
51
+ const Root = styled('div')<RootProps>`
52
+ ${fontStyles};
53
+ position: relative;
54
+ padding: 8px;
55
+ padding-right: ${props => (props.hideCloseButton ? '8px' : '40px')};
56
+ background: ${props => props.backgroundColor};
57
+ color: ${props => props.textColor};
58
+ text-align: center;
59
+ font-size: 12px;
60
+ line-height: 1.3;
61
+ @media (min-width: 768px) {
62
+ display: flex;
63
+ align-items: center;
64
+ }
65
+ `
66
+
67
+ const Content = styled('div')<ContentProps>`
68
+ margin-bottom: ${props => (props.asModal ? '20px' : '8px')};
69
+ @media (min-width: 768px) {
70
+ flex: auto;
71
+ margin-bottom: ${props => (props.asModal ? '20px' : '0')};
72
+ }
73
+ a,
74
+ button {
75
+ display: inline;
76
+ padding: 0;
77
+ border: none;
78
+ background: none;
79
+ color: inherit;
80
+ font: inherit;
81
+ text-decoration: underline;
82
+ cursor: pointer;
83
+ }
84
+ `
85
+
86
+ const ActionsBlock = styled('div')`
87
+ color: #000;
88
+ button {
89
+ margin: 4px 0;
90
+ width: 100%;
91
+ @media (min-width: 768px) {
92
+ margin: 4px 8px;
93
+ width: 200px;
94
+ }
95
+ }
96
+ `
97
+
98
+ const P = styled('p')`
99
+ margin: 0;
100
+ &:not(:last-child) {
101
+ margin-bottom: 6px;
102
+ }
103
+ `
104
+
105
+ interface CloseButtonProps {
106
+ isTop?: boolean
107
+ }
108
+
109
+ const CloseButton = styled('button')<CloseButtonProps>`
110
+ position: absolute;
111
+ right: 8px;
112
+ top: ${props => (props.isTop ? '20px' : '50%')};
113
+ transform: translateY(-50%);
114
+ padding: 8px;
115
+ border: none;
116
+ background: none;
117
+ color: inherit;
118
+ font: inherit;
119
+ font-size: 14px;
120
+ line-height: 1;
121
+ cursor: pointer;
122
+ `
123
+
124
+ interface BannerProps {
125
+ innerRef: (node: HTMLElement | null) => void
126
+ onClose: () => void
127
+ onChangePreferences: () => void
128
+ content: React.ReactNode
129
+ subContent: string | undefined
130
+ actionsBlock?: ((props: ActionsBlockProps) => React.ReactElement) | true
131
+ backgroundColor: string
132
+ textColor: string
133
+ onAcceptAll: () => void
134
+ onDenyAll: () => void
135
+ hideCloseButton: boolean
136
+ asModal?: boolean
137
+ }
138
+
139
+ export default class Banner extends PureComponent<BannerProps> {
140
+ static displayName = 'Banner'
141
+
142
+ render() {
143
+ const {
144
+ innerRef,
145
+ onClose,
146
+ onChangePreferences,
147
+ content,
148
+ subContent,
149
+ actionsBlock,
150
+ backgroundColor,
151
+ textColor,
152
+ onAcceptAll,
153
+ onDenyAll,
154
+ hideCloseButton,
155
+ asModal
156
+ } = this.props
157
+
158
+ const RootContent = (
159
+ <Fragment>
160
+ <Content asModal={asModal} id="hightouchio_fragmentBanner">
161
+ <P id="hightouchio_pContent">{content}</P>
162
+ <P id="hightouchio_pSubContent">
163
+ <button type="button" id="hightouchio_subContentBtn" onClick={onChangePreferences}>
164
+ {subContent}
165
+ </button>
166
+ </P>
167
+ </Content>
168
+ {typeof actionsBlock === 'function' &&
169
+ actionsBlock({
170
+ acceptAll: onAcceptAll,
171
+ denyAll: onDenyAll,
172
+ changePreferences: onChangePreferences
173
+ })}
174
+ {actionsBlock === true && (
175
+ <ActionsBlock id="hightouchio_actionBlock">
176
+ <GreenButton id="hightouchio_allowBtn" type="button" onClick={onAcceptAll}>
177
+ Allow all
178
+ </GreenButton>
179
+ <DefaultButton id="hightouchio_denyBtn" type="button" onClick={onDenyAll}>
180
+ Deny all
181
+ </DefaultButton>
182
+ </ActionsBlock>
183
+ )}
184
+ {!hideCloseButton && (
185
+ <CloseButton
186
+ id="hightouchio_closeButton"
187
+ type="button"
188
+ title="Close"
189
+ aria-label="Close"
190
+ onClick={onClose}
191
+ isTop={asModal}
192
+ >
193
+ ✕
194
+ </CloseButton>
195
+ )}
196
+ </Fragment>
197
+ )
198
+
199
+ if (asModal) {
200
+ return (
201
+ <Fragment>
202
+ <Overlay />
203
+ <Centered>
204
+ <RootCentered
205
+ ref={innerRef}
206
+ backgroundColor={backgroundColor}
207
+ textColor={textColor}
208
+ hideCloseButton={hideCloseButton}
209
+ >
210
+ {RootContent}
211
+ </RootCentered>
212
+ </Centered>
213
+ </Fragment>
214
+ )
215
+ }
216
+ return (
217
+ <Root
218
+ ref={innerRef}
219
+ backgroundColor={backgroundColor}
220
+ textColor={textColor}
221
+ hideCloseButton={hideCloseButton}
222
+ >
223
+ {RootContent}
224
+ </Root>
225
+ )
226
+ }
227
+ }
@@ -0,0 +1,78 @@
1
+ import styled from '@emotion/styled'
2
+ import { css } from '@emotion/react'
3
+
4
+ const baseStyles = css`
5
+ height: 32px;
6
+ padding: 0 16px;
7
+ border: none;
8
+ border-radius: 4px;
9
+ color: inherit;
10
+ font: inherit;
11
+ font-size: 12px;
12
+ line-height: 1;
13
+ cursor: pointer;
14
+ outline: none;
15
+ transition: box-shadow 80ms ease-in-out;
16
+ `
17
+
18
+ export const DefaultButton = styled('button')`
19
+ ${baseStyles};
20
+ margin-right: 8px;
21
+ background-color: #fff;
22
+ background-image: linear-gradient(to top, rgba(67, 90, 111, 0.041), rgba(255, 255, 255, 0.041));
23
+ box-shadow: inset 0 0 0 1px rgba(67, 90, 111, 0.146), inset 0 -1px 1px 0 rgba(67, 90, 111, 0.079);
24
+ &:hover {
25
+ background-image: linear-gradient(to top, rgba(67, 90, 111, 0.057), rgba(67, 90, 111, 0.025));
26
+ box-shadow: inset 0 0 0 1px rgba(67, 90, 111, 0.255),
27
+ inset 0 -1px 1px 0 rgba(67, 90, 111, 0.114);
28
+ }
29
+ &:focus {
30
+ box-shadow: 0 0 0 3px rgba(1, 108, 209, 0.146), inset 0 0 0 1px rgba(67, 90, 111, 0.38),
31
+ inset 0 -1px 1px 0 rgba(67, 90, 111, 0.079);
32
+ }
33
+ &:active {
34
+ background: rgba(1, 108, 209, 0.079);
35
+ box-shadow: inset 0 0 0 1px rgba(67, 90, 111, 0.146),
36
+ inset 0 -1px 1px 0 rgba(67, 90, 111, 0.079);
37
+ }
38
+ `
39
+
40
+ export const GreenButton = styled('button')`
41
+ ${baseStyles};
42
+ background-color: #47b881;
43
+ background-image: linear-gradient(to top, #3faf77, #47b881);
44
+ box-shadow: inset 0 0 0 1px rgba(67, 90, 111, 0.204), inset 0 -1px 1px 0 rgba(67, 90, 111, 0.204);
45
+ color: #fff;
46
+ &:hover {
47
+ background-image: linear-gradient(to top, #37a56d, #3faf77);
48
+ }
49
+ &:focus {
50
+ box-shadow: 0 0 0 3px rgba(71, 184, 129, 0.477), inset 0 0 0 1px rgba(71, 184, 129, 0.204),
51
+ inset 0 -1px 1px 0 rgba(71, 184, 129, 0.204);
52
+ }
53
+ &:active {
54
+ background-image: linear-gradient(to top, #2d9760, #248953);
55
+ box-shadow: inset 0 0 0 1px rgba(71, 184, 129, 0.204),
56
+ inset 0 -1px 1px 0 rgba(71, 184, 129, 0.204);
57
+ }
58
+ `
59
+
60
+ export const RedButton = styled('button')`
61
+ ${baseStyles};
62
+ background-color: #f36331;
63
+ background-image: linear-gradient(to top, #f4541d, #f36331);
64
+ box-shadow: inset 0 0 0 1px rgba(67, 90, 111, 0.204), inset 0 -1px 1px 0 rgba(67, 90, 111, 0.204);
65
+ color: #fff;
66
+ &:hover {
67
+ background-image: linear-gradient(to top, #f4450a, #f4541d);
68
+ }
69
+ &:focus {
70
+ box-shadow: 0 0 0 3px rgba(243, 99, 49, 0.477), inset 0 0 0 1px rgba(243, 99, 49, 0.204),
71
+ inset 0 -1px 1px 0 rgba(243, 99, 49, 0.204);
72
+ }
73
+ &:active {
74
+ background-image: linear-gradient(to top, #dd3c06, #c63403);
75
+ box-shadow: inset 0 0 0 1px rgba(67, 90, 111, 0.204),
76
+ inset 0 -1px 1px 0 rgba(67, 90, 111, 0.204);
77
+ }
78
+ `
@@ -0,0 +1,68 @@
1
+ import React, { PureComponent } from 'react'
2
+ import Dialog from './dialog'
3
+ import { DefaultButton, RedButton } from './buttons'
4
+ import { PreferenceDialogTemplate } from '../types'
5
+
6
+ interface Props {
7
+ innerRef: (node: HTMLElement) => void
8
+ onBack: () => void
9
+ onConfirm: () => void
10
+ title: React.ReactNode
11
+ content: React.ReactNode
12
+ preferencesDialogTemplate?: PreferenceDialogTemplate
13
+ }
14
+
15
+ export default class CancelDialog extends PureComponent<Props> {
16
+ static displayName = 'CancelDialog'
17
+
18
+ render() {
19
+ const { innerRef, onBack, title, content, preferencesDialogTemplate } = this.props
20
+
21
+ const buttons = (
22
+ <div>
23
+ <DefaultButton id="hightouchio_backDialogBtn" type="button" onClick={onBack}>
24
+ {preferencesDialogTemplate?.cancelDialogButtons!.backValue}
25
+ </DefaultButton>
26
+ <RedButton id="hightouchio_cancelDialogBtn" type="submit">
27
+ {preferencesDialogTemplate?.cancelDialogButtons!.cancelValue}
28
+ </RedButton>
29
+ </div>
30
+ )
31
+
32
+ return (
33
+ <Dialog
34
+ innerRef={innerRef}
35
+ title={title}
36
+ buttons={buttons}
37
+ onSubmit={this.handleSubmit}
38
+ width="500px"
39
+ >
40
+ {content}
41
+ </Dialog>
42
+ )
43
+ }
44
+
45
+ componentDidMount() {
46
+ document.body.addEventListener('keydown', this.handleEsc, false)
47
+ }
48
+
49
+ componentWillUnmount() {
50
+ document.body.removeEventListener('keydown', this.handleEsc, false)
51
+ }
52
+
53
+ handleSubmit = e => {
54
+ const { onConfirm } = this.props
55
+
56
+ e.preventDefault()
57
+ onConfirm()
58
+ }
59
+
60
+ handleEsc = (e: KeyboardEvent) => {
61
+ const { onConfirm } = this.props
62
+
63
+ // Esc key
64
+ if (e.keyCode === 27) {
65
+ onConfirm()
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,27 @@
1
+ export const MARKETING_AND_ANALYTICS_CATEGORIES = [
2
+ 'A/B Testing',
3
+ 'Analytics',
4
+ 'Attribution',
5
+ 'Email',
6
+ 'Enrichment',
7
+ 'Heatmaps & Recordings',
8
+ 'Raw Data',
9
+ 'Realtime Dashboards',
10
+ 'Referrals',
11
+ 'Surveys',
12
+ 'Video'
13
+ ]
14
+
15
+ export const ADVERTISING_CATEGORIES = ['Advertising', 'Tag Managers']
16
+
17
+ export const FUNCTIONAL_CATEGORIES = [
18
+ 'CRM',
19
+ 'Customer Success',
20
+ 'Deep Linking',
21
+ 'Helpdesk',
22
+ 'Livechat',
23
+ 'Performance Monitoring',
24
+ 'Personalization',
25
+ 'SMS & Push Notifications',
26
+ 'Security & Fraud'
27
+ ]
@@ -0,0 +1,282 @@
1
+ import { EventEmitter } from 'events'
2
+ import React from 'react'
3
+ import Banner from './banner'
4
+ import PreferenceDialog from './preference-dialog'
5
+ import CancelDialog from './cancel-dialog'
6
+ import { ADVERTISING_CATEGORIES, FUNCTIONAL_CATEGORIES } from './categories'
7
+ import {
8
+ Destination,
9
+ CategoryPreferences,
10
+ CustomCategories,
11
+ DefaultDestinationBehavior,
12
+ ActionsBlockProps,
13
+ PreferenceDialogTemplate,
14
+ CloseBehavior
15
+ } from '../types'
16
+
17
+ const emitter = new EventEmitter()
18
+ export function openDialog() {
19
+ emitter.emit('openDialog')
20
+ }
21
+
22
+ export interface CloseBehaviorFunction {
23
+ (categories: CategoryPreferences): CategoryPreferences
24
+ }
25
+
26
+ interface ContainerProps {
27
+ setPreferences: (prefs: CategoryPreferences) => void
28
+ saveConsent: (
29
+ newPreferences?: CategoryPreferences,
30
+ shouldReload?: boolean,
31
+ devMode?: boolean
32
+ ) => void
33
+ resetPreferences: () => void
34
+ closeBehavior?: CloseBehavior | CloseBehaviorFunction
35
+ destinations: Destination[]
36
+ customCategories?: CustomCategories | undefined
37
+ newDestinations: Destination[]
38
+ preferences: CategoryPreferences
39
+ havePreferencesChanged: boolean
40
+ isConsentRequired: boolean
41
+ implyConsentOnInteraction: boolean
42
+ bannerContent: React.ReactNode
43
+ bannerSubContent: string | undefined
44
+ bannerActionsBlock?: ((props: ActionsBlockProps) => React.ReactElement) | true
45
+ bannerTextColor: string
46
+ bannerBackgroundColor: string
47
+ bannerHideCloseButton: boolean
48
+ bannerAsModal?: boolean
49
+ preferencesDialogTitle: React.ReactNode
50
+ preferencesDialogContent: React.ReactNode
51
+ cancelDialogTitle: React.ReactNode
52
+ cancelDialogContent: React.ReactNode
53
+ workspaceAddedNewDestinations?: boolean
54
+ defaultDestinationBehavior?: DefaultDestinationBehavior
55
+ preferencesDialogTemplate?: PreferenceDialogTemplate
56
+ }
57
+
58
+ function normalizeDestinations(destinations: Destination[]) {
59
+ const marketingDestinations: Destination[] = []
60
+ const advertisingDestinations: Destination[] = []
61
+ const functionalDestinations: Destination[] = []
62
+
63
+ for (const destination of destinations) {
64
+ if (ADVERTISING_CATEGORIES.find(c => c === destination.category)) {
65
+ advertisingDestinations.push(destination)
66
+ } else if (FUNCTIONAL_CATEGORIES.find(c => c === destination.category)) {
67
+ functionalDestinations.push(destination)
68
+ } else {
69
+ // Fallback to marketing
70
+ marketingDestinations.push(destination)
71
+ }
72
+ }
73
+
74
+ return { marketingDestinations, advertisingDestinations, functionalDestinations }
75
+ }
76
+
77
+ const Container: React.FC<ContainerProps> = props => {
78
+ const [isDialogOpen, toggleDialog] = React.useState(
79
+ false || (props.workspaceAddedNewDestinations && props.defaultDestinationBehavior === 'ask')
80
+ )
81
+ const [showBanner, toggleBanner] = React.useState(true)
82
+ const [isCancelling, toggleCancel] = React.useState(false)
83
+
84
+ // check if all preferences are empty, in which case we should show the banner
85
+ const emptyPreferences = Object.values(props.preferences || {}).every(v => v == null)
86
+
87
+ let banner = React.useRef<HTMLElement>(null)
88
+ let preferenceDialog = React.useRef<HTMLElement>(null)
89
+ let cancelDialog = React.useRef<HTMLElement>(null)
90
+
91
+ const {
92
+ marketingDestinations,
93
+ advertisingDestinations,
94
+ functionalDestinations
95
+ } = normalizeDestinations(props.destinations)
96
+
97
+ const onAcceptAll = () => {
98
+ const truePreferences: CategoryPreferences = props.preferences
99
+ for (const preferenceName of Object.keys(props.preferences)) {
100
+ const value = props.preferences[preferenceName]
101
+ if (typeof value === 'string') {
102
+ truePreferences[preferenceName] = value
103
+ } else {
104
+ truePreferences[preferenceName] = true
105
+ }
106
+ }
107
+
108
+ props.setPreferences(truePreferences)
109
+ return props.saveConsent()
110
+ }
111
+
112
+ const onDenyAll = () => {
113
+ const falsePreferences: CategoryPreferences = props.preferences
114
+ for (const preferenceName of Object.keys(props.preferences)) {
115
+ const value = props.preferences[preferenceName]
116
+ if (typeof value === 'string') {
117
+ falsePreferences[preferenceName] = value
118
+ } else {
119
+ falsePreferences[preferenceName] = false
120
+ }
121
+ }
122
+
123
+ props.setPreferences(falsePreferences)
124
+ return props.saveConsent()
125
+ }
126
+
127
+ const onClose = () => {
128
+ if (props.closeBehavior === undefined || props.closeBehavior === 'dismiss') {
129
+ return toggleBanner(false)
130
+ }
131
+
132
+ if (props.closeBehavior === 'accept') {
133
+ toggleBanner(false)
134
+ return onAcceptAll()
135
+ }
136
+
137
+ if (props.closeBehavior === 'deny') {
138
+ toggleBanner(false)
139
+ return onDenyAll()
140
+ }
141
+
142
+ // closeBehavior is a custom function
143
+ const customClosePreferences = props.closeBehavior(props.preferences)
144
+ props.setPreferences(customClosePreferences)
145
+ props.saveConsent()
146
+ return toggleBanner(false)
147
+ }
148
+
149
+ const showDialog = () => toggleDialog(true)
150
+
151
+ const handleBodyClick = e => {
152
+ // Do nothing if no new implicit consent needs to be saved
153
+ if (
154
+ !props.isConsentRequired ||
155
+ !props.implyConsentOnInteraction ||
156
+ props.newDestinations.length === 0
157
+ ) {
158
+ return
159
+ }
160
+
161
+ // Ignore propogated clicks from inside the consent manager
162
+ if (
163
+ (banner.current && banner.current.contains(e.target)) ||
164
+ (preferenceDialog.current && preferenceDialog.current.contains(e.target)) ||
165
+ (cancelDialog.current && cancelDialog.current.contains(e.target)) ||
166
+ 'subContentBtn' === e.target.id
167
+ ) {
168
+ return
169
+ }
170
+
171
+ // Accept all consent on page interaction.
172
+ if (!isDialogOpen && props.implyConsentOnInteraction) {
173
+ onAcceptAll()
174
+ }
175
+ }
176
+
177
+ React.useEffect(() => {
178
+ emitter.on('openDialog', showDialog)
179
+ if (props.isConsentRequired && props.implyConsentOnInteraction) {
180
+ document.body.addEventListener('click', handleBodyClick, false)
181
+ }
182
+
183
+ return () => {
184
+ emitter.removeListener('openDialog', showDialog)
185
+ document.body.removeEventListener('click', handleBodyClick, false)
186
+ }
187
+ })
188
+
189
+ React.useEffect(() => {
190
+ if (isDialogOpen) {
191
+ props.resetPreferences()
192
+ }
193
+ }, [isDialogOpen])
194
+
195
+ const handleCategoryChange = (category: string, value: boolean) => {
196
+ props.setPreferences({
197
+ [category]: value
198
+ })
199
+ }
200
+
201
+ const handleSave = () => {
202
+ toggleDialog(false)
203
+ props.saveConsent(undefined, false)
204
+ }
205
+
206
+ const handleCancel = () => {
207
+ // Only show the cancel confirmation if there's unconsented destinations
208
+ if (props.newDestinations.length > 0) {
209
+ toggleCancel(true)
210
+ } else {
211
+ toggleDialog(false)
212
+ props.resetPreferences()
213
+ }
214
+ }
215
+
216
+ const handleCancelBack = () => {
217
+ toggleCancel(false)
218
+ }
219
+
220
+ const handleCancelConfirm = () => {
221
+ toggleCancel(false)
222
+ toggleDialog(false)
223
+ props.resetPreferences()
224
+ }
225
+
226
+ return (
227
+ <>
228
+ {showBanner &&
229
+ props.isConsentRequired &&
230
+ (props.newDestinations.length > 0 || emptyPreferences) && (
231
+ <Banner
232
+ innerRef={current => (banner = { current })}
233
+ onClose={onClose}
234
+ onChangePreferences={() => toggleDialog(true)}
235
+ content={props.bannerContent}
236
+ subContent={props.bannerSubContent}
237
+ actionsBlock={props.bannerActionsBlock}
238
+ textColor={props.bannerTextColor}
239
+ backgroundColor={props.bannerBackgroundColor}
240
+ onAcceptAll={onAcceptAll}
241
+ onDenyAll={onDenyAll}
242
+ hideCloseButton={props.bannerHideCloseButton}
243
+ asModal={props.bannerAsModal}
244
+ />
245
+ )}
246
+
247
+ {isDialogOpen && (
248
+ <PreferenceDialog
249
+ customCategories={props.customCategories}
250
+ destinations={props.destinations}
251
+ preferences={props.preferences}
252
+ innerRef={current => (preferenceDialog = { current })}
253
+ onCancel={handleCancel}
254
+ onSave={handleSave}
255
+ onChange={handleCategoryChange}
256
+ marketingDestinations={marketingDestinations}
257
+ advertisingDestinations={advertisingDestinations}
258
+ functionalDestinations={functionalDestinations}
259
+ marketingAndAnalytics={props.preferences.marketingAndAnalytics}
260
+ advertising={props.preferences.advertising}
261
+ functional={props.preferences.functional}
262
+ title={props.preferencesDialogTitle}
263
+ content={props.preferencesDialogContent}
264
+ preferencesDialogTemplate={props.preferencesDialogTemplate}
265
+ />
266
+ )}
267
+
268
+ {isCancelling && (
269
+ <CancelDialog
270
+ innerRef={current => (cancelDialog = { current })}
271
+ onBack={handleCancelBack}
272
+ onConfirm={handleCancelConfirm}
273
+ title={props.cancelDialogTitle}
274
+ content={props.cancelDialogContent}
275
+ preferencesDialogTemplate={props.preferencesDialogTemplate}
276
+ />
277
+ )}
278
+ </>
279
+ )
280
+ }
281
+
282
+ export default Container