Skip to content

Commit 78cf216

Browse files
authored
Merge pull request Expensify#67047 from shubham1206agra/refactor-onyx-5
Refactored localeCompare in CategoryOptionListUtils
2 parents b48e6ed + 96cfcbd commit 78cf216

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/components/CategoryPicker.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function CategoryPicker({selectedCategory, policyID, onSubmit, addBottomSafeArea
3333
const [policyRecentlyUsedCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`, {canBeMissing: true});
3434
const {isOffline} = useNetwork();
3535

36-
const {translate} = useLocalize();
36+
const {translate, localeCompare} = useLocalize();
3737
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
3838
const offlineMessage = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';
3939

@@ -58,6 +58,7 @@ function CategoryPicker({selectedCategory, policyID, onSubmit, addBottomSafeArea
5858
searchValue: debouncedSearchValue,
5959
selectedOptions,
6060
categories,
61+
localeCompare,
6162
recentlyUsedCategories: validPolicyRecentlyUsedCategories,
6263
});
6364

@@ -68,7 +69,7 @@ function CategoryPicker({selectedCategory, policyID, onSubmit, addBottomSafeArea
6869
const showInput = !isCategoriesCountBelowThreshold;
6970

7071
return [categoryOptions, header, showInput];
71-
}, [policyRecentlyUsedCategories, debouncedSearchValue, selectedOptions, policyCategories, policyCategoriesDraft]);
72+
}, [policyRecentlyUsedCategories, debouncedSearchValue, selectedOptions, policyCategories, policyCategoriesDraft, localeCompare]);
7273

7374
const selectedOptionKey = useMemo(() => (sections?.at(0)?.data ?? []).filter((category) => category.searchText === selectedCategory).at(0)?.keyForList, [sections, selectedCategory]);
7475

src/libs/CategoryOptionListUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// eslint-disable-next-line you-dont-need-lodash-underscore/get
22
import lodashGet from 'lodash/get';
33
import lodashSet from 'lodash/set';
4+
import type {LocaleContextProps} from '@components/LocaleContextProvider';
45
import CONST from '@src/CONST';
56
import type {PolicyCategories} from '@src/types/onyx';
67
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
78
import {isEmptyObject} from '@src/types/utils/EmptyObject';
89
import times from '@src/utils/times';
9-
import localeCompare from './LocaleCompare';
1010
import {translateLocal} from './Localize';
1111
import type {OptionTree, SectionBase} from './OptionsListUtils';
1212
import tokenizedSearch from './tokenizedSearch';
@@ -85,18 +85,20 @@ function getCategoryOptionTree(options: Record<string, Category> | Category[], i
8585
*/
8686
function getCategoryListSections({
8787
categories,
88+
localeCompare,
8889
searchValue,
8990
selectedOptions = [],
9091
recentlyUsedCategories = [],
9192
maxRecentReportsToShow = CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
9293
}: {
9394
categories: PolicyCategories;
95+
localeCompare: LocaleContextProps['localeCompare'];
9496
selectedOptions?: Category[];
9597
searchValue?: string;
9698
recentlyUsedCategories?: string[];
9799
maxRecentReportsToShow?: number;
98100
}): CategoryTreeSection[] {
99-
const sortedCategories = sortCategories(categories);
101+
const sortedCategories = sortCategories(categories, localeCompare);
100102
const enabledCategories = Object.values(sortedCategories).filter((category) => category.enabled);
101103
const enabledCategoriesNames = enabledCategories.map((category) => category.name);
102104
const selectedOptionsWithDisabledState: Category[] = [];
@@ -212,7 +214,7 @@ function getCategoryListSections({
212214
* It builds an hierarchy (based on an object), where each category has a name and other keys as subcategories.
213215
* Via the hierarchy we avoid duplicating and sort categories one by one. Subcategories are being sorted alphabetically.
214216
*/
215-
function sortCategories(categories: Record<string, Category>): Category[] {
217+
function sortCategories(categories: Record<string, Category>, localeCompare: LocaleContextProps['localeCompare']): Category[] {
216218
// Sorts categories alphabetically by name.
217219
const sortedCategories = Object.values(categories).sort((a, b) => localeCompare(a.name, b.name));
218220

tests/unit/CategoryOptionListUtilsTest.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import CONST from '@src/CONST';
55
import IntlStore from '@src/languages/IntlStore';
66
import type {PolicyCategories} from '@src/types/onyx';
77
import type {PendingAction} from '@src/types/onyx/OnyxCommon';
8+
import {localeCompare} from '../utils/TestHelper';
89
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
910

1011
describe('CategoryOptionListUtils', () => {
@@ -509,24 +510,26 @@ describe('CategoryOptionListUtils', () => {
509510

510511
const smallResult = getCategoryListSections({
511512
searchValue: emptySearch,
513+
localeCompare,
512514
categories: smallCategoriesList,
513515
});
514516
expect(smallResult).toStrictEqual(smallResultList);
515517

516-
const smallSearchResult = getCategoryListSections({searchValue: search, categories: smallCategoriesList});
518+
const smallSearchResult = getCategoryListSections({searchValue: search, categories: smallCategoriesList, localeCompare});
517519
expect(smallSearchResult).toStrictEqual(smallSearchResultList);
518520

519-
const smallWrongSearchResult = getCategoryListSections({searchValue: wrongSearch, categories: smallCategoriesList});
521+
const smallWrongSearchResult = getCategoryListSections({searchValue: wrongSearch, categories: smallCategoriesList, localeCompare});
520522
expect(smallWrongSearchResult).toStrictEqual(smallWrongSearchResultList);
521523

522-
const employeeSearchResult = getCategoryListSections({searchValue: employeeSearch, categories: smallCategoriesList});
524+
const employeeSearchResult = getCategoryListSections({searchValue: employeeSearch, categories: smallCategoriesList, localeCompare});
523525
expect(employeeSearchResult).toStrictEqual(employeeSearchResultList);
524526

525527
const largeResult = getCategoryListSections({
526528
searchValue: emptySearch,
527529
selectedOptions,
528530
categories: largeCategoriesList,
529531
recentlyUsedCategories,
532+
localeCompare,
530533
});
531534
expect(largeResult).toStrictEqual(largeResultList);
532535

@@ -535,6 +538,7 @@ describe('CategoryOptionListUtils', () => {
535538
selectedOptions,
536539
categories: largeCategoriesList,
537540
recentlyUsedCategories,
541+
localeCompare,
538542
});
539543
expect(largeSearchResult).toStrictEqual(largeSearchResultList);
540544

@@ -543,10 +547,11 @@ describe('CategoryOptionListUtils', () => {
543547
selectedOptions,
544548
categories: largeCategoriesList,
545549
recentlyUsedCategories,
550+
localeCompare,
546551
});
547552
expect(largeWrongSearchResult).toStrictEqual(largeWrongSearchResultList);
548553

549-
const emptyResult = getCategoryListSections({searchValue: search, selectedOptions, categories: emptyCategoriesList});
554+
const emptyResult = getCategoryListSections({searchValue: search, selectedOptions, categories: emptyCategoriesList, localeCompare});
550555
expect(emptyResult).toStrictEqual(emptySelectedResultList);
551556
});
552557

@@ -1285,8 +1290,8 @@ describe('CategoryOptionListUtils', () => {
12851290
},
12861291
];
12871292

1288-
expect(sortCategories(categoriesIncorrectOrdering)).toStrictEqual(result);
1289-
expect(sortCategories(categoriesIncorrectOrdering2)).toStrictEqual(result2);
1290-
expect(sortCategories(categoriesIncorrectOrdering3)).toStrictEqual(result3);
1293+
expect(sortCategories(categoriesIncorrectOrdering, localeCompare)).toStrictEqual(result);
1294+
expect(sortCategories(categoriesIncorrectOrdering2, localeCompare)).toStrictEqual(result2);
1295+
expect(sortCategories(categoriesIncorrectOrdering3, localeCompare)).toStrictEqual(result3);
12911296
});
12921297
});

0 commit comments

Comments
 (0)