Skip to content

Commit

Permalink
Merge pull request Expensify#22906 from tienifr/fix/22606-private-per…
Browse files Browse the repository at this point in the history
…sonal-details-are-not-fetched
  • Loading branch information
dangrous authored Jul 25, 2023
2 parents 2beee7b + 60ed833 commit abfbefa
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/components/NewDatePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class NewDatePicker extends React.Component {
this.defaultValue = props.defaultValue ? moment(props.defaultValue).format(CONST.DATE.MOMENT_FORMAT_STRING) : '';
}

componentDidUpdate(prevProps) {
if (prevProps.value === this.props.value) {
return;
}
this.setDate(this.props.value);
}

/**
* Trigger the `onInputChange` handler when the user input has a complete date or is cleared
* @param {string} selectedDate
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/usePrivatePersonalDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {useEffect, useContext} from 'react';
import * as PersonalDetails from '../libs/actions/PersonalDetails';
import {NetworkContext} from '../components/OnyxProvider';

/**
* Hook for fetching private personal details
*/
export default function usePrivatePersonalDetails() {
const {isOffline} = useContext(NetworkContext);

useEffect(() => {
if (isOffline || Boolean(PersonalDetails.getPrivatePersonalDetails())) {
return;
}
PersonalDetails.openPersonalDetailsPage();
}, [isOffline]);
}
47 changes: 46 additions & 1 deletion src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Onyx.connect({
callback: (val) => (allPersonalDetails = val),
});

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

/**
* Returns the displayName for a user
*
Expand Down Expand Up @@ -325,7 +331,37 @@ function updateSelectedTimezone(selectedTimezone) {
* Fetches additional personal data like legal name, date of birth, address
*/
function openPersonalDetailsPage() {
API.read('OpenPersonalDetailsPage');
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
value: {
isLoading: true,
},
},
];

const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
value: {
isLoading: false,
},
},
];

const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
value: {
isLoading: false,
},
},
];

API.read('OpenPersonalDetailsPage', {}, {optimisticData, successData, failureData});
}

/**
Expand Down Expand Up @@ -478,6 +514,14 @@ function clearAvatarErrors() {
});
}

/**
* Get private personal details value
* @returns {Object}
*/
function getPrivatePersonalDetails() {
return privatePersonalDetails;
}

export {
getDisplayName,
getDisplayNameForTypingIndicator,
Expand All @@ -495,4 +539,5 @@ export {
updateAutomaticTimezone,
updateSelectedTimezone,
getCountryISO,
getPrivatePersonalDetails,
};
7 changes: 7 additions & 0 deletions src/pages/settings/Profile/PersonalDetails/AddressPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import CountryPicker from '../../../../components/CountryPicker';
import StatePicker from '../../../../components/StatePicker';
import Navigation from '../../../../libs/Navigation/Navigation';
import ROUTES from '../../../../ROUTES';
import usePrivatePersonalDetails from '../../../../hooks/usePrivatePersonalDetails';
import FullscreenLoadingIndicator from '../../../../components/FullscreenLoadingIndicator';

const propTypes = {
/* Onyx Props */
Expand Down Expand Up @@ -61,6 +63,7 @@ function updateAddress(values) {
}

function AddressPage(props) {
usePrivatePersonalDetails();
const {translate} = props;
const [countryISO, setCountryISO] = useState(PersonalDetails.getCountryISO(lodashGet(props.privatePersonalDetails, 'address.country')) || CONST.COUNTRY.US);
const isUSAForm = countryISO === CONST.COUNTRY.US;
Expand Down Expand Up @@ -117,6 +120,10 @@ function AddressPage(props) {
return errors;
}, []);

if (lodashGet(props.privatePersonalDetails, 'isLoading', true)) {
return <FullscreenLoadingIndicator />;
}

return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithBackButton
Expand Down
9 changes: 9 additions & 0 deletions src/pages/settings/Profile/PersonalDetails/DateOfBirthPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment';
import PropTypes from 'prop-types';
import React, {useCallback} from 'react';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import CONST from '../../../../CONST';
import ONYXKEYS from '../../../../ONYXKEYS';
import ROUTES from '../../../../ROUTES';
Expand All @@ -15,6 +16,8 @@ import * as ValidationUtils from '../../../../libs/ValidationUtils';
import * as PersonalDetails from '../../../../libs/actions/PersonalDetails';
import compose from '../../../../libs/compose';
import styles from '../../../../styles/styles';
import usePrivatePersonalDetails from '../../../../hooks/usePrivatePersonalDetails';
import FullscreenLoadingIndicator from '../../../../components/FullscreenLoadingIndicator';

const propTypes = {
/* Onyx Props */
Expand All @@ -34,6 +37,8 @@ const defaultProps = {
};

function DateOfBirthPage({translate, privatePersonalDetails}) {
usePrivatePersonalDetails();

/**
* @param {Object} values
* @param {String} values.dob - date of birth
Expand All @@ -55,6 +60,10 @@ function DateOfBirthPage({translate, privatePersonalDetails}) {
return errors;
}, []);

if (lodashGet(privatePersonalDetails, 'isLoading', true)) {
return <FullscreenLoadingIndicator />;
}

return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithBackButton
Expand Down
7 changes: 7 additions & 0 deletions src/pages/settings/Profile/PersonalDetails/LegalNamePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import * as PersonalDetails from '../../../../libs/actions/PersonalDetails';
import compose from '../../../../libs/compose';
import Navigation from '../../../../libs/Navigation/Navigation';
import ROUTES from '../../../../ROUTES';
import usePrivatePersonalDetails from '../../../../hooks/usePrivatePersonalDetails';
import FullscreenLoadingIndicator from '../../../../components/FullscreenLoadingIndicator';

const propTypes = {
/* Onyx Props */
Expand All @@ -42,6 +44,7 @@ const updateLegalName = (values) => {
};

function LegalNamePage(props) {
usePrivatePersonalDetails();
const legalFirstName = lodashGet(props.privatePersonalDetails, 'legalFirstName', '');
const legalLastName = lodashGet(props.privatePersonalDetails, 'legalLastName', '');

Expand All @@ -63,6 +66,10 @@ function LegalNamePage(props) {
return errors;
}, []);

if (lodashGet(props.privatePersonalDetails, 'isLoading', true)) {
return <FullscreenLoadingIndicator />;
}

return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {useEffect} from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import {ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import HeaderWithBackButton from '../../../../components/HeaderWithBackButton';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
Expand All @@ -11,9 +12,10 @@ import styles from '../../../../styles/styles';
import Navigation from '../../../../libs/Navigation/Navigation';
import compose from '../../../../libs/compose';
import MenuItemWithTopDescription from '../../../../components/MenuItemWithTopDescription';
import * as PersonalDetails from '../../../../libs/actions/PersonalDetails';
import ONYXKEYS from '../../../../ONYXKEYS';
import {withNetwork} from '../../../../components/OnyxProvider';
import usePrivatePersonalDetails from '../../../../hooks/usePrivatePersonalDetails';
import FullscreenLoadingIndicator from '../../../../components/FullscreenLoadingIndicator';

const propTypes = {
/* Onyx Props */
Expand Down Expand Up @@ -54,13 +56,7 @@ const defaultProps = {
};

function PersonalDetailsInitialPage(props) {
useEffect(() => {
if (props.network.isOffline) {
return;
}
PersonalDetails.openPersonalDetailsPage();
}, [props.network.isOffline]);

usePrivatePersonalDetails();
const privateDetails = props.privatePersonalDetails || {};
const address = privateDetails.address || {};
const legalName = `${privateDetails.legalFirstName || ''} ${privateDetails.legalLastName || ''}`.trim();
Expand All @@ -87,6 +83,10 @@ function PersonalDetailsInitialPage(props) {
return formattedAddress.trim().replace(/,$/, '');
};

if (lodashGet(props.privatePersonalDetails, 'isLoading', true)) {
return <FullscreenLoadingIndicator />;
}

return (
<ScreenWrapper>
<HeaderWithBackButton
Expand Down

0 comments on commit abfbefa

Please sign in to comment.