Skip to content

Commit

Permalink
Merge pull request #34413 from barttom/refactor/remove-getDefaultValu…
Browse files Browse the repository at this point in the history
…eForReimbursementAccountField-usage

Refactor/remove get default value for reimbursement account field usage
  • Loading branch information
mountiny authored Jan 12, 2024
2 parents 2f82ba0 + a61e206 commit 7cee965
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import AddressForm from '@pages/ReimbursementAccount/AddressForm';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import * as BankAccounts from '@userActions/BankAccounts';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -53,10 +52,10 @@ function AddressBusiness({reimbursementAccount, onNext, isEditing}: AddressBusin
const styles = useThemeStyles();

const defaultValues = {
street: getDefaultValueForReimbursementAccountField(reimbursementAccount, companyBusinessInfoKey.STREET, ''),
city: getDefaultValueForReimbursementAccountField(reimbursementAccount, companyBusinessInfoKey.CITY, ''),
state: getDefaultValueForReimbursementAccountField(reimbursementAccount, companyBusinessInfoKey.STATE, ''),
zipCode: getDefaultValueForReimbursementAccountField(reimbursementAccount, companyBusinessInfoKey.ZIP_CODE, ''),
street: reimbursementAccount?.achData?.addressStreet ?? '',
city: reimbursementAccount?.achData?.addressCity ?? '',
state: reimbursementAccount?.achData?.addressState ?? '',
zipCode: reimbursementAccount?.achData?.addressZipCode ?? '',
};

const handleSubmit = (values: BankAccounts.BusinessAddress) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount, ReimbursementAccountDraft} from '@src/types/onyx';
Expand Down Expand Up @@ -44,8 +43,7 @@ function IncorporationDateBusiness({reimbursementAccount, reimbursementAccountDr
const {translate} = useLocalize();
const styles = useThemeStyles();

const defaultCompanyIncorporationDate =
getDefaultValueForReimbursementAccountField(reimbursementAccount, companyIncorporationDateKey, '') || (reimbursementAccountDraft?.[companyIncorporationDateKey] ?? '');
const defaultCompanyIncorporationDate = reimbursementAccount?.achData?.incorporationDate ?? reimbursementAccountDraft?.incorporationDate ?? '';

return (
// @ts-expect-error TODO: Remove this once FormProvider (https://github.com/Expensify/App/issues/31972) is migrated to TypeScript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
Expand All @@ -30,8 +29,7 @@ const validate = (values: FormValues): OnyxCommon.Errors => ValidationUtils.getF
function IncorporationStateBusiness({reimbursementAccount, onNext, isEditing}: IncorporationStateBusinessProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();

const defaultCompanyIncorporationState = getDefaultValueForReimbursementAccountField(reimbursementAccount, companyIncorporationStateKey, '');
const defaultCompanyIncorporationState = reimbursementAccount?.achData?.incorporationState ?? '';

return (
// @ts-expect-error TODO: Remove this once FormProvider (https://github.com/Expensify/App/issues/31972) is migrated to TypeScript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
Expand All @@ -31,9 +30,8 @@ function NameBusiness({reimbursementAccount, onNext, isEditing}: NameBusinessPro
const {translate} = useLocalize();
const styles = useThemeStyles();

const defaultCompanyName = getDefaultValueForReimbursementAccountField(reimbursementAccount, companyNameKey, '');

const bankAccountID = getDefaultValueForReimbursementAccountField(reimbursementAccount, 'bankAccountID', 0);
const defaultCompanyName = reimbursementAccount?.achData?.companyName ?? '';
const bankAccountID = reimbursementAccount?.achData?.bankAccountID ?? 0;

const shouldDisableCompanyName = Boolean(bankAccountID && defaultCompanyName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
Expand Down Expand Up @@ -38,8 +37,7 @@ const validate = (values: FormValues): OnyxCommon.Errors => {
function PhoneNumberBusiness({reimbursementAccount, onNext, isEditing}: PhoneNumberBusinessProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();

const defaultCompanyPhoneNumber = getDefaultValueForReimbursementAccountField(reimbursementAccount, companyPhoneNumberKey, '');
const defaultCompanyPhoneNumber = reimbursementAccount?.achData?.companyPhone ?? '';

return (
// @ts-expect-error TODO: Remove this once FormProvider (https://github.com/Expensify/App/issues/31972) is migrated to TypeScript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
Expand Down Expand Up @@ -38,10 +37,8 @@ const validate = (values: FormValues): OnyxCommon.Errors => {
function TaxIdBusiness({reimbursementAccount, onNext, isEditing}: TaxIdBusinessProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();

const defaultCompanyTaxId = getDefaultValueForReimbursementAccountField(reimbursementAccount, companyTaxIdKey, '');

const bankAccountID = getDefaultValueForReimbursementAccountField(reimbursementAccount, 'bankAccountID', 0);
const defaultCompanyTaxId = reimbursementAccount?.achData?.companyTaxID ?? '';
const bankAccountID = reimbursementAccount?.achData?.bankAccountID ?? 0;

const shouldDisableCompanyTaxID = Boolean(bankAccountID && defaultCompanyTaxId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
Expand All @@ -32,8 +31,7 @@ const validate = (values: FormValues): OnyxCommon.Errors => ValidationUtils.getF
function TypeBusiness({reimbursementAccount, onNext, isEditing}: TypeBusinessProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();

const defaultIncorporationType = getDefaultValueForReimbursementAccountField(reimbursementAccount, companyIncorporationTypeKey, '');
const defaultIncorporationType = reimbursementAccount?.achData?.incorporationType ?? '';

return (
// @ts-expect-error TODO: Remove this once FormProvider (https://github.com/Expensify/App/issues/31972) is migrated to TypeScript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import * as BankAccounts from '@userActions/BankAccounts';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -51,8 +50,7 @@ function WebsiteBusiness({reimbursementAccount, user, session, onNext, isEditing
() => (user?.isFromPublicDomain ? 'https://' : `https://www.${Str.extractEmailDomain(session?.email ?? '')}`),
[session?.email, user?.isFromPublicDomain],
);

const defaultCompanyWebsite = getDefaultValueForReimbursementAccountField(reimbursementAccount, companyWebsiteKey, defaultWebsiteExample);
const defaultCompanyWebsite = reimbursementAccount?.achData?.website ?? defaultWebsiteExample;

useEffect(() => {
BankAccounts.addBusinessWebsiteForDraft(defaultCompanyWebsite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import useLocalize from '@hooks/useLocalize';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ValidationUtils from '@libs/ValidationUtils';
import getDefaultValueForReimbursementAccountField from '@pages/ReimbursementAccount/utils/getDefaultValueForReimbursementAccountField';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
Expand Down Expand Up @@ -53,17 +52,9 @@ function ConfirmAgreements({onNext, reimbursementAccount}: ConfirmAgreementsProp
const {translate} = useLocalize();
const styles = useThemeStyles();
const defaultValues = {
[COMPLETE_VERIFICATION_KEYS.IS_AUTHORIZED_TO_USE_BANK_ACCOUNT]: getDefaultValueForReimbursementAccountField(
reimbursementAccount,
COMPLETE_VERIFICATION_KEYS.IS_AUTHORIZED_TO_USE_BANK_ACCOUNT,
false,
),
[COMPLETE_VERIFICATION_KEYS.CERTIFY_TRUE_INFORMATION]: getDefaultValueForReimbursementAccountField(reimbursementAccount, COMPLETE_VERIFICATION_KEYS.CERTIFY_TRUE_INFORMATION, false),
[COMPLETE_VERIFICATION_KEYS.ACCEPT_TERMS_AND_CONDITIONS]: getDefaultValueForReimbursementAccountField(
reimbursementAccount,
COMPLETE_VERIFICATION_KEYS.ACCEPT_TERMS_AND_CONDITIONS,
false,
),
isAuthorizedToUseBankAccount: reimbursementAccount?.achData?.isAuthorizedToUseBankAccount ?? false,
certifyTrueInformation: reimbursementAccount?.achData?.certifyTrueInformation ?? false,
acceptTermsAndConditions: reimbursementAccount?.achData?.acceptTermsAndConditions ?? false,
};

return (
Expand All @@ -87,7 +78,7 @@ function ConfirmAgreements({onNext, reimbursementAccount}: ConfirmAgreementsProp
inputID={COMPLETE_VERIFICATION_KEYS.IS_AUTHORIZED_TO_USE_BANK_ACCOUNT}
style={styles.mt4}
LabelComponent={() => <Text>{translate('completeVerificationStep.isAuthorizedToUseBankAccount')}</Text>}
defaultValue={defaultValues[COMPLETE_VERIFICATION_KEYS.IS_AUTHORIZED_TO_USE_BANK_ACCOUNT]}
defaultValue={defaultValues.isAuthorizedToUseBankAccount}
shouldSaveDraft
/>
<InputWrapper
Expand All @@ -97,7 +88,7 @@ function ConfirmAgreements({onNext, reimbursementAccount}: ConfirmAgreementsProp
inputID={COMPLETE_VERIFICATION_KEYS.CERTIFY_TRUE_INFORMATION}
style={styles.mt4}
LabelComponent={() => <Text>{translate('completeVerificationStep.certifyTrueAndAccurate')}</Text>}
defaultValue={defaultValues[COMPLETE_VERIFICATION_KEYS.CERTIFY_TRUE_INFORMATION]}
defaultValue={defaultValues.certifyTrueInformation}
shouldSaveDraft
/>
<InputWrapper
Expand All @@ -112,7 +103,7 @@ function ConfirmAgreements({onNext, reimbursementAccount}: ConfirmAgreementsProp
<TextLink href="https://use.expensify.com/achterms">{`${translate('completeVerificationStep.termsAndConditions')}`}</TextLink>
</Text>
)}
defaultValue={defaultValues[COMPLETE_VERIFICATION_KEYS.ACCEPT_TERMS_AND_CONDITIONS]}
defaultValue={defaultValues.acceptTermsAndConditions}
shouldSaveDraft
/>
</FormProvider>
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/pages/ReimbursementAccount/utils/getSubstepValues.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {OnyxEntry} from 'react-native-onyx';
import type {ReimbursementAccount, ReimbursementAccountDraft} from '@src/types/onyx';
import getDefaultValueForReimbursementAccountField from './getDefaultValueForReimbursementAccountField';
import type {ACHData} from '@src/types/onyx/ReimbursementAccount';

function getSubstepValues<T extends keyof ReimbursementAccountDraft>(
inputKeys: Record<string, T>,
Expand All @@ -10,7 +10,7 @@ function getSubstepValues<T extends keyof ReimbursementAccountDraft>(
return Object.entries(inputKeys).reduce(
(acc, [, value]) => ({
...acc,
[value]: reimbursementAccountDraft?.[value] ?? getDefaultValueForReimbursementAccountField(reimbursementAccount, value, ''),
[value]: reimbursementAccountDraft?.[value] ?? reimbursementAccount?.achData?.[value as keyof ACHData] ?? '',
}),
{} as {[K in T]: ReimbursementAccountDraft[K]},
);
Expand Down
7 changes: 4 additions & 3 deletions src/types/onyx/ReimbursementAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';
import type {BankName} from './Bank';
import type * as OnyxCommon from './OnyxCommon';
import type {BeneficialOwnersStepDraftProps, CompanyStepProps, RequestorStepProps} from './ReimbursementAccountDraft';
import type {ACHContractStepProps, BeneficialOwnersStepDraftProps, CompanyStepProps, RequestorStepProps} from './ReimbursementAccountDraft';

type BankAccountStep = ValueOf<typeof CONST.BANK_ACCOUNT.STEP>;

Expand Down Expand Up @@ -34,7 +34,8 @@ type ACHData = {
addressName?: string;
} & BeneficialOwnersStepDraftProps &
CompanyStepProps &
RequestorStepProps;
RequestorStepProps &
ACHContractStepProps;

type ReimbursementAccount = {
/** Whether we are loading the data via the API */
Expand Down Expand Up @@ -68,4 +69,4 @@ type ReimbursementAccount = {
};

export default ReimbursementAccount;
export type {BankAccountStep, BankAccountSubStep};
export type {BankAccountStep, BankAccountSubStep, ACHData};

0 comments on commit 7cee965

Please sign in to comment.