1- import React , { useEffect , useMemo , useRef , useState } from 'react' ;
1+ import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
22import CollapsibleHeaderOnKeyboard from '@components/CollapsibleHeaderOnKeyboard' ;
33import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator' ;
44import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
@@ -31,7 +31,7 @@ function PronounsPage({currentUserPersonalDetails}: PronounsPageProps) {
3131 const currentPronouns = currentUserPersonalDetails ?. pronouns ?? '' ;
3232 const currentPronounsKey = currentPronouns . substring ( CONST . PRONOUNS . PREFIX . length ) ;
3333 const [ searchValue , setSearchValue ] = useState ( '' ) ;
34- const isOptionSelected = useRef ( false ) ;
34+ const [ selectedPronouns , setSelectedPronouns ] = useState ( currentPronouns ) ;
3535 const currentUserAccountID = currentUserPersonalDetails ?. accountID ?? CONST . DEFAULT_NUMBER_ID ;
3636
3737 useEffect ( ( ) => {
@@ -41,6 +41,7 @@ function PronounsPage({currentUserPersonalDetails}: PronounsPageProps) {
4141 const currentPronounsText = CONST . PRONOUNS_LIST . find ( ( value ) => value === currentPronounsKey ) ;
4242
4343 setSearchValue ( currentPronounsText ? translate ( `pronouns.${ currentPronounsText } ` ) : '' ) ;
44+ setSelectedPronouns ( currentPronouns ) ;
4445
4546 // Only need to update search value when the first time the data is loaded
4647 // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -49,13 +50,12 @@ function PronounsPage({currentUserPersonalDetails}: PronounsPageProps) {
4950 const filteredPronounsList = useMemo ( ( ) : PronounEntry [ ] => {
5051 const pronouns = CONST . PRONOUNS_LIST . map ( ( value ) => {
5152 const fullPronounKey = `${ CONST . PRONOUNS . PREFIX } ${ value } ` ;
52- const isCurrentPronouns = fullPronounKey === currentPronouns ;
5353
5454 return {
5555 text : translate ( `pronouns.${ value } ` ) ,
5656 value : fullPronounKey ,
5757 keyForList : value ,
58- isSelected : isCurrentPronouns ,
58+ isSelected : fullPronounKey === selectedPronouns ,
5959 } ;
6060 } ) . sort ( ( a , b ) => localeCompare ( a . text . toLowerCase ( ) , b . text . toLowerCase ( ) ) ) ;
6161
@@ -65,17 +65,26 @@ function PronounsPage({currentUserPersonalDetails}: PronounsPageProps) {
6565 return [ ] ;
6666 }
6767 return pronouns . filter ( ( pronoun ) => pronoun . text . toLowerCase ( ) . indexOf ( trimmedSearch . toLowerCase ( ) ) >= 0 ) ;
68- } , [ searchValue , currentPronouns , translate , localeCompare ] ) ;
68+ } , [ searchValue , selectedPronouns , translate , localeCompare ] ) ;
6969
70- const updatePronouns = ( selectedPronouns : PronounEntry ) => {
71- if ( isOptionSelected . current ) {
72- return ;
73- }
74- isOptionSelected . current = true ;
75- updatePronounsPersonalDetails ( selectedPronouns . keyForList === currentPronounsKey ? '' : ( selectedPronouns ?. value ?? '' ) , currentUserAccountID ) ;
76- Navigation . goBack ( ) ;
70+ const selectPronoun = ( selectedPronoun : PronounEntry ) => {
71+ setSelectedPronouns ( selectedPronoun . value === selectedPronouns ? '' : ( selectedPronoun ?. value ?? '' ) ) ;
7772 } ;
7873
74+ const savePronouns = useCallback ( ( ) => {
75+ updatePronounsPersonalDetails ( selectedPronouns , currentUserAccountID ) ;
76+ Navigation . goBack ( ) ;
77+ } , [ selectedPronouns , currentUserAccountID ] ) ;
78+
79+ const confirmButtonOptions = useMemo (
80+ ( ) => ( {
81+ showButton : true ,
82+ text : translate ( 'common.save' ) ,
83+ onConfirm : savePronouns ,
84+ } ) ,
85+ [ savePronouns , translate ] ,
86+ ) ;
87+
7988 const textInputOptions = useMemo (
8089 ( ) => ( {
8190 label : translate ( 'pronounsPage.pronouns' ) ,
@@ -107,9 +116,10 @@ function PronounsPage({currentUserPersonalDetails}: PronounsPageProps) {
107116 < SelectionList
108117 data = { filteredPronounsList }
109118 ListItem = { SingleSelectListItem }
110- onSelectRow = { updatePronouns }
119+ onSelectRow = { selectPronoun }
111120 textInputOptions = { textInputOptions }
112121 initiallyFocusedItemKey = { currentPronounsKey }
122+ confirmButtonOptions = { confirmButtonOptions }
113123 shouldSingleExecuteRowSelect
114124 />
115125 </ >
0 commit comments