Skip to content

Commit

Permalink
fix taxRate selection bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymczakJ committed Aug 12, 2024
1 parent 3c0b0cf commit 190e44f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModa
);
const {status} = queryJSON;
const headerSubtitle = isCustomQuery ? SearchUtils.getSearchHeaderTitle(queryJSON) : translate(headerContent[status]?.titleTx);
console.log('%%%%%\n', 'headerSubtitle', headerSubtitle);
const headerTitle = isCustomQuery ? translate('search.filtersHeader') : '';
const headerIcon = isCustomQuery ? Illustrations.Filters : headerContent[status]?.icon;

Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchMultipleSelectionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SelectableListItem from './SelectionList/SelectableListItem';

type SearchMultipleSelectionPickerItem = {
name: string;
value: string;
value: string | string[];
};

type SearchMultipleSelectionPickerProps = {
Expand Down
15 changes: 10 additions & 5 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type {
PolicyConnectionSyncProgress,
PolicyFeatureName,
Rate,
TaxRates,
Tenant,
} from '@src/types/onyx/Policy';
import type PolicyEmployee from '@src/types/onyx/PolicyEmployee';
Expand Down Expand Up @@ -423,14 +422,20 @@ function getTaxByID(policy: OnyxEntry<Policy>, taxID: string): TaxRate | undefin
return policy?.taxRates?.taxes?.[taxID];
}

function getAllTaxRates(): Record<string, string> {
const allTaxRates: Record<string, string> = {};
/** Get a tax rate object built like Record<TaxRateName, RelatedTaxRateKeys>.
* We want to allow user to choose over TaxRateName and there might be a situation when on TaxRateName has two possible keys in different policies */
function getAllTaxRatesNamesAndKeys(): Record<string, string[]> {
const allTaxRates: Record<string, string[]> = {};
Object.values(allPolicies ?? {})?.forEach((policy) => {
if (!policy?.taxRates?.taxes) {
return;
}
Object.entries(policy?.taxRates?.taxes).forEach(([taxRateKey, taxRate]) => {
allTaxRates[taxRateKey] = taxRate.name;
if (!allTaxRates[taxRate.name]) {
allTaxRates[taxRate.name] = [taxRateKey];
return;
}
allTaxRates[taxRate.name].push(taxRateKey);
});
});
return allTaxRates;
Expand Down Expand Up @@ -1019,7 +1024,7 @@ export {
getCurrentTaxID,
areSettingsInErrorFields,
settingsPendingAction,
getAllTaxRates,
getAllTaxRatesNamesAndKeys as getAllTaxRates,
getTagNamesFromTagsLists,
};

Expand Down
2 changes: 1 addition & 1 deletion src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ function buildQueryStringFromFilters(filterValues: Partial<SearchAdvancedFilters
const filterValueArray = filterValues[filterKey] ?? [];
const keyInCorrectForm = (Object.keys(CONST.SEARCH.SYNTAX_FILTER_KEYS) as KeysOfFilterKeysObject[]).find((key) => CONST.SEARCH.SYNTAX_FILTER_KEYS[key] === filterKey);
if (keyInCorrectForm) {
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS[keyInCorrectForm]}:${filterValueArray.map(sanitizeString).join(', ')}`;
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS[keyInCorrectForm]}:${filterValueArray.map(sanitizeString).join(',')}`;
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/pages/Search/SearchFiltersTaxRatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import SearchMultipleSelectionPicker from '@components/SearchMultipleSelectionPicker';
import type {SearchMultipleSelectionPickerItem} from '@components/SearchMultipleSelectionPicker';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
Expand All @@ -18,16 +19,24 @@ function SearchFiltersTaxRatePage() {

const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM);
const allTaxRates = getAllTaxRates();
const selectedTaxesItems = searchAdvancedFiltersForm?.taxRate?.map((taxRate) => ({name: allTaxRates[taxRate], value: taxRate}));
const selectedTaxesItems: SearchMultipleSelectionPickerItem[] = [];
Object.entries(allTaxRates).forEach(([taxRateName, taxRateKeys]) => {
searchAdvancedFiltersForm?.taxRate.forEach((taxRateKey) => {
if (!taxRateKeys.includes(taxRateKey) || selectedTaxesItems.some((item) => item.name === taxRateName)) {
return;
}
selectedTaxesItems.push({name: taxRateName, value: taxRateKeys});
});
});
const policyID = searchAdvancedFiltersForm?.policyID ?? '-1';
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const singlePolicyTaxRates = policy?.taxRates?.taxes;

const taxItems = useMemo(() => {
if (!singlePolicyTaxRates) {
return Object.entries(allTaxRates).map(([taxRatekey, taxRate]) => ({name: taxRate, value: taxRatekey}));
return Object.entries(allTaxRates).map(([taxRateName, taxRateKeys]) => ({name: taxRateName, value: taxRateKeys}));
}
return Object.entries(singlePolicyTaxRates).map(([taxRatekey, taxRate]) => ({name: taxRate.name, value: taxRatekey}));
return Object.entries(singlePolicyTaxRates).map(([taxRatekey, taxRate]) => ({name: taxRate.name, value: [taxRatekey]}));
}, [allTaxRates, singlePolicyTaxRates]);

const updateTaxRateFilters = useCallback((values: string[]) => SearchActions.updateAdvancedFilters({taxRate: values}), []);
Expand Down

0 comments on commit 190e44f

Please sign in to comment.