Skip to content

Commit

Permalink
fix Taxpicker
Browse files Browse the repository at this point in the history
  • Loading branch information
teneeto committed Mar 25, 2024
1 parent 90fb9a1 commit 00d5cea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/components/TaxPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback, useMemo, useState} from 'react';
import React, {useMemo, useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import type {EdgeInsets} from 'react-native-safe-area-context';
Expand Down Expand Up @@ -47,21 +47,19 @@ function TaxPicker({selectedTaxRate = '', policy, insets, onSubmit}: TaxPickerPr

const shouldShowTextInput = !isTaxRatesCountBelowThreshold;

const getTaxName = useCallback((key: string) => taxRates?.taxes[key]?.name, [taxRates?.taxes]);

const selectedOptions = useMemo(() => {
if (!selectedTaxRate) {
return [];
}

return [
{
name: getTaxName(selectedTaxRate),
name: selectedTaxRate,
enabled: true,
accountID: null,
},
];
}, [selectedTaxRate, getTaxName]);
}, [selectedTaxRate]);

const sections = useMemo(
() => OptionsListUtils.getTaxRatesSection(taxRates, selectedOptions as OptionsListUtils.Category[], searchValue, selectedTaxRate),
Expand All @@ -70,18 +68,20 @@ function TaxPicker({selectedTaxRate = '', policy, insets, onSubmit}: TaxPickerPr

const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList(sections[0].data.length > 0, searchValue);

const selectedOptionKey = useMemo(() => sections?.[0]?.data?.find((taxRate) => taxRate.searchText === selectedTaxRate)?.keyForList, [sections, selectedTaxRate]);

return (
<SelectionList
ListItem={RadioListItem}
onSelectRow={onSubmit}
initiallyFocusedOptionKey={selectedTaxRate}
sections={sections}
containerStyle={{paddingBottom: StyleUtils.getSafeAreaMargins(insets).marginBottom}}
textInputLabel={shouldShowTextInput ? translate('common.search') : undefined}
isRowMultilineSupported
headerMessage={headerMessage}
textInputValue={searchValue}
textInputLabel={shouldShowTextInput ? translate('common.search') : undefined}
onChangeText={setSearchValue}
onSelectRow={onSubmit}
ListItem={RadioListItem}
initiallyFocusedOptionKey={selectedOptionKey ?? undefined}
isRowMultilineSupported
containerStyle={{paddingBottom: StyleUtils.getSafeAreaMargins(insets).marginBottom}}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ function sortTaxRates(taxRates: TaxRates): TaxRate[] {
function getTaxRatesOptions(taxRates: Array<Partial<TaxRate>>): Option[] {
return taxRates.map((taxRate) => ({
text: taxRate.modifiedName,
keyForList: taxRate.code,
keyForList: taxRate.modifiedName,
searchText: taxRate.modifiedName,
tooltipText: taxRate.modifiedName,
isDisabled: taxRate.isDisabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {setForeignCurrencyDefault} from '@libs/actions/Policy';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as TransactionUtils from '@libs/TransactionUtils';
import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper';
import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper';
import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper';
Expand All @@ -31,6 +32,8 @@ function WorkspaceTaxesSettingsForeignCurrency({
const {translate} = useLocalize();
const styles = useThemeStyles();

const selectedTaxRate = TransactionUtils.getTaxName(policy?.taxRates?.taxes ?? {}, policy?.taxRates?.foreignTaxDefault ?? '');

const submit = ({keyForList}: ListItem) => {
setForeignCurrencyDefault(policyID, keyForList ?? '');
Navigation.goBack(ROUTES.WORKSPACE_TAXES_SETTINGS.getRoute(policyID));
Expand All @@ -55,7 +58,7 @@ function WorkspaceTaxesSettingsForeignCurrency({

<View style={[styles.mb4, styles.flex1]}>
<TaxPicker
selectedTaxRate={policy?.taxRates?.foreignTaxDefault}
selectedTaxRate={selectedTaxRate}
insets={insets}
onSubmit={submit}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {setWorkspaceCurrencyDefault} from '@libs/actions/Policy';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as TransactionUtils from '@libs/TransactionUtils';
import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper';
import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper';
import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper';
Expand All @@ -31,6 +32,7 @@ function WorkspaceTaxesSettingsWorkspaceCurrency({
const {translate} = useLocalize();
const styles = useThemeStyles();

const selectedTaxRate = TransactionUtils.getTaxName(policy?.taxRates?.taxes ?? {}, policy?.taxRates?.foreignTaxDefault ?? '');
const submit = ({keyForList}: ListItem) => {
setWorkspaceCurrencyDefault(policyID, keyForList ?? '');
Navigation.goBack(ROUTES.WORKSPACE_TAXES_SETTINGS.getRoute(policyID));
Expand All @@ -55,7 +57,7 @@ function WorkspaceTaxesSettingsWorkspaceCurrency({

<View style={[styles.mb4, styles.flex1]}>
<TaxPicker
selectedTaxRate={policy?.taxRates?.defaultExternalID}
selectedTaxRate={selectedTaxRate}
insets={insets}
onSubmit={submit}
/>
Expand Down

0 comments on commit 00d5cea

Please sign in to comment.