Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace address with addresses for private personal details #45751

Merged
merged 7 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/libs/GetPhysicalCardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import * as PersonalDetailsUtils from './PersonalDetailsUtils';
import * as UserUtils from './UserUtils';

function getCurrentRoute(domain: string, privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>): Route {
const {address, legalFirstName, legalLastName, phoneNumber} = privatePersonalDetails ?? {};
const {legalFirstName, legalLastName, phoneNumber} = privatePersonalDetails ?? {};
const address = PersonalDetailsUtils.getAddress(privatePersonalDetails);

if (!legalFirstName && !legalLastName) {
return ROUTES.SETTINGS_WALLET_CARD_GET_PHYSICAL_NAME.getRoute(domain);
Expand Down Expand Up @@ -55,7 +56,8 @@ function setCurrentRoute(currentRoute: string, domain: string, privatePersonalDe
* @returns
*/
function getUpdatedDraftValues(draftValues: OnyxEntry<GetPhysicalCardForm>, privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>, loginList: OnyxEntry<LoginList>): GetPhysicalCardForm {
const {address, legalFirstName, legalLastName, phoneNumber} = privatePersonalDetails ?? {};
const {legalFirstName, legalLastName, phoneNumber} = privatePersonalDetails ?? {};
const address = PersonalDetailsUtils.getAddress(privatePersonalDetails);

return {
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
Expand All @@ -78,13 +80,13 @@ function getUpdatedDraftValues(draftValues: OnyxEntry<GetPhysicalCardForm>, priv
* @param draftValues
* @returns
*/
function getUpdatedPrivatePersonalDetails(draftValues: OnyxEntry<GetPhysicalCardForm>): PrivatePersonalDetails {
function getUpdatedPrivatePersonalDetails(draftValues: OnyxEntry<GetPhysicalCardForm>, privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>): PrivatePersonalDetails {
const {addressLine1, addressLine2, city = '', country = '', legalFirstName, legalLastName, phoneNumber, state = '', zipPostCode = ''} = draftValues ?? {};
return {
legalFirstName,
legalLastName,
phoneNumber,
address: {street: PersonalDetailsUtils.getFormattedStreet(addressLine1, addressLine2), city, country, state, zip: zipPostCode},
addresses: [...(privatePersonalDetails?.addresses ?? []), {street: PersonalDetailsUtils.getFormattedStreet(addressLine1, addressLine2), city, country, state, zip: zipPostCode}],
};
}

Expand Down
15 changes: 14 additions & 1 deletion src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxInputOrEntry, PersonalDetails, PersonalDetailsList, PrivatePersonalDetails} from '@src/types/onyx';
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';
Expand Down Expand Up @@ -224,14 +225,25 @@ function getStreetLines(street = '') {
return [streets[0], streets[1]];
}

/**
* Get the current address from addresses array
*
* @param privatePersonalDetails - details object
* @returns - current address object
*/
function getAddress(privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>): Address | undefined {
tienifr marked this conversation as resolved.
Show resolved Hide resolved
const {addresses} = privatePersonalDetails ?? {};
return addresses?.[addresses.length - 1];
tienifr marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Formats an address object into an easily readable string
*
* @param privatePersonalDetails - details object
* @returns - formatted address
*/
function getFormattedAddress(privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>): string {
const {address} = privatePersonalDetails ?? {};
const address = getAddress(privatePersonalDetails);
const [street1, street2] = getStreetLines(address?.street);
const formattedAddress =
formatPiece(street1) + formatPiece(street2) + formatPiece(address?.city) + formatPiece(address?.state) + formatPiece(address?.zip) + formatPiece(address?.country);
Expand Down Expand Up @@ -318,6 +330,7 @@ export {
getAccountIDsByLogins,
getLoginsByAccountIDs,
getPersonalDetailsOnyxDataForOptimisticUsers,
getAddress,
getFormattedAddress,
getFormattedStreet,
getStreetLines,
Expand Down
25 changes: 17 additions & 8 deletions src/libs/actions/PersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {DateOfBirthForm} from '@src/types/form';
import type {PersonalDetails, PersonalDetailsList} from '@src/types/onyx';
import type {PersonalDetails, PersonalDetailsList, PrivatePersonalDetails} from '@src/types/onyx';
import type {SelectedTimezone, Timezone} from '@src/types/onyx/PersonalDetails';

let currentUserEmail = '';
Expand All @@ -42,6 +42,12 @@ Onyx.connect({
callback: (val) => (allPersonalDetails = val),
});

let privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>;
Onyx.connect({
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
callback: (val) => (privatePersonalDetails = val),
});

function updatePronouns(pronouns: string) {
if (!currentUserAccountID) {
return;
Expand Down Expand Up @@ -170,13 +176,16 @@ function updateAddress(street: string, street2: string, city: string, state: str
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
value: {
address: {
street: PersonalDetailsUtils.getFormattedStreet(street, street2),
city,
state,
zip,
country,
},
addresses: [
...(privatePersonalDetails?.addresses ?? []),
{
street: PersonalDetailsUtils.getFormattedStreet(street, street2),
city,
state,
zip,
country,
tienifr marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
},
],
Expand Down
4 changes: 3 additions & 1 deletion src/libs/actions/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
} from '@libs/API/parameters';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import type {PrivatePersonalDetails} from '@libs/GetPhysicalCardUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import type CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {WalletAdditionalQuestionDetails} from '@src/types/onyx';
Expand Down Expand Up @@ -257,7 +258,8 @@ function answerQuestionsForWallet(answers: WalletQuestionAnswer[], idNumber: str
}

function requestPhysicalExpensifyCard(cardID: number, authToken: string, privatePersonalDetails: PrivatePersonalDetails) {
const {legalFirstName = '', legalLastName = '', phoneNumber = '', address: {city = '', country = '', state = '', street = '', zip = ''} = {}} = privatePersonalDetails;
const {legalFirstName = '', legalLastName = '', phoneNumber = ''} = privatePersonalDetails;
const {city = '', country = '', state = '', street = '', zip = ''} = PersonalDetailsUtils.getAddress(privatePersonalDetails) ?? {};

const requestParams: RequestPhysicalExpensifyCardParams = {
authToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import useLocalize from '@hooks/useLocalize';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import AddressPage from '@pages/AddressPage';
import * as PersonalDetails from '@userActions/PersonalDetails';
import type {FormOnyxValues} from '@src/components/Form/types';
Expand Down Expand Up @@ -37,7 +38,7 @@ function updateAddress(values: FormOnyxValues<typeof ONYXKEYS.FORMS.HOME_ADDRESS

function PersonalAddressPage({privatePersonalDetails, isLoadingApp = true}: PersonalAddressPageProps) {
const {translate} = useLocalize();
const address = useMemo(() => privatePersonalDetails?.address, [privatePersonalDetails]);
const address = useMemo(() => PersonalDetailsUtils.getAddress(privatePersonalDetails), [privatePersonalDetails]);

return (
<AddressPage
Expand Down
18 changes: 10 additions & 8 deletions src/pages/settings/Profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ function ProfilePage({
legalFirstName: '',
legalLastName: '',
dob: '',
address: {
street: '',
street2: '',
city: '',
state: '',
zip: '',
country: '',
},
addresses: [
{
street: '',
street2: '',
city: '',
state: '',
zip: '',
country: '',
},
],
},
currentUserPersonalDetails,
isLoadingApp,
Expand Down
6 changes: 3 additions & 3 deletions src/pages/settings/Wallet/Card/BaseGetPhysicalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ function BaseGetPhysicalCard({
}

// Redirect user to previous steps of the flow if he hasn't finished them yet
GetPhysicalCardUtils.setCurrentRoute(currentRoute, domain, GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues));
GetPhysicalCardUtils.setCurrentRoute(currentRoute, domain, GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues, privatePersonalDetails));
isRouteSet.current = true;
}, [cardList, currentRoute, domain, domainCards.length, draftValues, loginList, cardToBeIssued, privatePersonalDetails]);

const onSubmit = useCallback(() => {
const updatedPrivatePersonalDetails = GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues);
const updatedPrivatePersonalDetails = GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(draftValues, privatePersonalDetails);
// If the current step of the get physical card flow is the confirmation page
if (isConfirmation) {
Wallet.requestPhysicalExpensifyCard(cardToBeIssued?.cardID ?? -1, session?.authToken ?? '', updatedPrivatePersonalDetails);
Expand All @@ -156,7 +156,7 @@ function BaseGetPhysicalCard({
return;
}
GetPhysicalCardUtils.goToNextPhysicalCardRoute(domain, updatedPrivatePersonalDetails);
}, [cardID, cardToBeIssued?.cardID, domain, draftValues, isConfirmation, session?.authToken]);
}, [cardID, cardToBeIssued?.cardID, domain, draftValues, isConfirmation, session?.authToken, privatePersonalDetails]);
return (
<ScreenWrapper
shouldEnablePickerAvoiding={false}
Expand Down
16 changes: 9 additions & 7 deletions src/pages/settings/Wallet/Card/GetPhysicalCardConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ function GetPhysicalCardConfirm({
onPress={() => goToGetPhysicalCardAddress(domain)}
shouldShowRightIcon
title={PersonalDetailsUtils.getFormattedAddress({
address: {
street: PersonalDetailsUtils.getFormattedStreet(addressLine1, addressLine2),
city,
state,
zip: zipPostCode,
country,
},
addresses: [
{
street: PersonalDetailsUtils.getFormattedStreet(addressLine1, addressLine2),
city,
state,
zip: zipPostCode,
country,
},
],
})}
/>
</BaseGetPhysicalCard>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Wallet/ExpensifyCardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function ExpensifyCardPage({
FormActions.setDraftValues(ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM, updatedDraftValues);
}

GetPhysicalCardUtils.goToNextPhysicalCardRoute(domain, GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(updatedDraftValues));
GetPhysicalCardUtils.goToNextPhysicalCardRoute(domain, GetPhysicalCardUtils.getUpdatedPrivatePersonalDetails(updatedDraftValues, privatePersonalDetails));
};

if (isNotFound) {
Expand Down
18 changes: 10 additions & 8 deletions src/pages/settings/Wallet/ReportCardLostPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ type ReportCardLostPageProps = ReportCardLostPageOnyxProps & StackScreenProps<Se

function ReportCardLostPage({
privatePersonalDetails = {
address: {
street: '',
street2: '',
city: '',
state: '',
zip: '',
country: '',
},
addresses: [
{
street: '',
street2: '',
city: '',
state: '',
zip: '',
country: '',
},
],
},
cardList = {},
route: {
Expand Down
16 changes: 9 additions & 7 deletions src/pages/settings/Wallet/WalletPage/CardDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import ROUTES from '@src/ROUTES';
import type {PrivatePersonalDetails} from '@src/types/onyx';

const defaultPrivatePersonalDetails: PrivatePersonalDetails = {
address: {
street: '',
city: '',
state: '',
zip: '',
country: '',
},
addresses: [
{
street: '',
city: '',
state: '',
zip: '',
country: '',
},
],
};

type CardDetailsOnyxProps = {
Expand Down
5 changes: 4 additions & 1 deletion src/types/onyx/PrivatePersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ type PrivatePersonalDetails = {
/** User's phone number */
phoneNumber?: string;

/** User's home address */
/** @deprecated User's home address */
address?: Address;

/** User's home address history. The most recent address is the last item in the array */
addresses?: Address[];
};

export default PrivatePersonalDetails;
Expand Down
Loading