@redacto.io/consent-sdk-react 0.0.2 → 1.0.0

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 (30) hide show
  1. package/.turbo/turbo-build.log +8 -8
  2. package/CHANGELOG.md +11 -0
  3. package/dist/index.d.mts +58 -4
  4. package/dist/index.d.ts +58 -4
  5. package/dist/index.js +3125 -1014
  6. package/dist/index.mjs +3131 -1014
  7. package/package.json +2 -3
  8. package/src/RedactoNoticeConsent/RedactoNoticeConsent.test.tsx +504 -17
  9. package/src/RedactoNoticeConsent/RedactoNoticeConsent.tsx +1286 -269
  10. package/src/RedactoNoticeConsent/api/index.ts +267 -46
  11. package/src/RedactoNoticeConsent/api/types.ts +76 -0
  12. package/src/RedactoNoticeConsent/injectStyles.ts +102 -0
  13. package/src/RedactoNoticeConsent/styles.ts +13 -1
  14. package/src/RedactoNoticeConsent/types.ts +2 -0
  15. package/src/RedactoNoticeConsentInline/RedactoNoticeConsentInline.test.tsx +369 -0
  16. package/src/RedactoNoticeConsentInline/RedactoNoticeConsentInline.tsx +597 -0
  17. package/src/RedactoNoticeConsentInline/api/index.ts +159 -0
  18. package/src/RedactoNoticeConsentInline/api/types.ts +190 -0
  19. package/src/RedactoNoticeConsentInline/assets/redacto-logo.png +0 -0
  20. package/src/RedactoNoticeConsentInline/index.ts +1 -0
  21. package/src/RedactoNoticeConsentInline/injectStyles.ts +40 -0
  22. package/src/RedactoNoticeConsentInline/styles.ts +397 -0
  23. package/src/RedactoNoticeConsentInline/types.ts +45 -0
  24. package/src/RedactoNoticeConsentInline/useMediaQuery.ts +36 -0
  25. package/src/index.ts +1 -0
  26. package/tests/mocks.ts +98 -2
  27. package/tests/setup.ts +15 -0
  28. package/.changeset/README.md +0 -8
  29. package/.changeset/config.json +0 -11
  30. package/.changeset/fifty-candies-drop.md +0 -5
@@ -0,0 +1,159 @@
1
+ import type {
2
+ ConsentContent,
3
+ ConsentEventPayload,
4
+ FetchConsentContentParams,
5
+ SubmitConsentEventParams,
6
+ } from "./types";
7
+
8
+ const BASE_URL = "https://api.redacto.tech/consent";
9
+
10
+ export const fetchConsentContent = async ({
11
+ org_uuid,
12
+ workspace_uuid,
13
+ notice_uuid,
14
+ baseUrl,
15
+ language = "en",
16
+ specific_uuid,
17
+ }: FetchConsentContentParams): Promise<ConsentContent> => {
18
+ try {
19
+ const apiBaseUrl = baseUrl || BASE_URL;
20
+
21
+ const url = new URL(
22
+ `${apiBaseUrl}/public/organisations/${org_uuid}/workspaces/${workspace_uuid}/notices/get-notice/${notice_uuid}`
23
+ );
24
+
25
+ if (specific_uuid) {
26
+ url.searchParams.append("specific_uuid", specific_uuid);
27
+ }
28
+
29
+ const response = await fetch(url.toString(), {
30
+ method: "GET",
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ },
34
+ });
35
+
36
+ if (response.status === 401) {
37
+ const error = new Error("Unauthorized") as Error & {
38
+ status?: number;
39
+ };
40
+ error.status = 401;
41
+ throw error;
42
+ }
43
+
44
+ if (response.status === 409) {
45
+ const error = new Error("User has already provided consent") as Error & {
46
+ status?: number;
47
+ };
48
+ error.status = 409;
49
+ throw error;
50
+ }
51
+
52
+ if (!response.ok) {
53
+ throw new Error(
54
+ `Failed to fetch consent content: ${response.statusText}`
55
+ );
56
+ }
57
+
58
+ const data = await response.json();
59
+ return data;
60
+ } catch (error) {
61
+ console.error("Error fetching consent content:", error);
62
+ throw error;
63
+ }
64
+ };
65
+
66
+ export const submitConsentEvent = async ({
67
+ org_uuid,
68
+ workspace_uuid,
69
+ accessToken,
70
+ baseUrl,
71
+ noticeUuid,
72
+ purposes,
73
+ declined,
74
+ meta_data,
75
+ }: SubmitConsentEventParams): Promise<void> => {
76
+ try {
77
+ const primaryEmail = localStorage.getItem("userEmail");
78
+ const primaryMobile = localStorage.getItem("userMobile");
79
+ const payload: ConsentEventPayload = {
80
+ primary_email: primaryEmail || undefined,
81
+ primary_mobile: primaryMobile || undefined,
82
+ notice_uuid: noticeUuid,
83
+ source: "WEB",
84
+ declined,
85
+ consents: {
86
+ purposes: purposes.map((purpose) => ({
87
+ uuid: purpose.uuid,
88
+ name: purpose.name,
89
+ description: purpose.description,
90
+ industries: purpose.industries || "",
91
+ selected: purpose.selected,
92
+ data_elements: purpose.data_elements.map((element) => ({
93
+ uuid: element.uuid,
94
+ name: element.name,
95
+ enabled: element.enabled,
96
+ required: element.required,
97
+ selected: element.required ? true : element.selected,
98
+ })),
99
+ })),
100
+ },
101
+ meta_data,
102
+ };
103
+
104
+ const apiBaseUrl = baseUrl || BASE_URL;
105
+
106
+ const response = await fetch(
107
+ `${apiBaseUrl}/public/organisations/${org_uuid}/workspaces/${workspace_uuid}/by-token`,
108
+ {
109
+ method: "POST",
110
+ headers: {
111
+ Authorization: `Bearer ${accessToken}`,
112
+ "Content-Type": "application/json",
113
+ },
114
+ body: JSON.stringify(payload),
115
+ }
116
+ );
117
+
118
+ if (!response.ok) {
119
+ // Try to parse error response body
120
+ let errorData;
121
+ try {
122
+ errorData = await response.json();
123
+ } catch {
124
+ // If parsing fails, use status text
125
+ errorData = { message: response.statusText };
126
+ }
127
+
128
+ // Handle 409 CONFLICT error (user has already consented)
129
+ if (response.status === 409) {
130
+ const error = new Error(
131
+ errorData.detail?.message || errorData.message || "User has already provided consent"
132
+ ) as Error & {
133
+ status?: number;
134
+ code?: number;
135
+ detail?: { message?: string };
136
+ };
137
+ error.status = 409;
138
+ error.code = errorData.code || 409;
139
+ error.detail = errorData.detail;
140
+ throw error;
141
+ }
142
+
143
+ const error = new Error(
144
+ errorData.detail?.message || errorData.message || `Failed to submit consent event: ${response.statusText}`
145
+ ) as Error & {
146
+ status?: number;
147
+ code?: number;
148
+ detail?: { message?: string };
149
+ };
150
+ error.status = response.status;
151
+ error.code = errorData.code;
152
+ error.detail = errorData.detail;
153
+ throw error;
154
+ }
155
+ } catch (error) {
156
+ console.error("Error submitting consent event:", error);
157
+ throw error;
158
+ }
159
+ };
@@ -0,0 +1,190 @@
1
+ export type RedactoJwtPayload = {
2
+ organisation_uuid: string;
3
+ workspace_uuid: string;
4
+ };
5
+
6
+ export type ConsentContent = {
7
+ code: number;
8
+ status: string;
9
+ detail: {
10
+ uuid: string;
11
+ name: string;
12
+ organisation_uuid: string;
13
+ workspace_uuid: string;
14
+ collection_point_uuids: string[];
15
+ collection_points: {
16
+ uuid: string;
17
+ organisation_uuid: string;
18
+ workspace_uuid: string;
19
+ name: string;
20
+ created_at: string;
21
+ updated_at: string;
22
+ }[];
23
+ active_config: {
24
+ uuid: string;
25
+ notice_uuid: string;
26
+ organisation_uuid: string;
27
+ workspace_uuid: string;
28
+ version: number;
29
+ status: string;
30
+ notice_text: string;
31
+ additional_text: string;
32
+ confirm_button_text: string;
33
+ decline_button_text: string;
34
+ logo_url: string;
35
+ privacy_policy_url: string;
36
+ sub_processors_url: string;
37
+ primary_color: string;
38
+ secondary_color: string;
39
+ font_preference: string;
40
+ purposes: {
41
+ uuid: string;
42
+ name: string;
43
+ description: string;
44
+ industries?: string;
45
+ data_elements: {
46
+ uuid: string;
47
+ name: string;
48
+ description: string | null;
49
+ industries?: string | null;
50
+ enabled: boolean;
51
+ required: boolean;
52
+ }[];
53
+ }[];
54
+ default_language: string;
55
+ supported_languages_and_translations: {
56
+ [key: string]: {
57
+ notice_text: string;
58
+ additional_text: string;
59
+ confirm_button_text: string;
60
+ decline_button_text: string;
61
+ privacy_policy_prefix_text: string;
62
+ vendor_list_prefix_text: string;
63
+ privacy_policy_anchor_text: string;
64
+ vendors_list_anchor_text: string;
65
+ purpose_section_heading: string;
66
+ data_elements: {
67
+ [key: string]: string;
68
+ };
69
+ purposes: {
70
+ [key: string]: string;
71
+ };
72
+ };
73
+ };
74
+ created_at: string;
75
+ updated_at: string;
76
+ deployed_at: string;
77
+ privacy_policy_prefix_text: string;
78
+ vendor_list_prefix_text: string;
79
+ privacy_policy_anchor_text: string;
80
+ vendors_list_anchor_text: string;
81
+ purpose_section_heading: string;
82
+ };
83
+ created_at: string;
84
+ updated_at: string;
85
+ };
86
+ };
87
+
88
+ export type ConsentPreference = {
89
+ id: string;
90
+ name: string;
91
+ description: string;
92
+ required: boolean;
93
+ };
94
+
95
+ export type ConsentSelection = {
96
+ id: string;
97
+ enabled: boolean;
98
+ };
99
+
100
+ export type Settings = {
101
+ button: {
102
+ accept: {
103
+ backgroundColor: string;
104
+ textColor: string;
105
+ };
106
+ decline: {
107
+ backgroundColor: string;
108
+ textColor: string;
109
+ };
110
+ language: {
111
+ backgroundColor: string;
112
+ textColor: string;
113
+ selectedBackgroundColor?: string;
114
+ selectedTextColor?: string;
115
+ };
116
+ };
117
+ link: string;
118
+ borderRadius?: string;
119
+ backgroundColor?: string;
120
+ headingColor?: string;
121
+ textColor?: string;
122
+ borderColor?: string;
123
+ };
124
+
125
+ export type FetchConsentContentParams = {
126
+ org_uuid: string;
127
+ workspace_uuid: string;
128
+ notice_uuid: string;
129
+ baseUrl?: string;
130
+ language?: string;
131
+ specific_uuid?: string;
132
+ };
133
+
134
+ export type ConsentEventPayload = {
135
+ org_user_id?: string;
136
+ primary_email?: string;
137
+ primary_mobile?: string;
138
+ notice_uuid: string;
139
+ source: string;
140
+ declined: boolean;
141
+ consents: {
142
+ purposes: Array<{
143
+ uuid: string;
144
+ name: string;
145
+ description: string;
146
+ industries?: string;
147
+ selected: boolean;
148
+ data_elements: Array<{
149
+ uuid: string;
150
+ name: string;
151
+ enabled: boolean;
152
+ required: boolean;
153
+ selected: boolean;
154
+ }>;
155
+ }>;
156
+ };
157
+ meta_data?: {
158
+ specific_uuid: string;
159
+ };
160
+ };
161
+
162
+ export type DataElement = {
163
+ uuid: string;
164
+ name: string;
165
+ enabled: boolean;
166
+ required: boolean;
167
+ selected: boolean;
168
+ };
169
+
170
+ export type Purpose = {
171
+ uuid: string;
172
+ name: string;
173
+ description: string;
174
+ industries?: string;
175
+ selected: boolean;
176
+ data_elements: Array<DataElement>;
177
+ };
178
+
179
+ export type SubmitConsentEventParams = {
180
+ org_uuid: string;
181
+ workspace_uuid: string;
182
+ accessToken: string;
183
+ baseUrl?: string;
184
+ noticeUuid: string;
185
+ purposes: Array<Purpose>;
186
+ declined: boolean;
187
+ meta_data?: {
188
+ specific_uuid: string;
189
+ };
190
+ };
@@ -0,0 +1 @@
1
+ export { RedactoConsentInline as default } from "./RedactoNoticeConsentInline";
@@ -0,0 +1,40 @@
1
+ // CSS injection utility for the consent SDK
2
+ let spinnerAnimationInjected = false;
3
+
4
+ /**
5
+ * Injects the spinner animation keyframes needed for the loading indicator.
6
+ * This is separate from checkbox styles since this component uses native checkboxes.
7
+ */
8
+ export const injectSpinnerAnimation = () => {
9
+ if (spinnerAnimationInjected || typeof document === 'undefined') {
10
+ return;
11
+ }
12
+
13
+ const STYLE_ID = 'redacto-consent-spinner-animation';
14
+
15
+ // Check if animation is already injected
16
+ if (document.getElementById(STYLE_ID)) {
17
+ spinnerAnimationInjected = true;
18
+ return;
19
+ }
20
+
21
+ const style = document.createElement('style');
22
+ style.id = STYLE_ID;
23
+ style.textContent = `
24
+ /* Loading spinner animation */
25
+ @keyframes spin {
26
+ 0% {
27
+ transform: rotate(0deg);
28
+ }
29
+ 100% {
30
+ transform: rotate(360deg);
31
+ }
32
+ }
33
+ `;
34
+
35
+ document.head.appendChild(style);
36
+ spinnerAnimationInjected = true;
37
+ };
38
+
39
+ // Keep the old function name for backward compatibility if needed elsewhere
40
+ export const injectCheckboxStyles = injectSpinnerAnimation;