Repository navigation
Refactor IOUCurrencySelection class to use hooks #20579
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
Changes from all commits
cb9cdab
6491d6e
daf3b25
14b0a3c
3416326
02d0522
025bf9b
a020c99
5d9143c
5e2ea3b
0f0ea5b
7248544
0f41718
9012bae
2f91a6d
9febc2f
ba01429
b74c0aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React, {Component} from 'react'; | ||
| import React, {useState, useMemo, useCallback} from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import {withOnyx} from 'react-native-onyx'; | ||
| import _ from 'underscore'; | ||
|
|
@@ -53,50 +53,29 @@ const defaultProps = { | |
| }, | ||
| }; | ||
|
|
||
| class IOUCurrencySelection extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| searchValue: '', | ||
| currencyData: this.getCurrencyOptions(this.props.currencyList), | ||
| }; | ||
| this.getCurrencyOptions = this.getCurrencyOptions.bind(this); | ||
| this.getSections = this.getSections.bind(this); | ||
| this.confirmCurrencySelection = this.confirmCurrencySelection.bind(this); | ||
| this.changeSearchValue = this.changeSearchValue.bind(this); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the sections needed for the OptionsSelector | ||
| * | ||
| * @returns {Array} | ||
| */ | ||
| getSections() { | ||
| if (this.state.searchValue.trim() && !this.state.currencyData.length) { | ||
| return []; | ||
| } | ||
| const sections = []; | ||
| sections.push({ | ||
| title: this.props.translate('iOUCurrencySelection.allCurrencies'), | ||
| data: this.state.currencyData, | ||
| shouldShow: true, | ||
| indexOffset: 0, | ||
| }); | ||
|
|
||
| return sections; | ||
| } | ||
|
|
||
| getSelectedCurrencyCode() { | ||
| return lodashGet(this.props.route, 'params.currency', this.props.iou.currency); | ||
| } | ||
|
|
||
| /** | ||
| * @returns {Object} | ||
| */ | ||
| getCurrencyOptions() { | ||
| return _.map(this.props.currencyList, (currencyInfo, currencyCode) => { | ||
| const isSelectedCurrency = currencyCode === this.getSelectedCurrencyCode(); | ||
| function IOUCurrencySelection(props) { | ||
| const [searchValue, setSearchValue] = useState(''); | ||
| const selectedCurrencyCode = lodashGet(props.route, 'params.currency', props.iou.currency, CONST.CURRENCY.USD); | ||
|
|
||
| const confirmCurrencySelection = useCallback( | ||
| (option) => { | ||
| const backTo = lodashGet(props.route, 'params.backTo', ''); | ||
| // When we refresh the web, the money request route gets cleared from the navigation stack. | ||
| // Navigating to "backTo" will result in forward navigation instead, causing disruption to the currency selection. | ||
| // To prevent any negative experience, we have made the decision to simply close the currency selection page. | ||
| if (_.isEmpty(backTo) || props.navigation.getState().routes.length === 1) { | ||
| Navigation.goBack(); | ||
| } else { | ||
| Navigation.navigate(`${props.route.params.backTo}?currency=${option.currencyCode}`); | ||
| } | ||
| }, | ||
| [props.route, props.navigation], | ||
| ); | ||
|
|
||
| const {translate, currencyList} = props; | ||
| const {sections, headerMessage, initiallyFocusedOptionKey} = useMemo(() => { | ||
| const currencyOptions = _.map(currencyList, (currencyInfo, currencyCode) => { | ||
| const isSelectedCurrency = currencyCode === selectedCurrencyCode; | ||
| return { | ||
| text: `${currencyCode} - ${CurrencyUtils.getLocalizedCurrencySymbol(currencyCode)}`, | ||
| currencyCode, | ||
|
|
@@ -105,75 +84,56 @@ class IOUCurrencySelection extends Component { | |
| boldStyle: isSelectedCurrency, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Sets new search value | ||
| * @param {String} searchValue | ||
| * @return {void} | ||
| */ | ||
| changeSearchValue(searchValue) { | ||
| const currencyOptions = this.getCurrencyOptions(this.props.currencyList); | ||
| const searchRegex = new RegExp(Str.escapeForRegExp(searchValue), 'i'); | ||
| const filteredCurrencies = _.filter(currencyOptions, (currencyOption) => searchRegex.test(currencyOption.text)); | ||
|
|
||
| this.setState({ | ||
| searchValue, | ||
| currencyData: filteredCurrencies, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Confirms the selection of currency | ||
| * | ||
| * @param {Object} option | ||
| * @param {String} option.currencyCode | ||
| */ | ||
| confirmCurrencySelection(option) { | ||
| const backTo = lodashGet(this.props.route, 'params.backTo', ''); | ||
| // When we refresh the web, the money request route gets cleared from the navigation stack. | ||
| // Navigating to "backTo" will result in forward navigation instead, causing disruption to the currency selection. | ||
| // To prevent any negative experience, we have made the decision to simply close the currency selection page. | ||
| if (_.isEmpty(backTo) || this.props.navigation.getState().routes.length === 1) { | ||
| Navigation.goBack(); | ||
| } else { | ||
| Navigation.navigate(`${this.props.route.params.backTo}?currency=${option.currencyCode}`); | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| const headerMessage = this.state.searchValue.trim() && !this.state.currencyData.length ? this.props.translate('common.noResultsFound') : ''; | ||
| const iouType = lodashGet(this.props.route, 'params.iouType', CONST.IOU.MONEY_REQUEST_TYPE.REQUEST); | ||
| const reportID = lodashGet(this.props.route, 'params.reportID', ''); | ||
| return ( | ||
| <ScreenWrapper includeSafeAreaPaddingBottom={false}> | ||
| {({safeAreaPaddingBottomStyle}) => ( | ||
| <> | ||
| <HeaderWithBackButton | ||
| title={this.props.translate('iOUCurrencySelection.selectCurrency')} | ||
| onBackButtonPress={() => Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID))} | ||
| /> | ||
| <OptionsSelector | ||
| sections={this.getSections()} | ||
| onSelectRow={this.confirmCurrencySelection} | ||
| value={this.state.searchValue} | ||
| onChangeText={this.changeSearchValue} | ||
| textInputLabel={this.props.translate('common.search')} | ||
| headerMessage={headerMessage} | ||
| safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle} | ||
| initiallyFocusedOptionKey={_.get( | ||
| _.find(this.state.currencyData, (currency) => currency.currencyCode === this.getSelectedCurrencyCode()), | ||
| 'keyForList', | ||
| )} | ||
| shouldHaveOptionSeparator | ||
| /> | ||
| </> | ||
| )} | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
| const isEmpty = searchValue.trim() && !filteredCurrencies.length; | ||
|
|
||
| return { | ||
| initiallyFocusedOptionKey: _.get( | ||
| _.find(filteredCurrencies, (currency) => currency.currencyCode === selectedCurrencyCode), | ||
| 'keyForList', | ||
| ), | ||
| sections: isEmpty | ||
| ? [] | ||
| : [ | ||
| { | ||
| title: translate('iOUCurrencySelection.allCurrencies'), | ||
| data: filteredCurrencies, | ||
| shouldShow: true, | ||
| indexOffset: 0, | ||
| }, | ||
| ], | ||
| headerMessage: isEmpty ? translate('common.noResultsFound') : '', | ||
| }; | ||
| }, [currencyList, searchValue, selectedCurrencyCode, translate]); | ||
|
|
||
| return ( | ||
| <ScreenWrapper includeSafeAreaPaddingBottom={false}> | ||
| {({safeAreaPaddingBottomStyle}) => ( | ||
| <> | ||
| <HeaderWithBackButton | ||
| title={translate('iOUCurrencySelection.selectCurrency')} | ||
| onBackButtonPress={() => Navigation.goBack(ROUTES.getIouRequestRoute(Navigation.getTopmostReportId()))} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dangrous I think it should be : onBackButtonPress={() => Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID))}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! That's what I ended up going with - https://github.com/Expensify/App/pull/22135/files#diff-496700da68574808c5981ac4884e39d73ea02df4aa59bd5869e0c9227defe4c2R119 - it's weird, I looked through every commit in this PR and it never actually changed away from that in any of them. But it ends up being different in the end. I'm not sure how that happened
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe it's merging main into this branch, This PR was created while another PR that refactors the IOU modal is open, At some point the other PR was merged with new code, and I think upon merging the main we didn’t accept the new changes.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh that makes sense.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks both, Yeah my first guess was the merge as I recall some conflicts. I must have missed one when resolving. |
||
| /> | ||
| <OptionsSelector | ||
| sections={sections} | ||
| onSelectRow={confirmCurrencySelection} | ||
| value={searchValue} | ||
| onChangeText={setSearchValue} | ||
| textInputLabel={translate('common.search')} | ||
| headerMessage={headerMessage} | ||
| safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle} | ||
| initiallyFocusedOptionKey={initiallyFocusedOptionKey} | ||
| shouldHaveOptionSeparator | ||
| /> | ||
| </> | ||
| )} | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
|
|
||
| IOUCurrencySelection.displayName = 'IOUCurrencySelection'; | ||
| IOUCurrencySelection.propTypes = propTypes; | ||
| IOUCurrencySelection.defaultProps = defaultProps; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.