Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,14 +1333,15 @@ function sortTaxRates(taxRates: TaxRates): TaxRate[] {
/**
* Builds the options for taxRates
*/
function getTaxRatesOptions(taxRates: Array<Partial<TaxRate>>): TaxRatesOption[] {
return taxRates.map((taxRate) => ({
function getTaxRatesOptions(taxRates: Array<Partial<TaxRate & {isSelected: boolean}>>): TaxRatesOption[] {
return taxRates.map(({isSelected, ...taxRate}) => ({
text: taxRate.modifiedName,
keyForList: taxRate.modifiedName,
searchText: taxRate.modifiedName,
tooltipText: taxRate.modifiedName,
isDisabled: taxRate.isDisabled,
data: taxRate,
isSelected: !!isSelected,
}));
}

Expand All @@ -1353,7 +1354,9 @@ function getTaxRatesSection(taxRates: TaxRatesWithDefault | undefined, selectedO
const taxes = transformedTaxRates(taxRates);

const sortedTaxRates = sortTaxRates(taxes);
const enabledTaxRates = sortedTaxRates.filter((taxRate) => !taxRate.isDisabled);
const enabledTaxRates = sortedTaxRates
.filter((taxRate) => !taxRate.isDisabled)
.map((taxRate) => ({...taxRate, isSelected: selectedOptions.some((option) => option.name === taxRate.modifiedName)}));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we compare id's here instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MonilBhavsar we don't have id in selectedOptions as we constructed it only from selectedTax here

const selectedOptions = useMemo(() => {
if (!selectedTaxRate) {
return [];
}
return [
{
name: selectedTaxRate,
enabled: true,
accountID: null,
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add taxCode there and compare it?

@FitseTLT FitseTLT May 2, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MonilBhavsar there are different places we use tax picker and not straightforward to get taxCode for instance here

const moneyRequestSelectedTaxRate = transaction?.taxRate?.keyForList || (taxRates && TransactionUtils.getDefaultTaxName(taxRates));
const editingSelectedTaxRate =

there is not taxCode in transaction.taxRate (for creating request in confirmation page)

{
  "text": "Tax 3 (3%) • Default",
  "keyForList": "Tax 3 (3%) • Default",
  "searchText": "Tax 3 (3%) • Default",
  "tooltipText": "Tax 3 (3%) • Default",
  "isDisabled": false,
  "data": {
    "modifiedName": "Tax 3 (3%) • Default",
    "isDisabled": false
  },
  "isSelected": true
}

Maybe we need some other refactoring pr and I don't think it would be appropriate to handle in the scope of this pr, in terms of what we are trying to fix here

const numberOfTaxRates = enabledTaxRates.length;

// If all tax are disabled but there's a previously selected tag, show only the selected tag
Expand All @@ -1362,6 +1365,7 @@ function getTaxRatesSection(taxRates: TaxRatesWithDefault | undefined, selectedO
modifiedName: option.name,
// Should be marked as enabled to be able to be de-selected
isDisabled: false,
isSelected: true,
}));
policyRatesSections.push({
// "Selected" sectiong
Expand Down Expand Up @@ -1406,6 +1410,7 @@ function getTaxRatesSection(taxRates: TaxRatesWithDefault | undefined, selectedO

return {
modifiedName: option.name,
isSelected: true,
isDisabled: !!taxRateObject?.isDisabled,
};
});
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/OptionsListUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,7 @@ describe('OptionsListUtils', () => {
keyForList: 'Tax exempt 1 (0%) • Default',
searchText: 'Tax exempt 1 (0%) • Default',
tooltipText: 'Tax exempt 1 (0%) • Default',
isSelected: false,
isDisabled: undefined,
// creates a data option.
data: {
Expand All @@ -2600,6 +2601,7 @@ describe('OptionsListUtils', () => {
keyForList: 'Tax option 3 (5%)',
searchText: 'Tax option 3 (5%)',
tooltipText: 'Tax option 3 (5%)',
isSelected: false,
isDisabled: undefined,
data: {
name: 'Tax option 3',
Expand All @@ -2614,6 +2616,7 @@ describe('OptionsListUtils', () => {
searchText: 'Tax rate 2 (3%)',
tooltipText: 'Tax rate 2 (3%)',
isDisabled: undefined,
isSelected: false,
data: {
name: 'Tax rate 2',
code: 'CODE2',
Expand All @@ -2637,6 +2640,7 @@ describe('OptionsListUtils', () => {
searchText: 'Tax rate 2 (3%)',
tooltipText: 'Tax rate 2 (3%)',
isDisabled: undefined,
isSelected: false,
data: {
name: 'Tax rate 2',
code: 'CODE2',
Expand Down