Skip to content

Commit 54c5c4e

Browse files
authored
Merge pull request #26192 from thiagobrez/thiagobrez/pronouns-displayed-without-serach
fix(pronouns-page): pronouns displayed without search
2 parents 95a0edc + df01058 commit 54c5c4e

File tree

1 file changed

+37
-67
lines changed

1 file changed

+37
-67
lines changed
Lines changed: 37 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,45 @@
11
import _ from 'underscore';
22
import lodashGet from 'lodash/get';
3-
import React, {useState, useEffect, useCallback} from 'react';
3+
import React, {useState, useMemo} from 'react';
44
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../../components/withCurrentUserPersonalDetails';
55
import ScreenWrapper from '../../../components/ScreenWrapper';
66
import HeaderWithBackButton from '../../../components/HeaderWithBackButton';
7-
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
87
import Text from '../../../components/Text';
98
import styles from '../../../styles/styles';
109
import * as PersonalDetails from '../../../libs/actions/PersonalDetails';
11-
import compose from '../../../libs/compose';
1210
import CONST from '../../../CONST';
1311
import ROUTES from '../../../ROUTES';
1412
import Navigation from '../../../libs/Navigation/Navigation';
1513
import SelectionList from '../../../components/SelectionList';
14+
import useLocalize from '../../../hooks/useLocalize';
1615

1716
const propTypes = {
18-
...withLocalizePropTypes,
1917
...withCurrentUserPersonalDetailsPropTypes,
2018
};
2119

2220
const defaultProps = {
2321
...withCurrentUserPersonalDetailsDefaultProps,
2422
};
2523

26-
function PronounsPage(props) {
27-
const [initiallyFocusedOption, setInitiallyFocusedOption] = useState({});
28-
const [searchValue, setSearchValue] = useState('');
29-
const [pronounsList, setPronounsList] = useState([]);
24+
function PronounsPage({currentUserPersonalDetails}) {
25+
const {translate} = useLocalize();
26+
const currentPronouns = lodashGet(currentUserPersonalDetails, 'pronouns', '');
27+
const currentPronounsKey = currentPronouns.substring(CONST.PRONOUNS.PREFIX.length);
3028

31-
/**
32-
* Loads the pronouns list from the translations and adds the green checkmark icon to the currently selected value.
33-
*
34-
* @returns {void}
35-
*/
36-
const loadPronouns = useCallback(() => {
37-
const currentPronouns = lodashGet(props.currentUserPersonalDetails, 'pronouns', '');
29+
const [searchValue, setSearchValue] = useState(() => {
30+
const currentPronounsText = _.chain(translate('pronouns'))
31+
.find((_value, key) => key === currentPronounsKey)
32+
.value();
33+
34+
return currentPronounsText || '';
35+
});
3836

39-
const pronouns = _.chain(props.translate('pronouns'))
37+
const filteredPronounsList = useMemo(() => {
38+
const pronouns = _.chain(translate('pronouns'))
4039
.map((value, key) => {
4140
const fullPronounKey = `${CONST.PRONOUNS.PREFIX}${key}`;
4241
const isCurrentPronouns = fullPronounKey === currentPronouns;
4342

44-
if (isCurrentPronouns) {
45-
setInitiallyFocusedOption({
46-
text: value,
47-
keyForList: key,
48-
});
49-
}
50-
5143
return {
5244
text: value,
5345
value: fullPronounKey,
@@ -58,60 +50,38 @@ function PronounsPage(props) {
5850
.sortBy((pronoun) => pronoun.text.toLowerCase())
5951
.value();
6052

61-
setPronounsList(pronouns);
53+
const trimmedSearch = searchValue.trim();
6254

63-
// eslint-disable-next-line react-hooks/exhaustive-deps
64-
}, [props.currentUserPersonalDetails.pronouns]);
55+
if (trimmedSearch.length === 0) {
56+
return [];
57+
}
58+
return _.filter(pronouns, (pronoun) => pronoun.text.toLowerCase().indexOf(trimmedSearch.toLowerCase()) >= 0);
59+
}, [searchValue, currentPronouns, translate]);
6560

66-
const onChangeText = (value = '') => {
67-
setSearchValue(value);
68-
};
61+
const headerMessage = searchValue.trim() && filteredPronounsList.length === 0 ? translate('common.noResultsFound') : '';
6962

70-
/**
71-
* @param {Object} selectedPronouns
72-
*/
7363
const updatePronouns = (selectedPronouns) => {
74-
PersonalDetails.updatePronouns(selectedPronouns.keyForList === initiallyFocusedOption.keyForList ? '' : lodashGet(selectedPronouns, 'value', ''));
64+
PersonalDetails.updatePronouns(selectedPronouns.keyForList === currentPronouns.keyForList ? '' : lodashGet(selectedPronouns, 'value', ''));
7565
};
7666

77-
/**
78-
* Pronouns list filtered by searchValue needed for the OptionsSelector.
79-
* Empty array if the searchValue is empty.
80-
*/
81-
const filteredPronounsList = _.filter(pronounsList, (pronous) => pronous.text.toLowerCase().indexOf(searchValue.trim().toLowerCase()) >= 0);
82-
83-
const headerMessage = searchValue.trim() && !filteredPronounsList.length ? props.translate('common.noResultsFound') : '';
84-
85-
useEffect(() => {
86-
setSearchValue(initiallyFocusedOption.text);
87-
}, [initiallyFocusedOption]);
88-
89-
useEffect(() => {
90-
onChangeText();
91-
loadPronouns();
92-
}, [loadPronouns]);
93-
9467
return (
9568
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
9669
<HeaderWithBackButton
97-
title={props.translate('pronounsPage.pronouns')}
70+
title={translate('pronounsPage.pronouns')}
9871
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
9972
/>
100-
<Text style={[styles.ph5, styles.mb3]}>{props.translate('pronounsPage.isShownOnProfile')}</Text>
101-
{/* Only render pronouns if list was loaded (not filtered list), otherwise initially focused item will be empty */}
102-
{pronounsList.length > 0 && (
103-
<SelectionList
104-
headerMessage={headerMessage}
105-
textInputLabel={props.translate('pronounsPage.pronouns')}
106-
textInputPlaceholder={props.translate('pronounsPage.placeholderText')}
107-
textInputValue={searchValue}
108-
sections={[{data: filteredPronounsList, indexOffset: 0}]}
109-
onSelectRow={updatePronouns}
110-
onChangeText={onChangeText}
111-
initiallyFocusedOptionKey={initiallyFocusedOption.keyForList}
112-
shouldDelayFocus
113-
/>
114-
)}
73+
<Text style={[styles.ph5, styles.mb3]}>{translate('pronounsPage.isShownOnProfile')}</Text>
74+
<SelectionList
75+
headerMessage={headerMessage}
76+
textInputLabel={translate('pronounsPage.pronouns')}
77+
textInputPlaceholder={translate('pronounsPage.placeholderText')}
78+
textInputValue={searchValue}
79+
sections={[{data: filteredPronounsList, indexOffset: 0}]}
80+
onSelectRow={updatePronouns}
81+
onChangeText={setSearchValue}
82+
initiallyFocusedOptionKey={currentPronounsKey}
83+
shouldDelayFocus
84+
/>
11585
</ScreenWrapper>
11686
);
11787
}
@@ -120,4 +90,4 @@ PronounsPage.propTypes = propTypes;
12090
PronounsPage.defaultProps = defaultProps;
12191
PronounsPage.displayName = 'PronounsPage';
12292

123-
export default compose(withLocalize, withCurrentUserPersonalDetails)(PronounsPage);
93+
export default withCurrentUserPersonalDetails(PronounsPage);

0 commit comments

Comments
 (0)