|
1 |
| -import React, {useCallback, useMemo, useState} from 'react'; |
| 1 | +import React, {useCallback, useMemo, useRef} from 'react'; |
2 | 2 | import {View} from 'react-native';
|
3 |
| -import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton'; |
4 |
| -import SelectionList from '@components/SelectionList'; |
5 |
| -import RadioListItem from '@components/SelectionList/RadioListItem'; |
| 3 | +import FormProvider from '@components/Form/FormProvider'; |
| 4 | +import InputWrapper from '@components/Form/InputWrapper'; |
| 5 | +import type {FormInputErrors, FormOnyxValues, FormRef} from '@components/Form/types'; |
| 6 | +import ValuePicker from '@components/ValuePicker'; |
| 7 | +import useInternationalBankAccountFormSubmit from '@hooks/useInternationalBankAccountFormSubmit'; |
6 | 8 | import useLocalize from '@hooks/useLocalize';
|
7 | 9 | import useThemeStyles from '@hooks/useThemeStyles';
|
8 |
| -import type {Option} from '@libs/searchOptions'; |
| 10 | +import {setDraftValues} from '@libs/actions/FormActions'; |
9 | 11 | import type {CustomSubStepProps} from '@pages/settings/Wallet/InternationalDepositAccount/types';
|
10 |
| -import {setDraftValues} from '@userActions/FormActions'; |
11 | 12 | import Text from '@src/components/Text';
|
12 | 13 | import CONST from '@src/CONST';
|
13 |
| -import type {TranslationPaths} from '@src/languages/types'; |
14 | 14 | import ONYXKEYS from '@src/ONYXKEYS';
|
15 | 15 |
|
16 |
| -function AccountType({isEditing, onNext, formValues, fieldsMap}: CustomSubStepProps) { |
| 16 | +function AccountType({isEditing, onNext, fieldsMap}: CustomSubStepProps) { |
17 | 17 | const {translate} = useLocalize();
|
18 | 18 | const styles = useThemeStyles();
|
19 |
| - const [currentAccountType, setCurrentAccountType] = useState(formValues[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY]); |
20 |
| - const [error, setError] = useState<TranslationPaths | undefined>(undefined); |
| 19 | + const formRef = useRef<FormRef>(null); |
21 | 20 |
|
22 | 21 | const fieldData = fieldsMap[CONST.CORPAY_FIELDS.STEPS_NAME.ACCOUNT_TYPE]?.[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY] ?? {};
|
23 | 22 |
|
24 |
| - const onAccountTypeSelected = useCallback(() => { |
25 |
| - setError(undefined); |
26 |
| - if (isEditing && formValues[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY] === currentAccountType) { |
27 |
| - onNext(); |
28 |
| - return; |
29 |
| - } |
30 |
| - if (fieldData.isRequired && !currentAccountType) { |
31 |
| - setError('common.error.pleaseSelectOne'); |
32 |
| - return; |
33 |
| - } |
34 |
| - setDraftValues(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM, {[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY]: currentAccountType}); |
35 |
| - onNext(); |
36 |
| - }, [currentAccountType, fieldData.isRequired, formValues, isEditing, onNext]); |
| 23 | + const handleSubmit = useInternationalBankAccountFormSubmit({ |
| 24 | + fieldIds: Object.keys(fieldsMap[CONST.CORPAY_FIELDS.STEPS_NAME.ACCOUNT_TYPE]), |
| 25 | + onNext, |
| 26 | + shouldSaveDraft: isEditing, |
| 27 | + }); |
37 | 28 |
|
38 |
| - const onSelectionChange = useCallback( |
39 |
| - (country: Option) => { |
40 |
| - if (!isEditing) { |
41 |
| - setDraftValues(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM, {[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY]: country.value}); |
| 29 | + const validate = useCallback( |
| 30 | + (values: FormOnyxValues<typeof ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM> => { |
| 31 | + if (!fieldData.isRequired || values[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY]) { |
| 32 | + return {}; |
42 | 33 | }
|
43 |
| - setCurrentAccountType(country.value); |
| 34 | + return {[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY]: translate('common.error.pleaseSelectOne')}; |
44 | 35 | },
|
45 |
| - [isEditing], |
| 36 | + [fieldData.isRequired, translate], |
46 | 37 | );
|
47 | 38 |
|
48 | 39 | const options = useMemo(
|
49 | 40 | () =>
|
50 | 41 | (fieldData.valueSet ?? []).map((item) => {
|
51 | 42 | return {
|
52 | 43 | value: item.id,
|
53 |
| - keyForList: item.id, |
54 |
| - text: item.text, |
55 |
| - isSelected: currentAccountType === item.id, |
56 |
| - searchValue: item.text, |
| 44 | + label: item.text, |
57 | 45 | };
|
58 | 46 | }),
|
59 |
| - [fieldData.valueSet, currentAccountType], |
| 47 | + [fieldData.valueSet], |
60 | 48 | );
|
61 | 49 |
|
62 |
| - const button = useMemo(() => { |
63 |
| - const buttonText = isEditing ? translate('common.confirm') : translate('common.next'); |
64 |
| - return ( |
65 |
| - <FormAlertWithSubmitButton |
66 |
| - message={error ? translate(error) : ''} |
67 |
| - isAlertVisible={!!error} |
68 |
| - buttonText={buttonText} |
69 |
| - onSubmit={onAccountTypeSelected} |
70 |
| - containerStyles={[styles.flexReset, styles.flexGrow0, styles.flexShrink0, styles.flexBasisAuto]} |
71 |
| - enabledWhenOffline |
72 |
| - /> |
73 |
| - ); |
74 |
| - }, [error, isEditing, onAccountTypeSelected, styles.flexBasisAuto, styles.flexGrow0, styles.flexReset, styles.flexShrink0, translate]); |
75 |
| - |
76 | 50 | return (
|
77 |
| - <> |
| 51 | + <FormProvider |
| 52 | + formID={ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM} |
| 53 | + submitButtonText={translate('common.confirm')} |
| 54 | + onSubmit={handleSubmit} |
| 55 | + validate={validate} |
| 56 | + style={[styles.flexGrow1, styles.mt3]} |
| 57 | + submitButtonStyles={[styles.ph5, styles.mb0]} |
| 58 | + enabledWhenOffline |
| 59 | + ref={formRef} |
| 60 | + isSubmitButtonVisible={!isEditing} |
| 61 | + > |
78 | 62 | <View style={styles.ph5}>
|
79 | 63 | <Text style={[styles.textHeadlineLineHeightXXL, styles.mb6]}>{translate('addPersonalBankAccount.accountTypeStepHeader')}</Text>
|
80 | 64 | </View>
|
81 |
| - <SelectionList |
82 |
| - sections={[{data: options}]} |
83 |
| - onSelectRow={onSelectionChange} |
84 |
| - ListItem={RadioListItem} |
85 |
| - initiallyFocusedOptionKey={currentAccountType} |
86 |
| - footerContent={button} |
87 |
| - shouldSingleExecuteRowSelect |
88 |
| - shouldStopPropagation |
89 |
| - shouldUseDynamicMaxToRenderPerBatch |
90 |
| - shouldUpdateFocusedIndex |
| 65 | + <InputWrapper |
| 66 | + InputComponent={ValuePicker} |
| 67 | + inputID={fieldData.id} |
| 68 | + label={fieldData.label} |
| 69 | + items={options} |
| 70 | + shouldSaveDraft={!isEditing} |
| 71 | + shouldShowModal={false} |
| 72 | + onValueChange={(value) => { |
| 73 | + if (!isEditing) { |
| 74 | + return; |
| 75 | + } |
| 76 | + setDraftValues(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM, {[CONST.CORPAY_FIELDS.ACCOUNT_TYPE_KEY]: value}).then(() => { |
| 77 | + onNext(); |
| 78 | + }); |
| 79 | + }} |
91 | 80 | />
|
92 |
| - </> |
| 81 | + </FormProvider> |
93 | 82 | );
|
94 | 83 | }
|
95 | 84 |
|
|
0 commit comments