@terreno/ui 57.21.0 → 58.0.0-beta.2
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/.ai/guidelines/core.md +1 -1
- package/CHANGELOG.md +89 -0
- package/dist/ActionSheet.d.ts +9 -9
- package/dist/ActionSheet.d.ts.map +1 -1
- package/dist/ActionSheet.js +11 -4
- package/dist/ActionSheet.js.map +1 -1
- package/dist/AnnouncementNavigator.d.ts +7 -1
- package/dist/AnnouncementNavigator.d.ts.map +1 -1
- package/dist/AnnouncementNavigator.js +15 -16
- package/dist/AnnouncementNavigator.js.map +1 -1
- package/dist/Avatar.d.ts.map +1 -1
- package/dist/Avatar.js +1 -1
- package/dist/Avatar.js.map +1 -1
- package/dist/Box.d.ts.map +1 -1
- package/dist/Box.js +3 -1
- package/dist/Box.js.map +1 -1
- package/dist/Common.d.ts +16 -12
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.d.ts.map +1 -1
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/ConsentNavigator.d.ts +5 -3
- package/dist/ConsentNavigator.d.ts.map +1 -1
- package/dist/ConsentNavigator.js +4 -4
- package/dist/ConsentNavigator.js.map +1 -1
- package/dist/CustomSelectField.js.map +1 -1
- package/dist/DataTable.d.ts.map +1 -1
- package/dist/DataTable.js +37 -12
- package/dist/DataTable.js.map +1 -1
- package/dist/DateTimeField.js.map +1 -1
- package/dist/DropdownPanel.d.ts +0 -5
- package/dist/DropdownPanel.d.ts.map +1 -1
- package/dist/DropdownPanel.js +0 -5
- package/dist/DropdownPanel.js.map +1 -1
- package/dist/FilterAccordion.d.ts.map +1 -1
- package/dist/FilterAccordion.js.map +1 -1
- package/dist/FilterBoolean.d.ts.map +1 -1
- package/dist/FilterBoolean.js.map +1 -1
- package/dist/FilterSelectMenu.js +1 -1
- package/dist/FilterSelectMenu.js.map +1 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js.map +1 -1
- package/dist/Heading.d.ts.map +1 -1
- package/dist/Heading.js +6 -12
- package/dist/Heading.js.map +1 -1
- package/dist/HeightField.d.ts.map +1 -1
- package/dist/HeightField.js +1 -1
- package/dist/HeightField.js.map +1 -1
- package/dist/Hyperlink.d.ts.map +1 -1
- package/dist/Hyperlink.js +6 -1
- package/dist/Hyperlink.js.map +1 -1
- package/dist/Image.d.ts +1 -1
- package/dist/MarkdownEditorField.d.ts.map +1 -1
- package/dist/MarkdownEditorField.js +1 -1
- package/dist/MarkdownEditorField.js.map +1 -1
- package/dist/OfflineBanner.d.ts +3 -2
- package/dist/OfflineBanner.d.ts.map +1 -1
- package/dist/OfflineBanner.js +3 -2
- package/dist/OfflineBanner.js.map +1 -1
- package/dist/OtpField.d.ts.map +1 -1
- package/dist/OtpField.js +6 -3
- package/dist/OtpField.js.map +1 -1
- package/dist/PickerSelect.d.ts +4 -4
- package/dist/PickerSelect.d.ts.map +1 -1
- package/dist/PickerSelect.js.map +1 -1
- package/dist/ProvisionalConsentSigner.d.ts +3 -2
- package/dist/ProvisionalConsentSigner.d.ts.map +1 -1
- package/dist/ProvisionalConsentSigner.js +2 -2
- package/dist/ProvisionalConsentSigner.js.map +1 -1
- package/dist/SidebarNavigation.js +2 -2
- package/dist/SidebarNavigation.js.map +1 -1
- package/dist/SidebarNavigation.native.js +2 -2
- package/dist/SidebarNavigation.native.js.map +1 -1
- package/dist/Skeleton.js.map +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +27 -50
- package/dist/Text.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js.map +1 -1
- package/dist/ThumbsUpDownFeedback.d.ts.map +1 -1
- package/dist/ThumbsUpDownFeedback.js.map +1 -1
- package/dist/ToastNotifications.d.ts.map +1 -1
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip.js.map +1 -1
- package/dist/Unifier.d.ts +1 -1
- package/dist/WebAddressAutocomplete.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +3 -3
- package/dist/WebDropdownMenu.d.ts.map +1 -1
- package/dist/WebDropdownMenu.js +1 -1
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/announcementPlatform.d.ts +5 -0
- package/dist/announcementPlatform.d.ts.map +1 -1
- package/dist/announcementPlatform.js +9 -0
- package/dist/announcementPlatform.js.map +1 -1
- package/dist/asks/AskCard.d.ts +2 -2
- package/dist/asks/AskCard.d.ts.map +1 -1
- package/dist/dataTableColumnWidths.d.ts +76 -0
- package/dist/dataTableColumnWidths.d.ts.map +1 -0
- package/dist/dataTableColumnWidths.js +181 -0
- package/dist/dataTableColumnWidths.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/login/LoginScreen.js.map +1 -1
- package/dist/requestClient.d.ts +71 -0
- package/dist/requestClient.d.ts.map +1 -0
- package/dist/requestClient.js +139 -0
- package/dist/requestClient.js.map +1 -0
- package/dist/security/PasswordCard.d.ts.map +1 -1
- package/dist/security/PasswordCard.js.map +1 -1
- package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -1
- package/dist/security/TwoFactorSettingsCard.js +1 -1
- package/dist/security/TwoFactorSettingsCard.js.map +1 -1
- package/dist/signUp/SignUpScreen.js.map +1 -1
- package/dist/table/Table.d.ts.map +1 -1
- package/dist/table/Table.js.map +1 -1
- package/dist/table/TableTitle.d.ts.map +1 -1
- package/dist/table/TableTitle.js +2 -1
- package/dist/table/TableTitle.js.map +1 -1
- package/dist/textFontSizes.d.ts +12 -0
- package/dist/textFontSizes.d.ts.map +1 -0
- package/dist/textFontSizes.js +21 -0
- package/dist/textFontSizes.js.map +1 -0
- package/dist/useAcknowledgeAnnouncement.d.ts +8 -63
- package/dist/useAcknowledgeAnnouncement.d.ts.map +1 -1
- package/dist/useAcknowledgeAnnouncement.js +31 -80
- package/dist/useAcknowledgeAnnouncement.js.map +1 -1
- package/dist/useAnnouncements.d.ts +14 -47
- package/dist/useAnnouncements.d.ts.map +1 -1
- package/dist/useAnnouncements.js +30 -57
- package/dist/useAnnouncements.js.map +1 -1
- package/dist/useConsentForms.d.ts +6 -38
- package/dist/useConsentForms.d.ts.map +1 -1
- package/dist/useConsentForms.js +25 -48
- package/dist/useConsentForms.js.map +1 -1
- package/dist/useProvisionalConsent.d.ts +7 -58
- package/dist/useProvisionalConsent.d.ts.map +1 -1
- package/dist/useProvisionalConsent.js +57 -67
- package/dist/useProvisionalConsent.js.map +1 -1
- package/dist/useSubmitConsent.d.ts +9 -39
- package/dist/useSubmitConsent.d.ts.map +1 -1
- package/dist/useSubmitConsent.js +10 -39
- package/dist/useSubmitConsent.js.map +1 -1
- package/package.json +38 -38
- package/src/ActionSheet.test.tsx +4 -4
- package/src/ActionSheet.tsx +18 -5
- package/src/AnnouncementNavigator.test.tsx +215 -315
- package/src/AnnouncementNavigator.tsx +25 -20
- package/src/Avatar.tsx +2 -9
- package/src/Box.test.tsx +3 -3
- package/src/Box.tsx +4 -2
- package/src/Common.ts +19 -15
- package/src/ConsentFormScreen.tsx +2 -1
- package/src/ConsentNavigator.test.tsx +166 -242
- package/src/ConsentNavigator.tsx +9 -13
- package/src/CustomSelectField.tsx +3 -3
- package/src/DataTable.test.tsx +69 -1
- package/src/DataTable.tsx +72 -19
- package/src/DateTimeField.tsx +4 -4
- package/src/DropdownPanel.tsx +0 -6
- package/src/FilterAccordion.tsx +3 -3
- package/src/FilterBoolean.tsx +3 -3
- package/src/FilterSelectMenu.tsx +1 -1
- package/src/GPTChat.tsx +10 -9
- package/src/Heading.tsx +7 -14
- package/src/HeightField.tsx +9 -2
- package/src/Hyperlink.tsx +6 -3
- package/src/Link.test.tsx +2 -2
- package/src/MarkdownEditorField.tsx +10 -2
- package/src/MarkdownView.test.tsx +10 -6
- package/src/Modal.test.tsx +3 -1
- package/src/ModalSheet.test.tsx +6 -6
- package/src/OfflineBanner.tsx +3 -2
- package/src/OtpField.test.tsx +44 -0
- package/src/OtpField.tsx +8 -4
- package/src/PickerSelect.tsx +3 -2
- package/src/ProvisionalConsentSigner.test.tsx +140 -113
- package/src/ProvisionalConsentSigner.tsx +5 -4
- package/src/SidebarNavigation.native.tsx +2 -2
- package/src/SidebarNavigation.tsx +2 -2
- package/src/Skeleton.tsx +1 -1
- package/src/TapToEdit.test.tsx +2 -2
- package/src/Text.test.tsx +1 -1
- package/src/Text.tsx +25 -51
- package/src/TextField.tsx +2 -1
- package/src/ThumbsUpDownFeedback.tsx +3 -3
- package/src/ToastNotifications.tsx +3 -2
- package/src/ToastNotifications.webModal.test.tsx +4 -1
- package/src/Tooltip.tsx +2 -1
- package/src/UserInactivity.test.tsx +2 -3
- package/src/WebAddressAutocomplete.tsx +3 -3
- package/src/WebDropdownMenu.tsx +16 -5
- package/src/__snapshots__/DataTable.test.tsx.snap +13 -0
- package/src/announcementPlatform.ts +10 -0
- package/src/asks/AskCard.test.tsx +14 -12
- package/src/asks/AskCard.tsx +2 -2
- package/src/bunSetup.ts +42 -0
- package/src/dataTableColumnWidths.test.ts +266 -0
- package/src/dataTableColumnWidths.ts +272 -0
- package/src/index.tsx +9 -2
- package/src/login/LoginScreen.tsx +2 -2
- package/src/requestClient.test.ts +288 -0
- package/src/requestClient.ts +228 -0
- package/src/security/PasswordCard.tsx +7 -5
- package/src/security/TwoFactorSettingsCard.tsx +10 -3
- package/src/signUp/SignUpScreen.tsx +2 -2
- package/src/table/Table.tsx +1 -2
- package/src/table/TableTitle.tsx +2 -1
- package/src/test-utils.tsx +76 -0
- package/src/textFontSizes.ts +31 -0
- package/src/useAcknowledgeAnnouncement.test.ts +101 -193
- package/src/useAcknowledgeAnnouncement.ts +69 -147
- package/src/useAnnouncements.test.ts +122 -186
- package/src/useAnnouncements.ts +49 -105
- package/src/useConsentForms.test.ts +59 -112
- package/src/useConsentForms.ts +37 -87
- package/src/useProvisionalConsent.test.ts +74 -150
- package/src/useProvisionalConsent.ts +79 -125
- package/src/useSubmitConsent.test.ts +59 -85
- package/src/useSubmitConsent.ts +22 -77
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {useCallback} from "react";
|
|
2
|
-
|
|
1
|
+
import {useCallback, useRef} from "react";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
invalidateClientTags,
|
|
5
|
+
type TerrenoRequestClient,
|
|
6
|
+
unwrapDataEnvelope,
|
|
7
|
+
useClientMutation,
|
|
8
|
+
useClientQuery,
|
|
9
|
+
} from "./requestClient";
|
|
3
10
|
import type {ConsentFormPublic} from "./useConsentForms";
|
|
4
11
|
|
|
5
12
|
export type ProvisionalConsentStatus = "pending" | "provisional" | "signed" | "declined";
|
|
@@ -57,162 +64,109 @@ export interface ProvisionalConsentResponse {
|
|
|
57
64
|
provisionalSignedBy?: {_id: string; name?: string; email?: string} | string;
|
|
58
65
|
}
|
|
59
66
|
|
|
60
|
-
type MutationTrigger<TBody> = (body: TBody) => {unwrap: () => Promise<unknown>};
|
|
61
|
-
|
|
62
|
-
type MutationHook<TBody> = () => [MutationTrigger<TBody>, {isLoading: boolean; error?: unknown}];
|
|
63
|
-
|
|
64
67
|
export interface VoidProvisionalConsentArgs {
|
|
65
68
|
responseId: string;
|
|
66
69
|
reason: string;
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
interface ProvisionalConsentQueryBuilder {
|
|
70
|
-
query: (options: {providesTags: string[]; query: (userId: string) => string}) => unknown;
|
|
71
|
-
mutation: <TArgs>(options: {
|
|
72
|
-
invalidatesTags: string[];
|
|
73
|
-
query: (args: TArgs) => {body: unknown; method: "POST"; url: string};
|
|
74
|
-
}) => unknown;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
interface ProvisionalConsentQueryState {
|
|
78
|
-
data?: ProvisionalConsentStatusRow[] | ConsentStatusResponse;
|
|
79
|
-
error: unknown;
|
|
80
|
-
isLoading: boolean;
|
|
81
|
-
refetch: () => void | Promise<void>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
interface ProvisionalConsentEnhancedApi {
|
|
85
|
-
useGetConsentStatusQuery: (userId: string) => ProvisionalConsentQueryState;
|
|
86
|
-
useSignConsentProvisionallyMutation: MutationHook<SignConsentProvisionallyBody>;
|
|
87
|
-
useVoidProvisionalConsentMutation: MutationHook<VoidProvisionalConsentArgs>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface ProvisionalConsentApiWithTags {
|
|
91
|
-
injectEndpoints: (options: {
|
|
92
|
-
endpoints: (build: ProvisionalConsentQueryBuilder) => {
|
|
93
|
-
getConsentStatus: unknown;
|
|
94
|
-
signConsentProvisionally: unknown;
|
|
95
|
-
voidProvisionalConsent: unknown;
|
|
96
|
-
};
|
|
97
|
-
overrideExisting: boolean;
|
|
98
|
-
}) => ProvisionalConsentEnhancedApi;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** The consumer's RTK Query api, as passed to `ConsentNavigator`. */
|
|
102
|
-
export interface ProvisionalConsentApi {
|
|
103
|
-
enhanceEndpoints: (options: {addTagTypes: string[]}) => ProvisionalConsentApiWithTags;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
72
|
export interface UseProvisionalConsentResult {
|
|
107
73
|
statuses: ProvisionalConsentStatusRow[];
|
|
108
74
|
isLoading: boolean;
|
|
109
75
|
error: unknown;
|
|
110
|
-
refetch: () =>
|
|
111
|
-
/** Sign a form provisionally for the user. Rejects with the
|
|
76
|
+
refetch: () => Promise<void>;
|
|
77
|
+
/** Sign a form provisionally for the user. Rejects with the request error on failure. */
|
|
112
78
|
sign: (body: Omit<SignConsentProvisionallyBody, "userId">) => Promise<ProvisionalConsentResponse>;
|
|
113
79
|
isSigning: boolean;
|
|
114
|
-
/** Void a provisional signature made in error. Rejects with the
|
|
80
|
+
/** Void a provisional signature made in error. Rejects with the request error on failure. */
|
|
115
81
|
voidSignature: (args: VoidProvisionalConsentArgs) => Promise<ProvisionalConsentResponse>;
|
|
116
82
|
isVoiding: boolean;
|
|
117
83
|
}
|
|
118
84
|
|
|
119
|
-
/** Mutations may arrive as `{data}` envelopes or already unwrapped by the base query. */
|
|
120
|
-
const unwrapData = <T>(result: unknown): T => {
|
|
121
|
-
if (result && typeof result === "object" && "data" in result) {
|
|
122
|
-
return (result as {data: T}).data;
|
|
123
|
-
}
|
|
124
|
-
return result as T;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Cache the enhanced api per (api, baseUrl) so `injectEndpoints` runs once, as in
|
|
129
|
-
* `useConsentForms`: re-injecting the same endpoint name logs a development error.
|
|
130
|
-
*/
|
|
131
|
-
const enhancedApiCache = new WeakMap<
|
|
132
|
-
ProvisionalConsentApi,
|
|
133
|
-
Map<string, ProvisionalConsentEnhancedApi>
|
|
134
|
-
>();
|
|
135
|
-
|
|
136
|
-
const getEnhancedApi = (
|
|
137
|
-
api: ProvisionalConsentApi,
|
|
138
|
-
base: string
|
|
139
|
-
): ProvisionalConsentEnhancedApi => {
|
|
140
|
-
let byBase = enhancedApiCache.get(api);
|
|
141
|
-
if (!byBase) {
|
|
142
|
-
byBase = new Map();
|
|
143
|
-
enhancedApiCache.set(api, byBase);
|
|
144
|
-
}
|
|
145
|
-
const cached = byBase.get(base);
|
|
146
|
-
if (cached) {
|
|
147
|
-
return cached;
|
|
148
|
-
}
|
|
149
|
-
const apiWithTags = api.enhanceEndpoints({addTagTypes: ["ConsentStatus", "PendingConsents"]});
|
|
150
|
-
const enhanced = apiWithTags.injectEndpoints({
|
|
151
|
-
endpoints: (build) => ({
|
|
152
|
-
getConsentStatus: build.query({
|
|
153
|
-
providesTags: ["ConsentStatus"],
|
|
154
|
-
query: (userId: string) =>
|
|
155
|
-
`${base}/consent-responses/status?userId=${encodeURIComponent(userId)}`,
|
|
156
|
-
}),
|
|
157
|
-
// A provisional signature changes the target's statuses and their pending notice.
|
|
158
|
-
signConsentProvisionally: build.mutation<SignConsentProvisionallyBody>({
|
|
159
|
-
invalidatesTags: ["ConsentStatus", "PendingConsents"],
|
|
160
|
-
query: (body) => ({
|
|
161
|
-
body,
|
|
162
|
-
method: "POST",
|
|
163
|
-
url: `${base}/consent-responses/provisional`,
|
|
164
|
-
}),
|
|
165
|
-
}),
|
|
166
|
-
// A void brings the form back to pending and removes the user's notice.
|
|
167
|
-
voidProvisionalConsent: build.mutation<VoidProvisionalConsentArgs>({
|
|
168
|
-
invalidatesTags: ["ConsentStatus", "PendingConsents"],
|
|
169
|
-
query: ({reason, responseId}) => ({
|
|
170
|
-
body: {reason},
|
|
171
|
-
method: "POST",
|
|
172
|
-
url: `${base}/consent-responses/${encodeURIComponent(responseId)}/void`,
|
|
173
|
-
}),
|
|
174
|
-
}),
|
|
175
|
-
}),
|
|
176
|
-
overrideExisting: false,
|
|
177
|
-
});
|
|
178
|
-
byBase.set(base, enhanced);
|
|
179
|
-
return enhanced;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
85
|
/** Consent status of each form for `userId`, for staff who sign consent on the user's behalf. */
|
|
183
86
|
export const useProvisionalConsent = ({
|
|
184
|
-
api,
|
|
185
87
|
baseUrl,
|
|
88
|
+
client,
|
|
186
89
|
userId,
|
|
187
90
|
}: {
|
|
188
|
-
/** The consumer's RTK Query api. Typed loosely, like `ConsentNavigator`, so any app api fits. */
|
|
189
|
-
api: unknown;
|
|
190
91
|
baseUrl?: string;
|
|
92
|
+
/** The app's request client, as passed to `ConsentNavigator`. */
|
|
93
|
+
client: TerrenoRequestClient;
|
|
191
94
|
userId: string;
|
|
192
95
|
}): UseProvisionalConsentResult => {
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
96
|
+
const base = baseUrl || "";
|
|
97
|
+
|
|
98
|
+
const fetchStatuses = useCallback(async (): Promise<ProvisionalConsentStatusRow[]> => {
|
|
99
|
+
const result = await client.request<ProvisionalConsentStatusRow[] | ConsentStatusResponse>({
|
|
100
|
+
params: {userId},
|
|
101
|
+
url: `${base}/consent-responses/status`,
|
|
102
|
+
});
|
|
103
|
+
return unwrapDataEnvelope<ProvisionalConsentStatusRow[]>(result) ?? [];
|
|
104
|
+
}, [base, client, userId]);
|
|
105
|
+
|
|
106
|
+
const {data, error, isLoading, refetch} = useClientQuery({
|
|
107
|
+
client,
|
|
108
|
+
fetcher: fetchStatuses,
|
|
109
|
+
providesTag: "ConsentStatus",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// `useClientMutation` runs to completion without a value, so the response waits in a ref.
|
|
113
|
+
const signedRef = useRef<ProvisionalConsentResponse | undefined>(undefined);
|
|
114
|
+
const voidedRef = useRef<ProvisionalConsentResponse | undefined>(undefined);
|
|
115
|
+
|
|
116
|
+
// A provisional signature changes the target's statuses and their pending notice; a void
|
|
117
|
+
// brings the form back to pending and removes the user's notice.
|
|
118
|
+
const runSign = useCallback(
|
|
119
|
+
async (body: Omit<SignConsentProvisionallyBody, "userId">): Promise<void> => {
|
|
120
|
+
const result = await client.request<ProvisionalConsentResponse>({
|
|
121
|
+
body: {...body, userId},
|
|
122
|
+
method: "POST",
|
|
123
|
+
url: `${base}/consent-responses/provisional`,
|
|
124
|
+
});
|
|
125
|
+
signedRef.current = unwrapDataEnvelope<ProvisionalConsentResponse>(result);
|
|
126
|
+
invalidateClientTags({client, tags: ["ConsentStatus", "PendingConsents"]});
|
|
127
|
+
},
|
|
128
|
+
[base, client, userId]
|
|
129
|
+
);
|
|
130
|
+
const runVoid = useCallback(
|
|
131
|
+
async ({reason, responseId}: VoidProvisionalConsentArgs): Promise<void> => {
|
|
132
|
+
const result = await client.request<ProvisionalConsentResponse>({
|
|
133
|
+
body: {reason},
|
|
134
|
+
method: "POST",
|
|
135
|
+
url: `${base}/consent-responses/${encodeURIComponent(responseId)}/void`,
|
|
136
|
+
});
|
|
137
|
+
voidedRef.current = unwrapDataEnvelope<ProvisionalConsentResponse>(result);
|
|
138
|
+
invalidateClientTags({client, tags: ["ConsentStatus", "PendingConsents"]});
|
|
139
|
+
},
|
|
140
|
+
[base, client]
|
|
141
|
+
);
|
|
142
|
+
const {isLoading: isSigning, mutate: signMutate} = useClientMutation({run: runSign});
|
|
143
|
+
const {isLoading: isVoiding, mutate: voidMutate} = useClientMutation({run: runVoid});
|
|
197
144
|
|
|
198
145
|
const sign = useCallback(
|
|
199
146
|
async (
|
|
200
147
|
body: Omit<SignConsentProvisionallyBody, "userId">
|
|
201
148
|
): Promise<ProvisionalConsentResponse> => {
|
|
202
|
-
|
|
203
|
-
return
|
|
149
|
+
await signMutate(body);
|
|
150
|
+
return signedRef.current as ProvisionalConsentResponse;
|
|
204
151
|
},
|
|
205
|
-
[
|
|
152
|
+
[signMutate]
|
|
206
153
|
);
|
|
207
|
-
|
|
208
|
-
const [voidMutation, {isLoading: isVoiding}] = enhancedApi.useVoidProvisionalConsentMutation();
|
|
209
154
|
const voidSignature = useCallback(
|
|
210
155
|
async (args: VoidProvisionalConsentArgs): Promise<ProvisionalConsentResponse> => {
|
|
211
|
-
|
|
212
|
-
return
|
|
156
|
+
await voidMutate(args);
|
|
157
|
+
return voidedRef.current as ProvisionalConsentResponse;
|
|
213
158
|
},
|
|
214
|
-
[
|
|
159
|
+
[voidMutate]
|
|
215
160
|
);
|
|
216
161
|
|
|
217
|
-
return {
|
|
162
|
+
return {
|
|
163
|
+
error,
|
|
164
|
+
isLoading,
|
|
165
|
+
isSigning,
|
|
166
|
+
isVoiding,
|
|
167
|
+
refetch,
|
|
168
|
+
sign,
|
|
169
|
+
statuses: data ?? [],
|
|
170
|
+
voidSignature,
|
|
171
|
+
};
|
|
218
172
|
};
|
|
@@ -1,100 +1,74 @@
|
|
|
1
|
-
import {describe, expect, it
|
|
2
|
-
import {renderHook} from "@testing-library/react-native";
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {act, renderHook} from "@testing-library/react-native";
|
|
3
3
|
|
|
4
|
-
import
|
|
4
|
+
import {createFakeRequestClient, fakeHttpError} from "./test-utils";
|
|
5
5
|
import {useSubmitConsent} from "./useSubmitConsent";
|
|
6
6
|
|
|
7
|
-
type SubmitConsentApi = Parameters<typeof useSubmitConsent>[0];
|
|
8
|
-
|
|
9
|
-
interface MockMutationDef {
|
|
10
|
-
query: (body: SubmitConsentBody) => {method: string; url: string};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface MockInjectOpts {
|
|
14
|
-
endpoints: (build: {mutation: (def: MockMutationDef) => string}) => Record<string, unknown>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
7
|
describe("useSubmitConsent", () => {
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const useSubmitConsentResponseMutation = mock(() => [
|
|
22
|
-
submitMutation,
|
|
23
|
-
{error: undefined, isLoading: false},
|
|
24
|
-
]);
|
|
25
|
-
const api = {
|
|
26
|
-
enhanceEndpoints: mock(() => ({
|
|
27
|
-
injectEndpoints: mock((opts: MockInjectOpts) => {
|
|
28
|
-
const build = {
|
|
29
|
-
mutation: mock((def: MockMutationDef) => {
|
|
30
|
-
// Exercise the query builder
|
|
31
|
-
const result = def.query({
|
|
32
|
-
agreed: true,
|
|
33
|
-
consentFormId: "f1",
|
|
34
|
-
locale: "en",
|
|
35
|
-
});
|
|
36
|
-
expect(result.method).toBe("POST");
|
|
37
|
-
expect(result.url).toContain("/consents/respond");
|
|
38
|
-
return "submit-mutation";
|
|
39
|
-
}),
|
|
40
|
-
};
|
|
41
|
-
opts.endpoints(build);
|
|
42
|
-
return {useSubmitConsentResponseMutation};
|
|
43
|
-
}),
|
|
44
|
-
})),
|
|
45
|
-
};
|
|
46
|
-
return {api, submitMutation, unwrap};
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
it("returns submit function and state", () => {
|
|
50
|
-
const {api} = buildApi();
|
|
51
|
-
const {result} = renderHook(() => useSubmitConsent(api as unknown as SubmitConsentApi));
|
|
52
|
-
expect(typeof result.current.submit).toBe("function");
|
|
8
|
+
it("starts idle", () => {
|
|
9
|
+
const {client} = createFakeRequestClient({});
|
|
10
|
+
const {result} = renderHook(() => useSubmitConsent(client));
|
|
53
11
|
expect(result.current.isSubmitting).toBe(false);
|
|
54
12
|
expect(result.current.error).toBeUndefined();
|
|
13
|
+
expect(typeof result.current.submit).toBe("function");
|
|
55
14
|
});
|
|
56
15
|
|
|
57
|
-
it("
|
|
58
|
-
const {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
16
|
+
it("posts the consent response to /consents/respond under baseUrl", async () => {
|
|
17
|
+
const {client, calls} = createFakeRequestClient({
|
|
18
|
+
"POST /api/consents/respond": () => ({agreed: true, id: "response-1"}),
|
|
19
|
+
});
|
|
20
|
+
const {result} = renderHook(() => useSubmitConsent(client, "/api"));
|
|
21
|
+
await act(async () => {
|
|
22
|
+
await result.current.submit({
|
|
23
|
+
agreed: true,
|
|
24
|
+
checkboxValues: {"0": true},
|
|
25
|
+
consentFormId: "form-1",
|
|
26
|
+
locale: "en",
|
|
27
|
+
signature: "sig",
|
|
28
|
+
});
|
|
64
29
|
});
|
|
65
|
-
expect(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
30
|
+
expect(calls).toEqual([
|
|
31
|
+
{
|
|
32
|
+
body: {
|
|
33
|
+
agreed: true,
|
|
34
|
+
checkboxValues: {"0": true},
|
|
35
|
+
consentFormId: "form-1",
|
|
36
|
+
locale: "en",
|
|
37
|
+
signature: "sig",
|
|
38
|
+
},
|
|
39
|
+
method: "POST",
|
|
40
|
+
url: "/api/consents/respond",
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
expect(result.current.isSubmitting).toBe(false);
|
|
69
44
|
});
|
|
70
45
|
|
|
71
|
-
it("uses
|
|
72
|
-
const {
|
|
73
|
-
const {result} = renderHook(() => useSubmitConsent(
|
|
74
|
-
|
|
46
|
+
it("uses no prefix when baseUrl is omitted", async () => {
|
|
47
|
+
const {client, calls} = createFakeRequestClient({"POST /consents/respond": () => ({})});
|
|
48
|
+
const {result} = renderHook(() => useSubmitConsent(client));
|
|
49
|
+
await act(async () => {
|
|
50
|
+
await result.current.submit({agreed: false, consentFormId: "form-1", locale: "en"});
|
|
51
|
+
});
|
|
52
|
+
expect(calls[0]?.url).toBe("/consents/respond");
|
|
75
53
|
});
|
|
76
54
|
|
|
77
|
-
it("
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
rerender(undefined);
|
|
96
|
-
// The hook reuses the cached enhanced api after the first render so the
|
|
97
|
-
// dev-mode RTK warning about re-injecting endpoints never fires.
|
|
98
|
-
expect(injectCallCount).toBe(1);
|
|
55
|
+
it("rejects and exposes the error when the request fails", async () => {
|
|
56
|
+
const failure = fakeHttpError(400, "Invalid");
|
|
57
|
+
const {client} = createFakeRequestClient({
|
|
58
|
+
"POST /consents/respond": () => {
|
|
59
|
+
throw failure;
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
const {result} = renderHook(() => useSubmitConsent(client));
|
|
63
|
+
let caught: unknown;
|
|
64
|
+
await act(async () => {
|
|
65
|
+
await result.current
|
|
66
|
+
.submit({agreed: true, consentFormId: "form-1", locale: "en"})
|
|
67
|
+
.catch((error: unknown) => {
|
|
68
|
+
caught = error;
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
expect(caught).toBe(failure);
|
|
72
|
+
expect(result.current.error).toBe(failure);
|
|
99
73
|
});
|
|
100
74
|
});
|
package/src/useSubmitConsent.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {useCallback} from "react";
|
|
2
|
+
|
|
3
|
+
import {invalidateClientTags, type TerrenoRequestClient, useClientMutation} from "./requestClient";
|
|
4
|
+
|
|
1
5
|
export interface SubmitConsentBody {
|
|
2
6
|
agreed: boolean;
|
|
3
7
|
checkboxValues?: Record<string, boolean>;
|
|
@@ -6,91 +10,32 @@ export interface SubmitConsentBody {
|
|
|
6
10
|
signature?: string;
|
|
7
11
|
}
|
|
8
12
|
|
|
9
|
-
interface
|
|
10
|
-
unwrap: () => Promise<unknown>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface SubmitConsentMutationHookState {
|
|
13
|
+
export interface UseSubmitConsentResult {
|
|
14
14
|
error: unknown;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
interface SubmitConsentMutationBuilder {
|
|
19
|
-
mutation: (options: {
|
|
20
|
-
invalidatesTags: string[];
|
|
21
|
-
query: (body: SubmitConsentBody) => {
|
|
22
|
-
body: SubmitConsentBody;
|
|
23
|
-
method: "POST";
|
|
24
|
-
url: string;
|
|
25
|
-
};
|
|
26
|
-
}) => unknown;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface SubmitConsentEnhancedApi {
|
|
30
|
-
useSubmitConsentResponseMutation: () => [
|
|
31
|
-
(body: SubmitConsentBody) => SubmitConsentMutationResult,
|
|
32
|
-
SubmitConsentMutationHookState,
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface SubmitConsentApiWithTags {
|
|
37
|
-
injectEndpoints: (options: {
|
|
38
|
-
endpoints: (build: SubmitConsentMutationBuilder) => {submitConsentResponse: unknown};
|
|
39
|
-
overrideExisting: boolean;
|
|
40
|
-
}) => SubmitConsentEnhancedApi;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface SubmitConsentApi {
|
|
44
|
-
enhanceEndpoints: (options: {addTagTypes: string[]}) => SubmitConsentApiWithTags;
|
|
15
|
+
isSubmitting: boolean;
|
|
16
|
+
/** Post a consent response; rejects with the request error. */
|
|
17
|
+
submit: (body: SubmitConsentBody) => Promise<void>;
|
|
45
18
|
}
|
|
46
19
|
|
|
47
20
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* (with `overrideExisting: false`), so calling it on every render of every
|
|
51
|
-
* consumer would flood the console. WeakMap-by-api lets the GC reclaim entries
|
|
52
|
-
* when the api object is unreachable.
|
|
21
|
+
* Submit a consent response (`POST /consents/respond`). On success every mounted
|
|
22
|
+
* `useConsentForms` on the same client refetches its pending forms.
|
|
53
23
|
*/
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (!byBase) {
|
|
59
|
-
byBase = new Map();
|
|
60
|
-
enhancedApiCache.set(api, byBase);
|
|
61
|
-
}
|
|
62
|
-
const cached = byBase.get(base);
|
|
63
|
-
if (cached) {
|
|
64
|
-
return cached;
|
|
65
|
-
}
|
|
66
|
-
const apiWithConsentTags = api.enhanceEndpoints({addTagTypes: ["PendingConsents"]});
|
|
67
|
-
const enhanced = apiWithConsentTags.injectEndpoints({
|
|
68
|
-
endpoints: (build) => ({
|
|
69
|
-
submitConsentResponse: build.mutation({
|
|
70
|
-
invalidatesTags: ["PendingConsents"],
|
|
71
|
-
query: (body: SubmitConsentBody) => ({
|
|
72
|
-
body,
|
|
73
|
-
method: "POST",
|
|
74
|
-
url: `${base}/consents/respond`,
|
|
75
|
-
}),
|
|
76
|
-
}),
|
|
77
|
-
}),
|
|
78
|
-
overrideExisting: false,
|
|
79
|
-
});
|
|
80
|
-
byBase.set(base, enhanced);
|
|
81
|
-
return enhanced;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export const useSubmitConsent = (api: SubmitConsentApi, baseUrl?: string) => {
|
|
24
|
+
export const useSubmitConsent = (
|
|
25
|
+
client: TerrenoRequestClient,
|
|
26
|
+
baseUrl?: string
|
|
27
|
+
): UseSubmitConsentResult => {
|
|
85
28
|
const base = baseUrl || "";
|
|
86
|
-
const enhancedApi = getEnhancedApi(api, base);
|
|
87
29
|
|
|
88
|
-
const
|
|
89
|
-
|
|
30
|
+
const run = useCallback(
|
|
31
|
+
async (body: SubmitConsentBody): Promise<void> => {
|
|
32
|
+
await client.request({body: {...body}, method: "POST", url: `${base}/consents/respond`});
|
|
33
|
+
invalidateClientTags({client, tags: ["PendingConsents"]});
|
|
34
|
+
},
|
|
35
|
+
[base, client]
|
|
36
|
+
);
|
|
90
37
|
|
|
91
|
-
const
|
|
92
|
-
await submitMutation(body).unwrap();
|
|
93
|
-
};
|
|
38
|
+
const {error, isLoading: isSubmitting, mutate: submit} = useClientMutation({run});
|
|
94
39
|
|
|
95
40
|
return {error, isSubmitting, submit};
|
|
96
41
|
};
|