@velion-la/onboarding-sdk-react-native 1.3.0-beta.24 → 1.3.0-beta.25

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.
@@ -6,6 +6,8 @@
6
6
  * 1. **Assisted** (when `VelionConfig.googleMapsApiKey` is set, or on iOS
7
7
  * where Apple Maps needs no key): intro → map picker → details (floor /
8
8
  * apartment / references) → submit. Lat/Lng are included in the payload.
9
+ * The details screen shows the resolved address as a summary; "Edit
10
+ * address" opens the manual form's address fields to correct it (OSDKR-43).
9
11
  * 2. **Manual** (default fallback): the structured form — country, state,
10
12
  * city, district, neighborhood, street, number, zip, plus the same
11
13
  * details fields. No lat/lng.
@@ -18,7 +20,14 @@
18
20
  * `StepDeps`; the step itself decides which view to mount.
19
21
  */
20
22
 
21
- import { useCallback, useEffect, useMemo, useState } from 'react';
23
+ import {
24
+ useCallback,
25
+ useEffect,
26
+ useMemo,
27
+ useState,
28
+ type Dispatch,
29
+ type SetStateAction,
30
+ } from 'react';
22
31
  import { Platform, StyleSheet, Text, View } from 'react-native';
23
32
  import {
24
33
  SdkErrorCode,
@@ -34,7 +43,7 @@ import { Select } from '../ui/primitives/Select';
34
43
  import { LoadingOverlay } from '../ui/overlays/LoadingOverlay';
35
44
  import { RetryDialog } from '../ui/overlays/RetryDialog';
36
45
  import { StepShell } from '../ui/StepShell';
37
- import { useFieldChain } from '../ui/forms';
46
+ import { useFieldChain, type FieldChainProps } from '../ui/forms';
38
47
  import { HomeHeartIcon } from '../ui/icons/HomeHeartIcon';
39
48
  import { MapFallbackBoundary } from '../ui/MapFallbackBoundary';
40
49
  import { useTheme } from '../ui/theme';
@@ -50,8 +59,17 @@ import { log } from '../utils/log';
50
59
  import {
51
60
  keepStateIfValid,
52
61
  listStateOptions,
53
- suggestState,
62
+ type StateOption,
54
63
  } from '../utils/subdivisions';
64
+ import {
65
+ addressFormFromParsed,
66
+ addressRequirements,
67
+ validateAddressForm,
68
+ type AddressFieldKey,
69
+ type AddressForm,
70
+ type AddressFormErrors,
71
+ type AddressRequirements,
72
+ } from '../utils/addressForm';
55
73
  import type { StepDeps, StepModule } from './types';
56
74
  import { prefillString, toSubmissionError } from './_shared';
57
75
 
@@ -211,16 +229,14 @@ function AssistedAddressFlow({
211
229
  (ParsedAddressFields & { latitude: number; longitude: number }) | null
212
230
  >(null);
213
231
 
214
- const requiredFields = useMemo(() => {
215
- const f = stepConfig?.fields;
216
- return {
217
- state: f?.state?.required !== false,
218
- city: f?.city?.required !== false,
219
- street: f?.street?.required !== false,
220
- number: f?.number?.required !== false,
221
- postal: f?.postal_code?.required !== false,
222
- };
223
- }, [stepConfig?.fields]);
232
+ const requirements = useMemo(
233
+ () => addressRequirements(stepConfig?.fields),
234
+ [stepConfig?.fields],
235
+ );
236
+ const allowedCodes = useMemo(
237
+ () => allowedCountries(stepConfig?.country_allow),
238
+ [stepConfig?.country_allow],
239
+ );
224
240
 
225
241
  if (phase === 'intro') {
226
242
  return (
@@ -258,7 +274,13 @@ function AssistedAddressFlow({
258
274
  locationDenied={t.address_info.locationDenied}
259
275
  // The state is picked from the country's subdivisions in the next
260
276
  // phase, so a geocoder result without a region must not block here.
261
- requiredFields={{ ...requiredFields, state: false }}
277
+ requiredFields={{
278
+ city: requirements.city,
279
+ street: requirements.street,
280
+ number: requirements.number,
281
+ postal: requirements.zip,
282
+ state: false,
283
+ }}
262
284
  countryAllow={stepConfig?.country_allow}
263
285
  onConfirm={(p) => {
264
286
  setParsed(p);
@@ -280,7 +302,8 @@ function AssistedAddressFlow({
280
302
  sessionId={sessionId}
281
303
  locale={locale}
282
304
  parsed={parsed}
283
- stateRequired={requiredFields.state}
305
+ requirements={requirements}
306
+ allowedCodes={allowedCodes}
284
307
  addressType={stepConfig?.address_type?.id ?? ADDRESS_TYPE_HOME}
285
308
  prefill={prefill}
286
309
  onComplete={onComplete}
@@ -295,7 +318,8 @@ interface DetailsFormProps {
295
318
  sessionId: string;
296
319
  locale: string;
297
320
  parsed: ParsedAddressFields & { latitude: number; longitude: number };
298
- stateRequired: boolean;
321
+ requirements: AddressRequirements;
322
+ allowedCodes: string[] | undefined;
299
323
  addressType: string;
300
324
  prefill?: Record<string, unknown> | undefined;
301
325
  onComplete: () => void;
@@ -306,12 +330,20 @@ interface DetailsFormProps {
306
330
  /** Visual order of the assisted flow's detail inputs, for "next" chaining. */
307
331
  const DETAILS_FIELD_ORDER = ['floor', 'apartment', 'details'] as const;
308
332
 
333
+ /**
334
+ * Details screen of the assisted flow: floor, apartment and references, with
335
+ * the address the map resolved as a summary. "Edit address" opens the manual
336
+ * form's address fields, prefilled, to fix what the geocoder read (OSDKR-43).
337
+ * The state picker lives in that section, unless the state could not be
338
+ * resolved: then it is shown right away, since the user has to pick it.
339
+ */
309
340
  function AddressDetailsForm({
310
341
  api,
311
342
  sessionId,
312
343
  locale,
313
344
  parsed,
314
- stateRequired,
345
+ requirements,
346
+ allowedCodes,
315
347
  addressType,
316
348
  prefill,
317
349
  onComplete,
@@ -320,49 +352,47 @@ function AddressDetailsForm({
320
352
  }: DetailsFormProps) {
321
353
  const t = getStrings(locale);
322
354
  const theme = useTheme();
323
- const { field } = useFieldChain(DETAILS_FIELD_ORDER);
324
- const stateOptions = useMemo(
325
- () => listStateOptions(parsed.country, locale),
326
- [parsed.country, locale],
355
+ const [form, setForm] = useState<AddressForm>(() =>
356
+ addressFormFromParsed(parsed, prefill, prefillString),
327
357
  );
328
- // The pin the user just confirmed wins over a state saved by a previous visit.
329
- const [state, setState] = useState(
330
- () =>
331
- suggestState(parsed.country, parsed.state) ||
332
- keepStateIfValid(parsed.country, prefillString(prefill, 'state')),
358
+ // Decided once, from what the map resolved: picking a state afterwards must
359
+ // not hide the picker the user is using.
360
+ const [stateResolved] = useState(() => form.state !== '');
361
+ const [editing, setEditing] = useState(false);
362
+ const { field } = useFieldChain<(typeof MANUAL_FIELD_ORDER)[number]>(
363
+ editing ? MANUAL_FIELD_ORDER : DETAILS_FIELD_ORDER,
333
364
  );
334
- const [stateError, setStateError] = useState<string | null>(null);
335
- const [floor, setFloor] = useState(() => prefillString(prefill, 'floor'));
336
- const [apartment, setApartment] = useState(() =>
337
- prefillString(prefill, 'apartment'),
338
- );
339
- const [details, setDetails] = useState(() => prefillString(prefill, 'details'));
365
+ const [errors, setErrors] = useState<AddressFormErrors>({});
340
366
  const [bannerError, setBannerError] = useState<string | null>(null);
341
367
  const [submitting, setSubmitting] = useState(false);
342
368
  const [retry, setRetry] = useState<{ message: string } | null>(null);
343
369
 
370
+ const stateOptions = useMemo(
371
+ () => listStateOptions(form.country, locale),
372
+ [form.country, locale],
373
+ );
374
+ const stateLabel = stateOptions.find((o) => o.value === form.state)?.label;
375
+ const setField = useFieldSetter(setForm, setErrors);
376
+
344
377
  const handleSubmit = useCallback(async () => {
345
378
  setBannerError(null);
346
- if (stateRequired && stateOptions.length > 0 && !state) {
347
- setStateError(t.address_info.requiredState);
379
+ const next = validateAddressForm(
380
+ form,
381
+ requirements,
382
+ stateOptions.length > 0,
383
+ t.address_info,
384
+ );
385
+ setErrors(next);
386
+ if (Object.keys(next).length > 0) {
387
+ // An error outside the state picker is on a field only the edit section shows.
388
+ if (Object.keys(next).some((key) => key !== 'state')) setEditing(true);
348
389
  setBannerError(t.address_info.fillAllRequired);
349
390
  return;
350
391
  }
351
392
  const payload: AddressInfoPayload = {
352
- type: addressType,
353
- country: parsed.country,
354
- state,
355
- city: parsed.city,
356
- street: parsed.street,
357
- number: parsed.number,
358
- zip_code: parsed.zip_code,
393
+ ...addressPayload(form, addressType),
359
394
  latitude: parsed.latitude,
360
395
  longitude: parsed.longitude,
361
- ...(parsed.district && { district: parsed.district }),
362
- ...(parsed.neighborhood && { neighborhood: parsed.neighborhood }),
363
- ...(floor.trim() && { floor: floor.trim() }),
364
- ...(apartment.trim() && { apartment: apartment.trim() }),
365
- ...(details.trim() && { details: details.trim() }),
366
396
  };
367
397
  setSubmitting(true);
368
398
  try {
@@ -378,15 +408,13 @@ function AddressDetailsForm({
378
408
  }, [
379
409
  addressType,
380
410
  api,
381
- apartment,
382
- details,
383
- floor,
411
+ form,
384
412
  onComplete,
385
- parsed,
413
+ parsed.latitude,
414
+ parsed.longitude,
415
+ requirements,
386
416
  sessionId,
387
- state,
388
417
  stateOptions.length,
389
- stateRequired,
390
418
  t,
391
419
  ]);
392
420
 
@@ -406,59 +434,51 @@ function AddressDetailsForm({
406
434
  );
407
435
  }
408
436
 
437
+ const streetLine = [form.street, form.number].filter(Boolean).join(' ');
438
+ const placeLine = [form.city, stateLabel].filter(Boolean).join(', ');
439
+
409
440
  return (
410
441
  <View style={{ flex: 1 }}>
411
442
  <StepShell
412
443
  title={t.address_info.detailsTitle}
413
- subtitle={parsed.street ? `${parsed.street} ${parsed.number}` : undefined}
444
+ subtitle={streetLine || undefined}
414
445
  iconNode={<HomeHeartIcon />}
415
446
  >
416
- <Text style={[styles.summary, { color: theme.textSecondaryColor }]}>
417
- {parsed.city} ({parsed.country})
418
- </Text>
419
- {stateOptions.length > 0 ? (
420
- <Select
421
- label={t.address_info.state}
422
- required={stateRequired}
423
- options={stateOptions}
424
- value={state || null}
425
- onChange={(v) => {
426
- setState(v);
427
- setStateError(null);
428
- }}
429
- placeholder={t.address_info.statePlaceholder}
430
- errorMessage={stateError}
447
+ {editing ? (
448
+ <AddressFields
449
+ locale={locale}
450
+ form={form}
451
+ errors={errors}
452
+ requirements={requirements}
453
+ stateOptions={stateOptions}
454
+ allowedCodes={allowedCodes}
455
+ setField={setField}
456
+ field={field}
431
457
  />
432
- ) : null}
433
- <View style={styles.row}>
434
- <View style={styles.col}>
435
- <Input
436
- label={t.address_info.floor}
437
- autoCorrect={false}
438
- value={floor}
439
- onChangeText={setFloor}
440
- {...field('floor')}
441
- />
442
- </View>
443
- <View style={styles.col}>
444
- <Input
445
- label={t.address_info.apartment}
446
- autoCorrect={false}
447
- value={apartment}
448
- onChangeText={setApartment}
449
- {...field('apartment')}
458
+ ) : (
459
+ <>
460
+ <Text style={[styles.summary, { color: theme.textSecondaryColor }]}>
461
+ {placeLine ? `${placeLine} (${form.country})` : form.country}
462
+ </Text>
463
+ <Button
464
+ variant="ghost"
465
+ label={t.address_info.editAddress}
466
+ onPress={() => setEditing(true)}
467
+ disabled={submitting}
450
468
  />
451
- </View>
452
- </View>
453
- <Input
454
- label={t.address_info.references}
455
- autoCorrect
456
- multiline
457
- numberOfLines={3}
458
- value={details}
459
- onChangeText={setDetails}
460
- {...field('details')}
461
- />
469
+ {!stateResolved && stateOptions.length > 0 ? (
470
+ <StateSelect
471
+ locale={locale}
472
+ form={form}
473
+ errors={errors}
474
+ required={requirements.state}
475
+ stateOptions={stateOptions}
476
+ setField={setField}
477
+ />
478
+ ) : null}
479
+ </>
480
+ )}
481
+ <DetailsFields form={form} setField={setField} field={field} locale={locale} />
462
482
  {bannerError ? <Banner message={bannerError} /> : null}
463
483
  <View style={styles.row}>
464
484
  <Button
@@ -484,24 +504,12 @@ function AddressDetailsForm({
484
504
  }
485
505
 
486
506
  // ─────────────────────────────────────────────────────────────────────────────
487
- // MANUAL FLOW — structured form, kept side-by-side with the assisted flow so
488
- // consumers without a Maps key still get a full onboarding experience.
507
+ // SHARED FIELDS — the address inputs of the manual form, reused by the assisted
508
+ // flow's "edit address" section so both validate and look the same (OSDKR-43).
489
509
  // ─────────────────────────────────────────────────────────────────────────────
490
510
 
491
- type FieldKey =
492
- | 'country'
493
- | 'state'
494
- | 'city'
495
- | 'district'
496
- | 'neighborhood'
497
- | 'street'
498
- | 'number'
499
- | 'zip_code'
500
- | 'floor'
501
- | 'apartment'
502
- | 'details';
503
-
504
- type ManualForm = Record<FieldKey, string>;
511
+ type SetField = (key: AddressFieldKey, value: string) => void;
512
+ type FieldChain = (key: (typeof MANUAL_FIELD_ORDER)[number]) => FieldChainProps;
505
513
 
506
514
  /** Visual order of the manual form's text inputs, for keyboard "next" chaining.
507
515
  * Module-level so the chain callback stays stable across renders. */
@@ -517,6 +525,237 @@ const MANUAL_FIELD_ORDER = [
517
525
  'details',
518
526
  ] as const;
519
527
 
528
+ /** Updates one field and clears its error. */
529
+ function useFieldSetter(
530
+ setForm: Dispatch<SetStateAction<AddressForm>>,
531
+ setErrors: Dispatch<SetStateAction<AddressFormErrors>>,
532
+ ): SetField {
533
+ return useCallback(
534
+ (key: AddressFieldKey, value: string) => {
535
+ setForm((prev) => ({ ...prev, [key]: value }));
536
+ setErrors((prev) => {
537
+ if (!(key in prev)) return prev;
538
+ const next = { ...prev };
539
+ delete next[key];
540
+ return next;
541
+ });
542
+ },
543
+ [setErrors, setForm],
544
+ );
545
+ }
546
+
547
+ /** The fields both flows send, trimmed; optional ones only when filled. */
548
+ function addressPayload(form: AddressForm, addressType: string): AddressInfoPayload {
549
+ return {
550
+ type: addressType,
551
+ country: form.country.toUpperCase(),
552
+ state: form.state,
553
+ city: form.city.trim(),
554
+ street: form.street.trim(),
555
+ number: form.number.trim(),
556
+ zip_code: form.zip_code.trim(),
557
+ ...(form.district.trim() && { district: form.district.trim() }),
558
+ ...(form.neighborhood.trim() && { neighborhood: form.neighborhood.trim() }),
559
+ ...(form.floor.trim() && { floor: form.floor.trim() }),
560
+ ...(form.apartment.trim() && { apartment: form.apartment.trim() }),
561
+ ...(form.details.trim() && { details: form.details.trim() }),
562
+ };
563
+ }
564
+
565
+ interface StateSelectProps {
566
+ locale: string;
567
+ form: AddressForm;
568
+ errors: AddressFormErrors;
569
+ required: boolean;
570
+ stateOptions: StateOption[];
571
+ setField: SetField;
572
+ }
573
+
574
+ function StateSelect({ locale, form, errors, required, stateOptions, setField }: StateSelectProps) {
575
+ const t = getStrings(locale);
576
+ return (
577
+ <Select
578
+ label={t.address_info.state}
579
+ required={required}
580
+ options={stateOptions}
581
+ value={form.state || null}
582
+ onChange={(v) => setField('state', v)}
583
+ placeholder={t.address_info.statePlaceholder}
584
+ errorMessage={errors.state ?? null}
585
+ />
586
+ );
587
+ }
588
+
589
+ interface AddressFieldsProps {
590
+ locale: string;
591
+ form: AddressForm;
592
+ errors: AddressFormErrors;
593
+ requirements: AddressRequirements;
594
+ stateOptions: StateOption[];
595
+ allowedCodes: string[] | undefined;
596
+ setField: SetField;
597
+ field: FieldChain;
598
+ }
599
+
600
+ /** Country, state, city, district, neighborhood, street, number and zip code. */
601
+ function AddressFields({
602
+ locale,
603
+ form,
604
+ errors,
605
+ requirements,
606
+ stateOptions,
607
+ allowedCodes,
608
+ setField,
609
+ field,
610
+ }: AddressFieldsProps) {
611
+ const t = getStrings(locale);
612
+ return (
613
+ <>
614
+ <CountrySelect
615
+ label={t.address_info.country}
616
+ required
617
+ value={form.country || null}
618
+ onChange={(v) => {
619
+ setField('country', v);
620
+ // A state of the previous country would be rejected by the backend.
621
+ if (v !== form.country) setField('state', '');
622
+ }}
623
+ locale={locale}
624
+ allowedCodes={allowedCodes}
625
+ placeholder={t.address_info.countryPlaceholder}
626
+ searchPlaceholder={t.address_info.countryPlaceholder}
627
+ errorMessage={errors.country ?? null}
628
+ />
629
+ {stateOptions.length > 0 ? (
630
+ <StateSelect
631
+ locale={locale}
632
+ form={form}
633
+ errors={errors}
634
+ required={requirements.state}
635
+ stateOptions={stateOptions}
636
+ setField={setField}
637
+ />
638
+ ) : null}
639
+ <Input
640
+ label={t.address_info.city}
641
+ required={requirements.city}
642
+ autoCapitalize="words"
643
+ autoCorrect={false}
644
+ value={form.city}
645
+ onChangeText={(v) => setField('city', v)}
646
+ errorMessage={errors.city ?? null}
647
+ {...field('city')}
648
+ />
649
+ <Input
650
+ label={t.address_info.district}
651
+ required={requirements.district}
652
+ autoCapitalize="words"
653
+ autoCorrect={false}
654
+ value={form.district}
655
+ onChangeText={(v) => setField('district', v)}
656
+ errorMessage={errors.district ?? null}
657
+ {...field('district')}
658
+ />
659
+ <Input
660
+ label={t.address_info.neighborhood}
661
+ required={requirements.neighborhood}
662
+ autoCapitalize="words"
663
+ autoCorrect={false}
664
+ value={form.neighborhood}
665
+ onChangeText={(v) => setField('neighborhood', v)}
666
+ errorMessage={errors.neighborhood ?? null}
667
+ {...field('neighborhood')}
668
+ />
669
+ <Input
670
+ label={t.address_info.street}
671
+ required={requirements.street}
672
+ autoCapitalize="words"
673
+ autoCorrect={false}
674
+ value={form.street}
675
+ onChangeText={(v) => setField('street', v)}
676
+ errorMessage={errors.street ?? null}
677
+ {...field('street')}
678
+ />
679
+ <View style={styles.row}>
680
+ <View style={styles.col}>
681
+ <Input
682
+ label={t.address_info.number}
683
+ required={requirements.number}
684
+ keyboardType="number-pad"
685
+ autoCorrect={false}
686
+ value={form.number}
687
+ onChangeText={(v) => setField('number', v)}
688
+ errorMessage={errors.number ?? null}
689
+ {...field('number')}
690
+ />
691
+ </View>
692
+ <View style={styles.col}>
693
+ <Input
694
+ label={t.address_info.zipCode}
695
+ required={requirements.zip}
696
+ autoCapitalize="characters"
697
+ autoCorrect={false}
698
+ value={form.zip_code}
699
+ onChangeText={(v) => setField('zip_code', v)}
700
+ errorMessage={errors.zip_code ?? null}
701
+ {...field('zip_code')}
702
+ />
703
+ </View>
704
+ </View>
705
+ </>
706
+ );
707
+ }
708
+
709
+ interface DetailsFieldsProps {
710
+ locale: string;
711
+ form: AddressForm;
712
+ setField: SetField;
713
+ field: (key: 'floor' | 'apartment' | 'details') => FieldChainProps;
714
+ }
715
+
716
+ /** Floor, apartment and references: optional in both flows. */
717
+ function DetailsFields({ locale, form, setField, field }: DetailsFieldsProps) {
718
+ const t = getStrings(locale);
719
+ return (
720
+ <>
721
+ <View style={styles.row}>
722
+ <View style={styles.col}>
723
+ <Input
724
+ label={t.address_info.floor}
725
+ autoCorrect={false}
726
+ value={form.floor}
727
+ onChangeText={(v) => setField('floor', v)}
728
+ {...field('floor')}
729
+ />
730
+ </View>
731
+ <View style={styles.col}>
732
+ <Input
733
+ label={t.address_info.apartment}
734
+ autoCorrect={false}
735
+ value={form.apartment}
736
+ onChangeText={(v) => setField('apartment', v)}
737
+ {...field('apartment')}
738
+ />
739
+ </View>
740
+ </View>
741
+ <Input
742
+ label={t.address_info.references}
743
+ autoCorrect
744
+ multiline
745
+ numberOfLines={3}
746
+ value={form.details}
747
+ onChangeText={(v) => setField('details', v)}
748
+ {...field('details')}
749
+ />
750
+ </>
751
+ );
752
+ }
753
+
754
+ // ─────────────────────────────────────────────────────────────────────────────
755
+ // MANUAL FLOW — structured form, kept side-by-side with the assisted flow so
756
+ // consumers without a Maps key still get a full onboarding experience.
757
+ // ─────────────────────────────────────────────────────────────────────────────
758
+
520
759
  function ManualAddressForm({
521
760
  api,
522
761
  sessionId,
@@ -528,36 +767,25 @@ function ManualAddressForm({
528
767
  onCancel,
529
768
  }: ViewProps) {
530
769
  const t = getStrings(locale);
531
- const f = stepConfig?.fields;
532
770
  // "Next" on the keyboard walks down the form (and the scroll follows the
533
771
  // focused input); only the last field closes it, which is what brings the
534
772
  // submit button back into reach (OSDKR-14).
535
773
  const { field } = useFieldChain(MANUAL_FIELD_ORDER);
536
774
 
537
- const allowedCodes = useMemo(() => {
538
- const allow = stepConfig?.country_allow;
539
- if (!Array.isArray(allow) || allow.length === 0) return undefined;
540
- return allow.map((c) => c.toUpperCase());
541
- }, [stepConfig?.country_allow]);
542
-
775
+ const allowedCodes = useMemo(
776
+ () => allowedCountries(stepConfig?.country_allow),
777
+ [stepConfig?.country_allow],
778
+ );
543
779
  const requirements = useMemo(
544
- () => ({
545
- state: f?.state?.required !== false,
546
- city: f?.city?.required !== false,
547
- district: f?.district?.required === true,
548
- neighborhood: f?.neighborhood?.required === true,
549
- street: f?.street?.required !== false,
550
- number: f?.number?.required !== false,
551
- zip: f?.postal_code?.required !== false,
552
- }),
553
- [f],
780
+ () => addressRequirements(stepConfig?.fields),
781
+ [stepConfig?.fields],
554
782
  );
555
783
 
556
- const [form, setForm] = useState<ManualForm>(() => {
784
+ const [form, setForm] = useState<AddressForm>(() => {
557
785
  // Seed from prefill first (reverse navigation re-opened this step), then
558
786
  // fall back to a single allowed country. Prefill takes precedence.
559
787
  const prefillCountry = prefillString(prefill, 'country');
560
- const base: ManualForm = {
788
+ const base: AddressForm = {
561
789
  country: prefillCountry,
562
790
  // Sessions from before OSDKR-38 may carry free text here.
563
791
  state: keepStateIfValid(prefillCountry, prefillString(prefill, 'state')),
@@ -583,7 +811,7 @@ function ManualAddressForm({
583
811
  }
584
812
  }, [allowedCodes, form.country]);
585
813
 
586
- const [errors, setErrors] = useState<Partial<Record<FieldKey, string>>>({});
814
+ const [errors, setErrors] = useState<AddressFormErrors>({});
587
815
  const [bannerError, setBannerError] = useState<string | null>(null);
588
816
  const [submitting, setSubmitting] = useState(false);
589
817
  const [retry, setRetry] = useState<{ message: string } | null>(null);
@@ -592,58 +820,22 @@ function ManualAddressForm({
592
820
  () => listStateOptions(form.country, locale),
593
821
  [form.country, locale],
594
822
  );
595
-
596
- const setField = useCallback((key: FieldKey, value: string) => {
597
- setForm((prev) => ({ ...prev, [key]: value }));
598
- setErrors((prev) => {
599
- if (!(key in prev)) return prev;
600
- const next = { ...prev };
601
- delete next[key];
602
- return next;
603
- });
604
- }, []);
605
-
606
- const validate = useCallback((): boolean => {
607
- const next: Partial<Record<FieldKey, string>> = {};
608
- if (!form.country) next.country = t.address_info.requiredCountry;
609
- if (requirements.state && stateOptions.length > 0 && !form.state)
610
- next.state = t.address_info.requiredState;
611
- if (requirements.city && !form.city.trim())
612
- next.city = t.address_info.requiredCity;
613
- if (requirements.district && !form.district.trim())
614
- next.district = t.address_info.requiredDistrict;
615
- if (requirements.neighborhood && !form.neighborhood.trim())
616
- next.neighborhood = t.address_info.requiredNeighborhood;
617
- if (requirements.street && !form.street.trim())
618
- next.street = t.address_info.requiredStreet;
619
- if (requirements.number && !form.number.trim())
620
- next.number = t.address_info.requiredNumber;
621
- if (requirements.zip && !form.zip_code.trim())
622
- next.zip_code = t.address_info.requiredZip;
623
- setErrors(next);
624
- return Object.keys(next).length === 0;
625
- }, [form, requirements, stateOptions.length, t.address_info]);
823
+ const setField = useFieldSetter(setForm, setErrors);
626
824
 
627
825
  const handleSubmit = useCallback(async () => {
628
826
  setBannerError(null);
629
- if (!validate()) {
827
+ const next = validateAddressForm(
828
+ form,
829
+ requirements,
830
+ stateOptions.length > 0,
831
+ t.address_info,
832
+ );
833
+ setErrors(next);
834
+ if (Object.keys(next).length > 0) {
630
835
  setBannerError(t.address_info.fillAllRequired);
631
836
  return;
632
837
  }
633
- const payload: AddressInfoPayload = {
634
- type: stepConfig?.address_type?.id ?? ADDRESS_TYPE_HOME,
635
- country: form.country.toUpperCase(),
636
- state: form.state,
637
- city: form.city.trim(),
638
- street: form.street.trim(),
639
- number: form.number.trim(),
640
- zip_code: form.zip_code.trim(),
641
- ...(form.district.trim() && { district: form.district.trim() }),
642
- ...(form.neighborhood.trim() && { neighborhood: form.neighborhood.trim() }),
643
- ...(form.floor.trim() && { floor: form.floor.trim() }),
644
- ...(form.apartment.trim() && { apartment: form.apartment.trim() }),
645
- ...(form.details.trim() && { details: form.details.trim() }),
646
- };
838
+ const payload = addressPayload(form, stepConfig?.address_type?.id ?? ADDRESS_TYPE_HOME);
647
839
  setSubmitting(true);
648
840
  try {
649
841
  await api.post(`/sessions/${sessionId}/address-info`, payload);
@@ -655,7 +847,16 @@ function ManualAddressForm({
655
847
  setBannerError(sdkErr.message);
656
848
  setRetry({ message: sdkErr.message });
657
849
  }
658
- }, [api, form, onComplete, sessionId, stepConfig?.address_type?.id, t, validate]);
850
+ }, [
851
+ api,
852
+ form,
853
+ onComplete,
854
+ requirements,
855
+ sessionId,
856
+ stateOptions.length,
857
+ stepConfig?.address_type?.id,
858
+ t,
859
+ ]);
659
860
 
660
861
  if (retry) {
661
862
  return (
@@ -683,127 +884,17 @@ function ManualAddressForm({
683
884
  backLabel={t.common.back}
684
885
  backDisabled={submitting}
685
886
  >
686
- <CountrySelect
687
- label={t.address_info.country}
688
- required
689
- value={form.country || null}
690
- onChange={(v) => {
691
- setField('country', v);
692
- // A state of the previous country would be rejected by the backend.
693
- if (v !== form.country) setField('state', '');
694
- }}
887
+ <AddressFields
695
888
  locale={locale}
889
+ form={form}
890
+ errors={errors}
891
+ requirements={requirements}
892
+ stateOptions={stateOptions}
696
893
  allowedCodes={allowedCodes}
697
- placeholder={t.address_info.countryPlaceholder}
698
- searchPlaceholder={t.address_info.countryPlaceholder}
699
- errorMessage={errors.country ?? null}
700
- />
701
- {stateOptions.length > 0 ? (
702
- <Select
703
- label={t.address_info.state}
704
- required={requirements.state}
705
- options={stateOptions}
706
- value={form.state || null}
707
- onChange={(v) => setField('state', v)}
708
- placeholder={t.address_info.statePlaceholder}
709
- errorMessage={errors.state ?? null}
710
- />
711
- ) : null}
712
- <Input
713
- label={t.address_info.city}
714
- required={requirements.city}
715
- autoCapitalize="words"
716
- autoCorrect={false}
717
- value={form.city}
718
- onChangeText={(v) => setField('city', v)}
719
- errorMessage={errors.city ?? null}
720
- {...field('city')}
721
- />
722
- <Input
723
- label={t.address_info.district}
724
- required={requirements.district}
725
- autoCapitalize="words"
726
- autoCorrect={false}
727
- value={form.district}
728
- onChangeText={(v) => setField('district', v)}
729
- errorMessage={errors.district ?? null}
730
- {...field('district')}
731
- />
732
- <Input
733
- label={t.address_info.neighborhood}
734
- required={requirements.neighborhood}
735
- autoCapitalize="words"
736
- autoCorrect={false}
737
- value={form.neighborhood}
738
- onChangeText={(v) => setField('neighborhood', v)}
739
- errorMessage={errors.neighborhood ?? null}
740
- {...field('neighborhood')}
741
- />
742
- <Input
743
- label={t.address_info.street}
744
- required={requirements.street}
745
- autoCapitalize="words"
746
- autoCorrect={false}
747
- value={form.street}
748
- onChangeText={(v) => setField('street', v)}
749
- errorMessage={errors.street ?? null}
750
- {...field('street')}
751
- />
752
- <View style={styles.row}>
753
- <View style={styles.col}>
754
- <Input
755
- label={t.address_info.number}
756
- required={requirements.number}
757
- keyboardType="number-pad"
758
- autoCorrect={false}
759
- value={form.number}
760
- onChangeText={(v) => setField('number', v)}
761
- errorMessage={errors.number ?? null}
762
- {...field('number')}
763
- />
764
- </View>
765
- <View style={styles.col}>
766
- <Input
767
- label={t.address_info.zipCode}
768
- required={requirements.zip}
769
- autoCapitalize="characters"
770
- autoCorrect={false}
771
- value={form.zip_code}
772
- onChangeText={(v) => setField('zip_code', v)}
773
- errorMessage={errors.zip_code ?? null}
774
- {...field('zip_code')}
775
- />
776
- </View>
777
- </View>
778
- <View style={styles.row}>
779
- <View style={styles.col}>
780
- <Input
781
- label={t.address_info.floor}
782
- autoCorrect={false}
783
- value={form.floor}
784
- onChangeText={(v) => setField('floor', v)}
785
- {...field('floor')}
786
- />
787
- </View>
788
- <View style={styles.col}>
789
- <Input
790
- label={t.address_info.apartment}
791
- autoCorrect={false}
792
- value={form.apartment}
793
- onChangeText={(v) => setField('apartment', v)}
794
- {...field('apartment')}
795
- />
796
- </View>
797
- </View>
798
- <Input
799
- label={t.address_info.references}
800
- autoCorrect
801
- multiline
802
- numberOfLines={3}
803
- value={form.details}
804
- onChangeText={(v) => setField('details', v)}
805
- {...field('details')}
894
+ setField={setField}
895
+ field={field}
806
896
  />
897
+ <DetailsFields form={form} setField={setField} field={field} locale={locale} />
807
898
  {bannerError ? <Banner message={bannerError} /> : null}
808
899
  <Button
809
900
  label={t.common.continue}
@@ -817,6 +908,12 @@ function ManualAddressForm({
817
908
  );
818
909
  }
819
910
 
911
+ /** The `country_allow` of the step config, upper-cased, or `undefined` for every country. */
912
+ function allowedCountries(allow: string[] | undefined): string[] | undefined {
913
+ if (!Array.isArray(allow) || allow.length === 0) return undefined;
914
+ return allow.map((c) => c.toUpperCase());
915
+ }
916
+
820
917
  const styles = StyleSheet.create({
821
918
  row: {
822
919
  flexDirection: 'row',