1
1
import _ from 'underscore' ;
2
2
import lodashGet from 'lodash/get' ;
3
- import React , { useState , useEffect , useCallback } from 'react' ;
3
+ import React , { useState , useMemo } from 'react' ;
4
4
import withCurrentUserPersonalDetails , { withCurrentUserPersonalDetailsPropTypes , withCurrentUserPersonalDetailsDefaultProps } from '../../../components/withCurrentUserPersonalDetails' ;
5
5
import ScreenWrapper from '../../../components/ScreenWrapper' ;
6
6
import HeaderWithBackButton from '../../../components/HeaderWithBackButton' ;
7
- import withLocalize , { withLocalizePropTypes } from '../../../components/withLocalize' ;
8
7
import Text from '../../../components/Text' ;
9
8
import styles from '../../../styles/styles' ;
10
9
import * as PersonalDetails from '../../../libs/actions/PersonalDetails' ;
11
- import compose from '../../../libs/compose' ;
12
10
import CONST from '../../../CONST' ;
13
11
import ROUTES from '../../../ROUTES' ;
14
12
import Navigation from '../../../libs/Navigation/Navigation' ;
15
13
import SelectionList from '../../../components/SelectionList' ;
14
+ import useLocalize from '../../../hooks/useLocalize' ;
16
15
17
16
const propTypes = {
18
- ...withLocalizePropTypes ,
19
17
...withCurrentUserPersonalDetailsPropTypes ,
20
18
} ;
21
19
22
20
const defaultProps = {
23
21
...withCurrentUserPersonalDetailsDefaultProps ,
24
22
} ;
25
23
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 ) ;
30
28
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
+ } ) ;
38
36
39
- const pronouns = _ . chain ( props . translate ( 'pronouns' ) )
37
+ const filteredPronounsList = useMemo ( ( ) => {
38
+ const pronouns = _ . chain ( translate ( 'pronouns' ) )
40
39
. map ( ( value , key ) => {
41
40
const fullPronounKey = `${ CONST . PRONOUNS . PREFIX } ${ key } ` ;
42
41
const isCurrentPronouns = fullPronounKey === currentPronouns ;
43
42
44
- if ( isCurrentPronouns ) {
45
- setInitiallyFocusedOption ( {
46
- text : value ,
47
- keyForList : key ,
48
- } ) ;
49
- }
50
-
51
43
return {
52
44
text : value ,
53
45
value : fullPronounKey ,
@@ -58,60 +50,38 @@ function PronounsPage(props) {
58
50
. sortBy ( ( pronoun ) => pronoun . text . toLowerCase ( ) )
59
51
. value ( ) ;
60
52
61
- setPronounsList ( pronouns ) ;
53
+ const trimmedSearch = searchValue . trim ( ) ;
62
54
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 ] ) ;
65
60
66
- const onChangeText = ( value = '' ) => {
67
- setSearchValue ( value ) ;
68
- } ;
61
+ const headerMessage = searchValue . trim ( ) && filteredPronounsList . length === 0 ? translate ( 'common.noResultsFound' ) : '' ;
69
62
70
- /**
71
- * @param {Object } selectedPronouns
72
- */
73
63
const updatePronouns = ( selectedPronouns ) => {
74
- PersonalDetails . updatePronouns ( selectedPronouns . keyForList === initiallyFocusedOption . keyForList ? '' : lodashGet ( selectedPronouns , 'value' , '' ) ) ;
64
+ PersonalDetails . updatePronouns ( selectedPronouns . keyForList === currentPronouns . keyForList ? '' : lodashGet ( selectedPronouns , 'value' , '' ) ) ;
75
65
} ;
76
66
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
-
94
67
return (
95
68
< ScreenWrapper includeSafeAreaPaddingBottom = { false } >
96
69
< HeaderWithBackButton
97
- title = { props . translate ( 'pronounsPage.pronouns' ) }
70
+ title = { translate ( 'pronounsPage.pronouns' ) }
98
71
onBackButtonPress = { ( ) => Navigation . goBack ( ROUTES . SETTINGS_PROFILE ) }
99
72
/>
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
+ />
115
85
</ ScreenWrapper >
116
86
) ;
117
87
}
@@ -120,4 +90,4 @@ PronounsPage.propTypes = propTypes;
120
90
PronounsPage . defaultProps = defaultProps ;
121
91
PronounsPage . displayName = 'PronounsPage' ;
122
92
123
- export default compose ( withLocalize , withCurrentUserPersonalDetails ) ( PronounsPage ) ;
93
+ export default withCurrentUserPersonalDetails ( PronounsPage ) ;
0 commit comments