Skip to content

Commit

Permalink
fix: [Track tax] Disabled tax rate can be selected.
Browse files Browse the repository at this point in the history
Signed-off-by: Krishna Gupta <[email protected]>
  • Loading branch information
Krishna2323 committed Jun 19, 2024
1 parent e033314 commit 46c0a51
Showing 1 changed file with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useMemo} from 'react';
import React from 'react';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import TaxPicker from '@components/TaxPicker';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type * as OptionsListUtils from '@libs/OptionsListUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyOnyxProps} from '@pages/workspace/withPolicy';
Expand All @@ -15,15 +16,6 @@ import * as DistanceRate from '@userActions/Policy/DistanceRate';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {PendingAction} from '@src/types/onyx/OnyxCommon';

type ListItemType = {
value: string;
text: string;
isSelected: boolean;
keyForList: string;
pendingAction?: PendingAction;
};

type PolicyDistanceRateTaxRateEditPageProps = WithPolicyOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DISTANCE_RATE_TAX_RATE_EDIT>;

Expand All @@ -36,21 +28,10 @@ function PolicyDistanceRateTaxRateEditPage({route, policy}: PolicyDistanceRateTa
const customUnit = customUnits[Object.keys(customUnits)[0]];
const rate = customUnit?.rates[rateID];
const taxRateExternalID = rate.attributes?.taxRateExternalID;
const taxRateItems: ListItemType[] = useMemo(() => {
const taxes = policy?.taxRates?.taxes;
const result = Object.entries(taxes ?? {}).map(([key, value]) => ({
value: key,
text: `${value.name} (${value.value})`,
isSelected: taxRateExternalID === key,
keyForList: key,
pendingAction: value.pendingAction,
isDisabled: value.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
}));
return result;
}, [policy, taxRateExternalID]);
const selectedTaxRate = TransactionUtils.getWorkspaceTaxesSettingsName(policy, taxRateExternalID ?? '');

const onTaxRateChange = (newTaxRate: ListItemType) => {
if (taxRateExternalID === newTaxRate.value) {
const onTaxRateChange = (newTaxRate: OptionsListUtils.TaxRatesOption) => {
if (taxRateExternalID === newTaxRate.code) {
Navigation.goBack();
return;
}
Expand All @@ -59,13 +40,17 @@ function PolicyDistanceRateTaxRateEditPage({route, policy}: PolicyDistanceRateTa
...rate,
attributes: {
...rate.attributes,
taxRateExternalID: newTaxRate.value,
taxRateExternalID: newTaxRate.code,
},
},
]);
Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_DETAILS.getRoute(policyID, rateID));
};

const dismiss = () => {
Navigation.goBack(ROUTES.WORKSPACE_TAXES_SETTINGS.getRoute(policyID));
};

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
Expand All @@ -78,16 +63,21 @@ function PolicyDistanceRateTaxRateEditPage({route, policy}: PolicyDistanceRateTa
shouldEnableMaxHeight
testID={PolicyDistanceRateTaxRateEditPage.displayName}
>
<HeaderWithBackButton
title={translate('workspace.taxes.taxRate')}
shouldShowBackButton
/>
<SelectionList
sections={[{data: taxRateItems}]}
ListItem={RadioListItem}
onSelectRow={onTaxRateChange}
initiallyFocusedOptionKey={taxRateItems.find((item) => item.isSelected)?.keyForList}
/>
{({insets}) => (
<>
<HeaderWithBackButton
title={translate('workspace.taxes.taxRate')}
shouldShowBackButton
/>
<TaxPicker
selectedTaxRate={selectedTaxRate}
policyID={policyID}
insets={insets}
onSubmit={onTaxRateChange}
onDismiss={dismiss}
/>
</>
)}
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
Expand Down

0 comments on commit 46c0a51

Please sign in to comment.