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

Fix pronouns is empty when we access by deep link after signing in #28368

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Changes from 1 commit
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
73 changes: 52 additions & 21 deletions src/pages/settings/Profile/PronounsPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import _ from 'underscore';
import lodashGet from 'lodash/get';
import React, {useState, useMemo} from 'react';
import React, {useState, useMemo, useRef, useEffect} from 'react';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../../components/withCurrentUserPersonalDetails';
import ScreenWrapper from '../../../components/ScreenWrapper';
import HeaderWithBackButton from '../../../components/HeaderWithBackButton';
Expand All @@ -12,27 +14,43 @@ import ROUTES from '../../../ROUTES';
import Navigation from '../../../libs/Navigation/Navigation';
import SelectionList from '../../../components/SelectionList';
import useLocalize from '../../../hooks/useLocalize';
import ONYXKEYS from '../../../ONYXKEYS';
import FullScreenLoadingIndicator from '../../../components/FullscreenLoadingIndicator';
import compose from '../../../libs/compose';

const propTypes = {
...withCurrentUserPersonalDetailsPropTypes,

/** Indicates whether the app is loading initial data */
isLoadingReportData: PropTypes.bool,
};

const defaultProps = {
...withCurrentUserPersonalDetailsDefaultProps,
isLoadingReportData: true,
};

function PronounsPage({currentUserPersonalDetails}) {
function PronounsPage({currentUserPersonalDetails, isLoadingReportData}) {
const {translate} = useLocalize();
const currentPronouns = lodashGet(currentUserPersonalDetails, 'pronouns', '');
const currentPronounsKey = currentPronouns.substring(CONST.PRONOUNS.PREFIX.length);
const loadingCompleteRef = useRef(false);
const [searchValue, setSearchValue] = useState('');

const [searchValue, setSearchValue] = useState(() => {
useEffect(() => {
if ((isLoadingReportData && _.isEmpty(currentUserPersonalDetails.login)) || loadingCompleteRef.current) {
return;
}
loadingCompleteRef.current = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dukenv0307 Can you please explain this condition? I'm not able to understand fully why we need to use the ref here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the expectation here is only to set the current pronouns to the state the first time. After that, if we update this on another device, we don't want to update this to search text because we are finding another in the current device.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dukenv0307 I think if we use IS_LOADING_APP instead of IS_LOADING_REPORT_DATA, there would be no need for the ref. Can you please verify / confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we are using IS_LOADING_REPORT_DATA from other components and only use IS_LOADING_APP in AuthScreen

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, is this an issue? I mean, we can use it anywhere. Essentially, it is just setting the value of the constant as true when openApp is called. The command openApp is called only once on the first load and this is what we need here as well I assume.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work fine? 😸

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and it works fine.

const currentPronounsText = _.chain(CONST.PRONOUNS_LIST)
.find((_value) => _value === currentPronounsKey)
.value();

return currentPronounsText ? translate(`pronouns.${currentPronounsText}`) : '';
});
setSearchValue(currentPronounsText ? translate(`pronouns.${currentPronounsText}`) : '');

// Only need to add login to dependency because after the data is loaded, other fields are also exist
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentUserPersonalDetails.login, isLoadingReportData]);

const filteredPronounsList = useMemo(() => {
const pronouns = _.chain(CONST.PRONOUNS_LIST)
Expand Down Expand Up @@ -69,21 +87,27 @@ function PronounsPage({currentUserPersonalDetails}) {
includeSafeAreaPaddingBottom={false}
testID={PronounsPage.displayName}
>
<HeaderWithBackButton
title={translate('pronounsPage.pronouns')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
/>
<Text style={[styles.ph5, styles.mb3]}>{translate('pronounsPage.isShownOnProfile')}</Text>
<SelectionList
headerMessage={headerMessage}
textInputLabel={translate('pronounsPage.pronouns')}
textInputPlaceholder={translate('pronounsPage.placeholderText')}
textInputValue={searchValue}
sections={[{data: filteredPronounsList, indexOffset: 0}]}
onSelectRow={updatePronouns}
onChangeText={setSearchValue}
initiallyFocusedOptionKey={currentPronounsKey}
/>
{isLoadingReportData && _.isEmpty(currentUserPersonalDetails.login) ? (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be || instead @dukenv0307?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we already have currentUserPersonalDetail, we don't need to wait isLoadingReportData as false.

<FullScreenLoadingIndicator />
) : (
<>
<HeaderWithBackButton
title={translate('pronounsPage.pronouns')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
/>
<Text style={[styles.ph5, styles.mb3]}>{translate('pronounsPage.isShownOnProfile')}</Text>
<SelectionList
headerMessage={headerMessage}
textInputLabel={translate('pronounsPage.pronouns')}
textInputPlaceholder={translate('pronounsPage.placeholderText')}
textInputValue={searchValue}
sections={[{data: filteredPronounsList, indexOffset: 0}]}
onSelectRow={updatePronouns}
onChangeText={setSearchValue}
initiallyFocusedOptionKey={currentPronounsKey}
/>
</>
)}
</ScreenWrapper>
);
}
Expand All @@ -92,4 +116,11 @@ PronounsPage.propTypes = propTypes;
PronounsPage.defaultProps = defaultProps;
PronounsPage.displayName = 'PronounsPage';

export default withCurrentUserPersonalDetails(PronounsPage);
export default compose(
withCurrentUserPersonalDetails,
withOnyx({
isLoadingReportData: {
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
},
}),
)(PronounsPage);
Loading