Skip to content

Commit

Permalink
Merge pull request #56581 from daledah/fix/56095
Browse files Browse the repository at this point in the history
fix: autofill country on address form
  • Loading branch information
mjasikowski authored Feb 12, 2025
2 parents 4c1919e + 8419c0f commit 6000b2a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
37 changes: 27 additions & 10 deletions src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type {OnyxInputOrEntry, PersonalDetails, PersonalDetailsList, PrivatePers
import type {Address} from '@src/types/onyx/PrivatePersonalDetails';
import type {OnyxData} from '@src/types/onyx/Request';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as Localize from './Localize';
import * as UserUtils from './UserUtils';
import {formatPhoneNumber} from './LocalePhoneNumber';
import {translateLocal} from './Localize';
import {generateAccountID} from './UserUtils';

type FirstAndLastName = {
firstName: string;
Expand Down Expand Up @@ -42,8 +42,20 @@ Onyx.connect({
if (!value) {
return;
}
hiddenTranslation = Localize.translateLocal('common.hidden');
youTranslation = Localize.translateLocal('common.you').toLowerCase();
hiddenTranslation = translateLocal('common.hidden');
youTranslation = translateLocal('common.you').toLowerCase();
},
});

let defaultCountry = '';

Onyx.connect({
key: ONYXKEYS.COUNTRY,
callback: (value) => {
if (!value) {
return;
}
defaultCountry = value;
},
});

Expand Down Expand Up @@ -118,7 +130,7 @@ function getPersonalDetailsByIDs({
if (shouldChangeUserDisplayName && currentUserAccountID === detail.accountID) {
return {
...detail,
displayName: Localize.translateLocal('common.you'),
displayName: translateLocal('common.you'),
};
}

Expand All @@ -143,7 +155,7 @@ function getAccountIDsByLogins(logins: string[]): number[] {
const currentDetail = personalDetails.find((detail) => detail?.login === login?.toLowerCase());
if (!currentDetail) {
// generate an account ID because in this case the detail is probably new, so we don't have a real accountID yet
foundAccountIDs.push(UserUtils.generateAccountID(login));
foundAccountIDs.push(generateAccountID(login));
} else {
foundAccountIDs.push(Number(currentDetail.accountID));
}
Expand Down Expand Up @@ -197,7 +209,7 @@ function getPersonalDetailsOnyxDataForOptimisticUsers(newLogins: string[], newAc
personalDetailsNew[accountID] = {
login,
accountID,
displayName: LocalePhoneNumber.formatPhoneNumber(login),
displayName: formatPhoneNumber(login),
isOptimisticPersonalDetail: true,
};

Expand Down Expand Up @@ -294,7 +306,7 @@ function getFormattedAddress(privatePersonalDetails: OnyxEntry<PrivatePersonalDe
*/
function getEffectiveDisplayName(personalDetail?: PersonalDetails): string | undefined {
if (personalDetail) {
return LocalePhoneNumber.formatPhoneNumber(personalDetail?.login ?? '') || personalDetail.displayName;
return formatPhoneNumber(personalDetail?.login ?? '') || personalDetail.displayName;
}

return undefined;
Expand All @@ -306,7 +318,7 @@ function getEffectiveDisplayName(personalDetail?: PersonalDetails): string | und
function createDisplayName(login: string, passedPersonalDetails: Pick<PersonalDetails, 'firstName' | 'lastName'> | OnyxInputOrEntry<PersonalDetails>): string {
// If we have a number like [email protected] then let's remove @expensify.sms and format it
// so that the option looks cleaner in our UI.
const userLogin = LocalePhoneNumber.formatPhoneNumber(login);
const userLogin = formatPhoneNumber(login);

if (!passedPersonalDetails) {
return userLogin;
Expand Down Expand Up @@ -370,6 +382,10 @@ function getUserNameByEmail(email: string, nameToDisplay: 'firstName' | 'display
return email;
}

function getDefaultCountry() {
return defaultCountry;
}

export {
isPersonalDetailsEmpty,
getDisplayNameOrDefault,
Expand All @@ -388,4 +404,5 @@ export {
getNewAccountIDsAndLogins,
getPersonalDetailsLength,
getUserNameByEmail,
getDefaultCountry,
};
6 changes: 4 additions & 2 deletions src/pages/AddressPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ type AddressPageProps = {
updateAddress: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM>) => void;
/** Title of address page */
title: string;

defaultCountry?: Country;
} & BackToParams;

function AddressPage({title, address, updateAddress, isLoadingApp = true, backTo}: AddressPageProps) {
function AddressPage({title, address, updateAddress, isLoadingApp = true, backTo, defaultCountry}: AddressPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();

// Check if country is valid
const {street} = address ?? {};
const [street1, street2] = street ? street.split('\n') : [undefined, undefined];
const [currentCountry, setCurrentCountry] = useState(address?.country);
const [currentCountry, setCurrentCountry] = useState(address?.country ?? defaultCountry);
const [state, setState] = useState(address?.state);
const [city, setCity] = useState(address?.city);
const [zipcode, setZipcode] = useState(address?.zip);
Expand Down
40 changes: 12 additions & 28 deletions src/pages/settings/Profile/PersonalDetails/PersonalAddressPage.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
import React, {useMemo} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import useLocalize from '@hooks/useLocalize';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import {getCurrentAddress, getDefaultCountry} from '@libs/PersonalDetailsUtils';
import AddressPage from '@pages/AddressPage';
import * as PersonalDetails from '@userActions/PersonalDetails';
import type {FormOnyxValues} from '@src/components/Form/types';
import type {Country} from '@src/CONST';
import {updateAddress as updateAddressPersonalDetails} from '@src/libs/actions/PersonalDetails';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import type {PrivatePersonalDetails} from '@src/types/onyx';

type PersonalAddressPageOnyxProps = {
/** User's private personal details */
privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>;
/** Whether app is loading */
isLoadingApp: OnyxEntry<boolean>;
};

type PersonalAddressPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.ADDRESS> & PersonalAddressPageOnyxProps;

/**
* Submit form to update user's first and last legal name
* @param values - form input values
*/
function updateAddress(values: FormOnyxValues<typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM>) {
PersonalDetails.updateAddress(
updateAddressPersonalDetails(
values.addressLine1?.trim() ?? '',
values.addressLine2?.trim() ?? '',
values.city.trim(),
Expand All @@ -36,12 +23,16 @@ function updateAddress(values: FormOnyxValues<typeof ONYXKEYS.FORMS.HOME_ADDRESS
);
}

function PersonalAddressPage({privatePersonalDetails, isLoadingApp = true}: PersonalAddressPageProps) {
function PersonalAddressPage() {
const {translate} = useLocalize();
const address = useMemo(() => PersonalDetailsUtils.getCurrentAddress(privatePersonalDetails), [privatePersonalDetails]);
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS);
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP);
const address = useMemo(() => getCurrentAddress(privatePersonalDetails), [privatePersonalDetails]);
const defaultCountry = getDefaultCountry();

return (
<AddressPage
defaultCountry={defaultCountry as Country}
address={address}
isLoadingApp={isLoadingApp}
updateAddress={updateAddress}
Expand All @@ -52,11 +43,4 @@ function PersonalAddressPage({privatePersonalDetails, isLoadingApp = true}: Pers

PersonalAddressPage.displayName = 'PersonalAddressPage';

export default withOnyx<PersonalAddressPageProps, PersonalAddressPageOnyxProps>({
privatePersonalDetails: {
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
},
isLoadingApp: {
key: ONYXKEYS.IS_LOADING_APP,
},
})(PersonalAddressPage);
export default PersonalAddressPage;

0 comments on commit 6000b2a

Please sign in to comment.