@velion-la/onboarding-sdk-react-native 1.4.0-beta.7 → 1.4.0-beta.8

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 (119) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +31 -1
  3. package/lib/commonjs/core/VelionOnboarding.js +52 -44
  4. package/lib/commonjs/core/VelionOnboarding.js.map +1 -1
  5. package/lib/commonjs/core/routingCommands.js +33 -0
  6. package/lib/commonjs/core/routingCommands.js.map +1 -1
  7. package/lib/commonjs/core/sessionStateMapper.js +48 -36
  8. package/lib/commonjs/core/sessionStateMapper.js.map +1 -1
  9. package/lib/commonjs/i18n/strings/en.js +27 -0
  10. package/lib/commonjs/i18n/strings/en.js.map +1 -1
  11. package/lib/commonjs/i18n/strings/es.js +27 -0
  12. package/lib/commonjs/i18n/strings/es.js.map +1 -1
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/steps/CustomFormStep.js +348 -0
  15. package/lib/commonjs/steps/CustomFormStep.js.map +1 -0
  16. package/lib/commonjs/steps/EntityTypeSelectionStep.js +194 -0
  17. package/lib/commonjs/steps/EntityTypeSelectionStep.js.map +1 -0
  18. package/lib/commonjs/steps/customForm.js +251 -0
  19. package/lib/commonjs/steps/customForm.js.map +1 -0
  20. package/lib/commonjs/steps/entityType.js +30 -0
  21. package/lib/commonjs/steps/entityType.js.map +1 -0
  22. package/lib/commonjs/steps/submissionOutcome.js +101 -0
  23. package/lib/commonjs/steps/submissionOutcome.js.map +1 -0
  24. package/lib/commonjs/steps/unsupportedStep.js +33 -0
  25. package/lib/commonjs/steps/unsupportedStep.js.map +1 -0
  26. package/lib/commonjs/types/index.js +35 -3
  27. package/lib/commonjs/types/index.js.map +1 -1
  28. package/lib/commonjs/ui/primitives/RadioCard.js +120 -0
  29. package/lib/commonjs/ui/primitives/RadioCard.js.map +1 -0
  30. package/lib/commonjs/ui/primitives/Select.js +60 -7
  31. package/lib/commonjs/ui/primitives/Select.js.map +1 -1
  32. package/lib/commonjs/ui/primitives/index.js +7 -0
  33. package/lib/commonjs/ui/primitives/index.js.map +1 -1
  34. package/lib/commonjs/utils/sdkVersion.js +1 -1
  35. package/lib/module/core/VelionOnboarding.js +53 -45
  36. package/lib/module/core/VelionOnboarding.js.map +1 -1
  37. package/lib/module/core/routingCommands.js +33 -0
  38. package/lib/module/core/routingCommands.js.map +1 -1
  39. package/lib/module/core/sessionStateMapper.js +47 -36
  40. package/lib/module/core/sessionStateMapper.js.map +1 -1
  41. package/lib/module/i18n/strings/en.js +27 -0
  42. package/lib/module/i18n/strings/en.js.map +1 -1
  43. package/lib/module/i18n/strings/es.js +27 -0
  44. package/lib/module/i18n/strings/es.js.map +1 -1
  45. package/lib/module/index.js.map +1 -1
  46. package/lib/module/steps/CustomFormStep.js +345 -0
  47. package/lib/module/steps/CustomFormStep.js.map +1 -0
  48. package/lib/module/steps/EntityTypeSelectionStep.js +189 -0
  49. package/lib/module/steps/EntityTypeSelectionStep.js.map +1 -0
  50. package/lib/module/steps/customForm.js +237 -0
  51. package/lib/module/steps/customForm.js.map +1 -0
  52. package/lib/module/steps/entityType.js +24 -0
  53. package/lib/module/steps/entityType.js.map +1 -0
  54. package/lib/module/steps/submissionOutcome.js +94 -0
  55. package/lib/module/steps/submissionOutcome.js.map +1 -0
  56. package/lib/module/steps/unsupportedStep.js +30 -0
  57. package/lib/module/steps/unsupportedStep.js.map +1 -0
  58. package/lib/module/types/index.js +42 -3
  59. package/lib/module/types/index.js.map +1 -1
  60. package/lib/module/ui/primitives/RadioCard.js +116 -0
  61. package/lib/module/ui/primitives/RadioCard.js.map +1 -0
  62. package/lib/module/ui/primitives/Select.js +62 -9
  63. package/lib/module/ui/primitives/Select.js.map +1 -1
  64. package/lib/module/ui/primitives/index.js +1 -0
  65. package/lib/module/ui/primitives/index.js.map +1 -1
  66. package/lib/module/utils/sdkVersion.js +1 -1
  67. package/lib/typescript/core/VelionOnboarding.d.ts +11 -5
  68. package/lib/typescript/core/VelionOnboarding.d.ts.map +1 -1
  69. package/lib/typescript/core/routingCommands.d.ts +23 -0
  70. package/lib/typescript/core/routingCommands.d.ts.map +1 -1
  71. package/lib/typescript/core/sessionStateMapper.d.ts +13 -5
  72. package/lib/typescript/core/sessionStateMapper.d.ts.map +1 -1
  73. package/lib/typescript/i18n/strings/en.d.ts.map +1 -1
  74. package/lib/typescript/i18n/strings/es.d.ts +27 -0
  75. package/lib/typescript/i18n/strings/es.d.ts.map +1 -1
  76. package/lib/typescript/index.d.ts +1 -1
  77. package/lib/typescript/index.d.ts.map +1 -1
  78. package/lib/typescript/steps/CustomFormStep.d.ts +28 -0
  79. package/lib/typescript/steps/CustomFormStep.d.ts.map +1 -0
  80. package/lib/typescript/steps/EntityTypeSelectionStep.d.ts +24 -0
  81. package/lib/typescript/steps/EntityTypeSelectionStep.d.ts.map +1 -0
  82. package/lib/typescript/steps/customForm.d.ts +99 -0
  83. package/lib/typescript/steps/customForm.d.ts.map +1 -0
  84. package/lib/typescript/steps/entityType.d.ts +15 -0
  85. package/lib/typescript/steps/entityType.d.ts.map +1 -0
  86. package/lib/typescript/steps/submissionOutcome.d.ts +43 -0
  87. package/lib/typescript/steps/submissionOutcome.d.ts.map +1 -0
  88. package/lib/typescript/steps/types.d.ts +5 -0
  89. package/lib/typescript/steps/types.d.ts.map +1 -1
  90. package/lib/typescript/steps/unsupportedStep.d.ts +15 -0
  91. package/lib/typescript/steps/unsupportedStep.d.ts.map +1 -0
  92. package/lib/typescript/types/index.d.ts +107 -14
  93. package/lib/typescript/types/index.d.ts.map +1 -1
  94. package/lib/typescript/ui/primitives/RadioCard.d.ts +22 -0
  95. package/lib/typescript/ui/primitives/RadioCard.d.ts.map +1 -0
  96. package/lib/typescript/ui/primitives/Select.d.ts +9 -3
  97. package/lib/typescript/ui/primitives/Select.d.ts.map +1 -1
  98. package/lib/typescript/ui/primitives/index.d.ts +1 -0
  99. package/lib/typescript/ui/primitives/index.d.ts.map +1 -1
  100. package/lib/typescript/utils/sdkVersion.d.ts +1 -1
  101. package/package.json +1 -1
  102. package/src/core/VelionOnboarding.ts +59 -53
  103. package/src/core/routingCommands.ts +42 -0
  104. package/src/core/sessionStateMapper.ts +63 -52
  105. package/src/i18n/strings/en.ts +28 -0
  106. package/src/i18n/strings/es.ts +28 -0
  107. package/src/index.ts +7 -0
  108. package/src/steps/CustomFormStep.tsx +400 -0
  109. package/src/steps/EntityTypeSelectionStep.tsx +207 -0
  110. package/src/steps/customForm.ts +302 -0
  111. package/src/steps/entityType.ts +25 -0
  112. package/src/steps/submissionOutcome.ts +93 -0
  113. package/src/steps/types.ts +5 -0
  114. package/src/steps/unsupportedStep.ts +32 -0
  115. package/src/types/index.ts +117 -17
  116. package/src/ui/primitives/RadioCard.tsx +127 -0
  117. package/src/ui/primitives/Select.tsx +73 -7
  118. package/src/ui/primitives/index.ts +1 -0
  119. package/src/utils/sdkVersion.ts +1 -1
@@ -0,0 +1,400 @@
1
+ /**
2
+ * CUSTOM_FORM — POST /sessions/:id/custom-form/:stepKey { responses }.
3
+ *
4
+ * A form the workflow defines field by field (`config.form_fields`), with an
5
+ * optional `title` / `subtitle`. A workflow may hold several, so the step
6
+ * submits to its own `step_key`, never to the code. Field types and rules are
7
+ * in `customForm.ts` (spike VON-131 §3.2): TEXT → `Input`, NUMBER → decimal
8
+ * `Input`, BOOLEAN → Yes / No cards, DATE → `DatePicker`, LIST → `Select`
9
+ * (searchable above eight options).
10
+ *
11
+ * The POST answers 202 and the next step arrives over SSE. A 400 names the
12
+ * refused answers (`errors["responses.<field_key>"]`), shown next to each
13
+ * field and cleared when that field is edited; the other failures follow
14
+ * `submissionOutcome.ts`. A required field of
15
+ * a type this SDK does not know fails the step with `UNSUPPORTED_STEP` before
16
+ * anything renders. Prefill: `result.responses` (reverse navigation).
17
+ */
18
+
19
+ import { useCallback, useState } from 'react';
20
+ import { StyleSheet, View } from 'react-native';
21
+ import {
22
+ SdkErrorCode,
23
+ type CustomFormSubmitPayload,
24
+ type SdkError,
25
+ } from '../types';
26
+ import { Banner } from '../ui/primitives/Banner';
27
+ import { Button } from '../ui/primitives/Button';
28
+ import { DatePicker } from '../ui/primitives/DatePicker';
29
+ import { Input } from '../ui/primitives/Input';
30
+ import { RadioCard } from '../ui/primitives/RadioCard';
31
+ import { Select } from '../ui/primitives/Select';
32
+ import { Text } from '../ui/primitives/Text';
33
+ import { LoadingOverlay } from '../ui/overlays/LoadingOverlay';
34
+ import { RetryDialog } from '../ui/overlays/RetryDialog';
35
+ import { StepShell } from '../ui/StepShell';
36
+ import { useTheme } from '../ui/theme';
37
+ import { getStrings } from '../i18n';
38
+ import { log } from '../utils/log';
39
+ import type { ApiCaller } from '../transport/api';
40
+ import type { StepDeps, StepModule } from './types';
41
+ import { toSubmissionError } from './_shared';
42
+ import {
43
+ SEARCHABLE_LIST_THRESHOLD,
44
+ buildResponses,
45
+ initialDrafts,
46
+ parseCustomFormConfig,
47
+ splitCustomFormErrors,
48
+ validateForm,
49
+ type CustomFormFieldDef,
50
+ type FieldDraft,
51
+ type ParsedCustomForm,
52
+ } from './customForm';
53
+ import { classifySubmissionError } from './submissionOutcome';
54
+ import { unsupportedStepError } from './unsupportedStep';
55
+
56
+ /** Latest year the DATE picker offers: dates of a generic form may lie ahead. */
57
+ const DATE_MAX_YEARS_AHEAD = 50;
58
+
59
+ export class CustomFormStep implements StepModule {
60
+ private resolveStep!: () => void;
61
+ private rejectStep!: (err: SdkError) => void;
62
+ private destroyed = false;
63
+
64
+ constructor(private readonly deps: StepDeps) {}
65
+
66
+ run(): Promise<void> {
67
+ const form = parseCustomFormConfig(this.deps.stepConfig);
68
+ if (form.unsupportedFields.length > 0) {
69
+ const types = form.unsupportedFields.map((f) => `${f.fieldKey}:${f.type}`).join(', ');
70
+ return Promise.reject(
71
+ unsupportedStepError(
72
+ this.deps.locale,
73
+ this.deps.step,
74
+ this.deps.stepKey,
75
+ `CUSTOM_FORM "${this.deps.stepKey}" has required fields of a type this SDK does not support (${types}).`,
76
+ ),
77
+ );
78
+ }
79
+ return new Promise((resolve, reject) => {
80
+ this.resolveStep = resolve;
81
+ this.rejectStep = reject;
82
+ this.deps.ui.setNode(
83
+ <CustomFormView
84
+ api={this.deps.api}
85
+ sessionId={this.deps.sessionId}
86
+ stepKey={this.deps.stepKey}
87
+ locale={this.deps.locale}
88
+ form={form}
89
+ prefill={this.deps.stepResult}
90
+ onBack={this.deps.onBack}
91
+ onComplete={() => {
92
+ if (this.destroyed) return;
93
+ this.deps.ui.setNode(null);
94
+ this.resolveStep();
95
+ }}
96
+ onCancel={(err) => {
97
+ if (this.destroyed) return;
98
+ this.deps.ui.setNode(null);
99
+ this.rejectStep(err);
100
+ }}
101
+ />,
102
+ );
103
+ });
104
+ }
105
+
106
+ destroy(): void {
107
+ this.destroyed = true;
108
+ this.deps.ui.setNode(null);
109
+ }
110
+ }
111
+
112
+ interface ViewProps {
113
+ api: ApiCaller;
114
+ sessionId: string;
115
+ stepKey: string;
116
+ locale: string;
117
+ form: ParsedCustomForm;
118
+ prefill?: Record<string, unknown> | undefined;
119
+ onBack?: (() => void) | undefined;
120
+ onComplete: () => void;
121
+ onCancel: (err: SdkError) => void;
122
+ }
123
+
124
+ function CustomFormView({
125
+ api,
126
+ sessionId,
127
+ stepKey,
128
+ locale,
129
+ form,
130
+ prefill,
131
+ onBack,
132
+ onComplete,
133
+ onCancel,
134
+ }: ViewProps) {
135
+ const t = getStrings(locale);
136
+ const [drafts, setDrafts] = useState<Record<string, FieldDraft>>(() =>
137
+ initialDrafts(form.fields, prefill),
138
+ );
139
+ const [errors, setErrors] = useState<Record<string, string>>({});
140
+ const [bannerError, setBannerError] = useState<string | null>(null);
141
+ const [submitting, setSubmitting] = useState(false);
142
+ const [retry, setRetry] = useState<{ message: string } | null>(null);
143
+
144
+ const setDraft = useCallback((fieldKey: string, value: FieldDraft) => {
145
+ setDrafts((prev) => ({ ...prev, [fieldKey]: value }));
146
+ setErrors((prev) => {
147
+ if (!(fieldKey in prev)) return prev;
148
+ const next = { ...prev };
149
+ delete next[fieldKey];
150
+ return next;
151
+ });
152
+ }, []);
153
+
154
+ const handleSubmit = useCallback(async () => {
155
+ setBannerError(null);
156
+ const found = validateForm(form.fields, drafts, t.custom_form);
157
+ setErrors(found);
158
+ if (Object.keys(found).length > 0) return;
159
+
160
+ const payload: CustomFormSubmitPayload = {
161
+ responses: buildResponses(form.fields, drafts),
162
+ };
163
+ setSubmitting(true);
164
+ try {
165
+ await api.post(
166
+ `/sessions/${sessionId}/custom-form/${encodeURIComponent(stepKey)}`,
167
+ payload,
168
+ );
169
+ setSubmitting(false);
170
+ onComplete();
171
+ } catch (err) {
172
+ setSubmitting(false);
173
+ const outcome = classifySubmissionError(err);
174
+ if (outcome.kind === 'aborted') return;
175
+ if (outcome.kind === 'stale') {
176
+ log.warn('[CustomForm] Submit refused for a step the session left; re-routing', {
177
+ stepKey,
178
+ code: outcome.code,
179
+ });
180
+ onComplete();
181
+ return;
182
+ }
183
+ if (outcome.kind === 'invalid') {
184
+ const { byField, other } = splitCustomFormErrors(form.fields, outcome.errors);
185
+ setErrors(byField);
186
+ if (other.length > 0) setBannerError(other.join('\n'));
187
+ return;
188
+ }
189
+ const sdkErr = toSubmissionError(err, t);
190
+ setBannerError(sdkErr.message);
191
+ setRetry({ message: sdkErr.message });
192
+ }
193
+ }, [api, drafts, form.fields, onComplete, sessionId, stepKey, t]);
194
+
195
+ if (retry) {
196
+ return (
197
+ <RetryDialog
198
+ title={t.common.submissionTitle}
199
+ message={retry.message || t.common.submissionFallback}
200
+ retryLabel={t.common.retry}
201
+ cancelLabel={t.common.cancel}
202
+ onRetry={() => setRetry(null)}
203
+ onCancel={() => {
204
+ setRetry(null);
205
+ onCancel({ code: SdkErrorCode.API_ERROR, message: retry.message });
206
+ }}
207
+ />
208
+ );
209
+ }
210
+
211
+ return (
212
+ <View style={styles.fill}>
213
+ <StepShell
214
+ title={form.title ?? t.custom_form.defaultTitle}
215
+ subtitle={form.subtitle}
216
+ onBack={onBack}
217
+ backLabel={t.common.back}
218
+ backDisabled={submitting}
219
+ >
220
+ {form.fields.map((field) => (
221
+ <CustomFormFieldView
222
+ key={field.fieldKey}
223
+ field={field}
224
+ draft={drafts[field.fieldKey] ?? null}
225
+ errorMessage={errors[field.fieldKey] ?? null}
226
+ disabled={submitting}
227
+ locale={locale}
228
+ onChange={(value) => setDraft(field.fieldKey, value)}
229
+ />
230
+ ))}
231
+ {bannerError ? <Banner message={bannerError} /> : null}
232
+ <Button
233
+ label={t.common.continue}
234
+ onPress={handleSubmit}
235
+ loading={submitting}
236
+ disabled={submitting}
237
+ />
238
+ </StepShell>
239
+ {submitting ? <LoadingOverlay message={t.common.submitting} /> : null}
240
+ </View>
241
+ );
242
+ }
243
+
244
+ interface FieldViewProps {
245
+ field: CustomFormFieldDef;
246
+ draft: FieldDraft;
247
+ errorMessage: string | null;
248
+ disabled: boolean;
249
+ locale: string;
250
+ onChange: (value: FieldDraft) => void;
251
+ }
252
+
253
+ function CustomFormFieldView({
254
+ field,
255
+ draft,
256
+ errorMessage,
257
+ disabled,
258
+ locale,
259
+ onChange,
260
+ }: FieldViewProps) {
261
+ const t = getStrings(locale);
262
+ const theme = useTheme();
263
+ const help = field.description ? (
264
+ <Text style={[styles.help, { color: theme.textSecondaryColor }]}>
265
+ {field.description}
266
+ </Text>
267
+ ) : null;
268
+
269
+ switch (field.type) {
270
+ case 'TEXT':
271
+ case 'NUMBER':
272
+ return (
273
+ <View>
274
+ <Input
275
+ label={field.name}
276
+ required={field.required}
277
+ value={typeof draft === 'string' ? draft : ''}
278
+ onChangeText={(text) => onChange(text)}
279
+ editable={!disabled}
280
+ errorMessage={errorMessage}
281
+ {...(field.type === 'NUMBER' && { keyboardType: 'decimal-pad' as const })}
282
+ />
283
+ {help}
284
+ </View>
285
+ );
286
+ case 'DATE': {
287
+ const currentYear = new Date().getFullYear();
288
+ return (
289
+ <View>
290
+ <DatePicker
291
+ label={field.name}
292
+ required={field.required}
293
+ value={typeof draft === 'string' ? draft : null}
294
+ onChange={(value) => onChange(value)}
295
+ errorMessage={errorMessage}
296
+ monthLabels={t.months}
297
+ minYear={1900}
298
+ maxYear={currentYear + DATE_MAX_YEARS_AHEAD}
299
+ />
300
+ {help}
301
+ </View>
302
+ );
303
+ }
304
+ case 'LIST':
305
+ return (
306
+ <View>
307
+ <Select
308
+ label={field.name}
309
+ required={field.required}
310
+ options={field.options.map((option) => ({ value: option, label: option }))}
311
+ value={typeof draft === 'string' ? draft : null}
312
+ onChange={(value) => onChange(value)}
313
+ placeholder={t.custom_form.selectPlaceholder}
314
+ errorMessage={errorMessage}
315
+ disabled={disabled}
316
+ searchable={field.options.length > SEARCHABLE_LIST_THRESHOLD}
317
+ searchPlaceholder={t.custom_form.searchPlaceholder}
318
+ emptyText={t.custom_form.empty}
319
+ />
320
+ {help}
321
+ </View>
322
+ );
323
+ case 'BOOLEAN':
324
+ return (
325
+ <View style={styles.booleanWrap} accessibilityRole="radiogroup" accessibilityLabel={field.name}>
326
+ <Text style={[styles.label, { color: theme.textColor }]}>
327
+ {field.name}
328
+ {field.required ? (
329
+ <Text style={[styles.required, { color: theme.textSecondaryColor }]}>
330
+ {' '}*
331
+ </Text>
332
+ ) : null}
333
+ </Text>
334
+ {help}
335
+ <View style={styles.booleanRow}>
336
+ <RadioCard
337
+ title={t.custom_form.yes}
338
+ selected={draft === true}
339
+ onPress={() => onChange(true)}
340
+ disabled={disabled}
341
+ invalid={errorMessage !== null}
342
+ style={styles.booleanOption}
343
+ />
344
+ <RadioCard
345
+ title={t.custom_form.no}
346
+ selected={draft === false}
347
+ onPress={() => onChange(false)}
348
+ disabled={disabled}
349
+ invalid={errorMessage !== null}
350
+ style={styles.booleanOption}
351
+ />
352
+ </View>
353
+ {errorMessage ? (
354
+ <Text
355
+ accessibilityLiveRegion="polite"
356
+ style={[styles.error, { color: theme.errorColor }]}
357
+ >
358
+ {errorMessage}
359
+ </Text>
360
+ ) : null}
361
+ </View>
362
+ );
363
+ }
364
+ }
365
+
366
+ const styles = StyleSheet.create({
367
+ fill: {
368
+ flex: 1,
369
+ },
370
+ help: {
371
+ fontSize: 13,
372
+ lineHeight: 18,
373
+ marginTop: 2,
374
+ marginBottom: 4,
375
+ },
376
+ label: {
377
+ fontSize: 14,
378
+ fontWeight: '500',
379
+ marginBottom: 4,
380
+ },
381
+ required: {
382
+ fontWeight: '400',
383
+ },
384
+ booleanWrap: {
385
+ width: '100%',
386
+ marginBottom: 4,
387
+ },
388
+ booleanRow: {
389
+ flexDirection: 'row',
390
+ gap: 10,
391
+ },
392
+ booleanOption: {
393
+ flex: 1,
394
+ },
395
+ error: {
396
+ fontSize: 13,
397
+ marginTop: 4,
398
+ lineHeight: 18,
399
+ },
400
+ });
@@ -0,0 +1,207 @@
1
+ /**
2
+ * ENTITY_TYPE_SELECTION — POST /sessions/:id/entity-type { selected_type }.
3
+ *
4
+ * The person chooses whether they complete the onboarding as a natural person
5
+ * (`HUMAN`) or on behalf of a legal entity (`LEGAL`); the backend routes to the
6
+ * matching branch of the workflow. The step has no UI config in v1: two fixed
7
+ * options with the SDK's own copy (`entity_type` in the strings table).
8
+ *
9
+ * The POST answers 202 and the first step of the chosen branch arrives over
10
+ * SSE. A 400 `errors.selected_type` is shown in the banner; the other failures
11
+ * follow `submissionOutcome.ts`. Prefill: `result.selected_type` (reverse
12
+ * navigation), so going back shows the previous choice selected.
13
+ */
14
+
15
+ import { useCallback, useState } from 'react';
16
+ import { StyleSheet, View } from 'react-native';
17
+ import {
18
+ SdkErrorCode,
19
+ type EntityType,
20
+ type EntityTypeSelectionPayload,
21
+ type SdkError,
22
+ } from '../types';
23
+ import { Banner } from '../ui/primitives/Banner';
24
+ import { Button } from '../ui/primitives/Button';
25
+ import { RadioCard } from '../ui/primitives/RadioCard';
26
+ import { LoadingOverlay } from '../ui/overlays/LoadingOverlay';
27
+ import { RetryDialog } from '../ui/overlays/RetryDialog';
28
+ import { StepShell } from '../ui/StepShell';
29
+ import { StepIcon } from '../ui/icons/StepIcon';
30
+ import { getStrings } from '../i18n';
31
+ import { log } from '../utils/log';
32
+ import type { ApiCaller } from '../transport/api';
33
+ import type { StepDeps, StepModule } from './types';
34
+ import { toSubmissionError } from './_shared';
35
+ import { ENTITY_TYPES, prefillEntityType } from './entityType';
36
+ import { classifySubmissionError } from './submissionOutcome';
37
+
38
+ export class EntityTypeSelectionStep implements StepModule {
39
+ private resolveStep!: () => void;
40
+ private rejectStep!: (err: SdkError) => void;
41
+ private destroyed = false;
42
+
43
+ constructor(private readonly deps: StepDeps) {}
44
+
45
+ run(): Promise<void> {
46
+ return new Promise((resolve, reject) => {
47
+ this.resolveStep = resolve;
48
+ this.rejectStep = reject;
49
+ this.deps.ui.setNode(
50
+ <EntityTypeSelectionView
51
+ api={this.deps.api}
52
+ sessionId={this.deps.sessionId}
53
+ locale={this.deps.locale}
54
+ prefill={this.deps.stepResult}
55
+ onBack={this.deps.onBack}
56
+ onComplete={() => {
57
+ if (this.destroyed) return;
58
+ this.deps.ui.setNode(null);
59
+ this.resolveStep();
60
+ }}
61
+ onCancel={(err) => {
62
+ if (this.destroyed) return;
63
+ this.deps.ui.setNode(null);
64
+ this.rejectStep(err);
65
+ }}
66
+ />,
67
+ );
68
+ });
69
+ }
70
+
71
+ destroy(): void {
72
+ this.destroyed = true;
73
+ this.deps.ui.setNode(null);
74
+ }
75
+ }
76
+
77
+ interface ViewProps {
78
+ api: ApiCaller;
79
+ sessionId: string;
80
+ locale: string;
81
+ prefill?: Record<string, unknown> | undefined;
82
+ onBack?: (() => void) | undefined;
83
+ onComplete: () => void;
84
+ onCancel: (err: SdkError) => void;
85
+ }
86
+
87
+ function EntityTypeSelectionView({
88
+ api,
89
+ sessionId,
90
+ locale,
91
+ prefill,
92
+ onBack,
93
+ onComplete,
94
+ onCancel,
95
+ }: ViewProps) {
96
+ const t = getStrings(locale);
97
+ const [selected, setSelected] = useState<EntityType | null>(() =>
98
+ prefillEntityType(prefill),
99
+ );
100
+ const [bannerError, setBannerError] = useState<string | null>(null);
101
+ const [submitting, setSubmitting] = useState(false);
102
+ const [retry, setRetry] = useState<{ message: string } | null>(null);
103
+
104
+ const labels: Record<EntityType, { title: string; description: string }> = {
105
+ HUMAN: { title: t.entity_type.human, description: t.entity_type.humanDescription },
106
+ LEGAL: { title: t.entity_type.legal, description: t.entity_type.legalDescription },
107
+ };
108
+
109
+ const handleSubmit = useCallback(async () => {
110
+ if (!selected) {
111
+ setBannerError(t.entity_type.required);
112
+ return;
113
+ }
114
+ setBannerError(null);
115
+ const payload: EntityTypeSelectionPayload = { selected_type: selected };
116
+ setSubmitting(true);
117
+ try {
118
+ await api.post(`/sessions/${sessionId}/entity-type`, payload);
119
+ setSubmitting(false);
120
+ onComplete();
121
+ } catch (err) {
122
+ setSubmitting(false);
123
+ const outcome = classifySubmissionError(err);
124
+ if (outcome.kind === 'aborted') return;
125
+ if (outcome.kind === 'stale') {
126
+ log.warn('[EntityType] Submit refused for a step the session left; re-routing', {
127
+ code: outcome.code,
128
+ });
129
+ onComplete();
130
+ return;
131
+ }
132
+ if (outcome.kind === 'invalid') {
133
+ setBannerError(
134
+ outcome.errors.selected_type ?? Object.values(outcome.errors).join('\n'),
135
+ );
136
+ return;
137
+ }
138
+ const sdkErr = toSubmissionError(err, t);
139
+ setBannerError(sdkErr.message);
140
+ setRetry({ message: sdkErr.message });
141
+ }
142
+ }, [api, onComplete, selected, sessionId, t]);
143
+
144
+ if (retry) {
145
+ return (
146
+ <RetryDialog
147
+ title={t.common.submissionTitle}
148
+ message={retry.message || t.common.submissionFallback}
149
+ retryLabel={t.common.retry}
150
+ cancelLabel={t.common.cancel}
151
+ onRetry={() => setRetry(null)}
152
+ onCancel={() => {
153
+ setRetry(null);
154
+ onCancel({ code: SdkErrorCode.API_ERROR, message: retry.message });
155
+ }}
156
+ />
157
+ );
158
+ }
159
+
160
+ return (
161
+ <View style={styles.fill}>
162
+ <StepShell
163
+ title={t.entity_type.title}
164
+ subtitle={t.entity_type.subtitle}
165
+ iconNode={<StepIcon name="person" />}
166
+ onBack={onBack}
167
+ backLabel={t.common.back}
168
+ backDisabled={submitting}
169
+ >
170
+ <View accessibilityRole="radiogroup" style={styles.options}>
171
+ {ENTITY_TYPES.map((type) => (
172
+ <RadioCard
173
+ key={type}
174
+ title={labels[type].title}
175
+ description={labels[type].description}
176
+ selected={selected === type}
177
+ onPress={() => {
178
+ setSelected(type);
179
+ setBannerError(null);
180
+ }}
181
+ disabled={submitting}
182
+ testID={`entity-type-${type.toLowerCase()}`}
183
+ />
184
+ ))}
185
+ </View>
186
+ {bannerError ? <Banner message={bannerError} /> : null}
187
+ <Button
188
+ label={t.common.continue}
189
+ onPress={handleSubmit}
190
+ loading={submitting}
191
+ disabled={submitting || selected === null}
192
+ />
193
+ </StepShell>
194
+ {submitting ? <LoadingOverlay message={t.common.submitting} /> : null}
195
+ </View>
196
+ );
197
+ }
198
+
199
+ const styles = StyleSheet.create({
200
+ fill: {
201
+ flex: 1,
202
+ },
203
+ options: {
204
+ width: '100%',
205
+ gap: 10,
206
+ },
207
+ });