Skip to content

Commit

Permalink
Merge pull request #46757 from cretadn22/add-sort-func
Browse files Browse the repository at this point in the history
Add sort function in report field pages
  • Loading branch information
youssef-lr authored Aug 6, 2024
2 parents 290a9d9 + 634fe3d commit 984fd39
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/pages/EditReportFieldDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {ListItem} from '@components/SelectionList/types';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import localeCompare from '@libs/LocaleCompare';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -42,7 +43,7 @@ function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptio
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
const theme = useTheme();
const {translate} = useLocalize();
const recentlyUsedOptions = useMemo(() => recentlyUsedReportFields?.[fieldKey] ?? [], [recentlyUsedReportFields, fieldKey]);
const recentlyUsedOptions = useMemo(() => recentlyUsedReportFields?.[fieldKey]?.sort(localeCompare) ?? [], [recentlyUsedReportFields, fieldKey]);

const itemRightSideComponent = useCallback(
(item: ListItem) => {
Expand All @@ -61,7 +62,7 @@ function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptio
);

const [sections, headerMessage] = useMemo(() => {
const validFieldOptions = fieldOptions?.filter((option) => !!option);
const validFieldOptions = fieldOptions?.filter((option) => !!option)?.sort(localeCompare);

const {policyReportFieldOptions} = OptionsListUtils.getFilteredOptions(
[],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useMemo} from 'react';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import localeCompare from '@libs/LocaleCompare';

type ReportFieldsInitialListValuePickerProps = {
/** Options to select from if field is of type list */
Expand All @@ -22,6 +23,7 @@ function ReportFieldsInitialListValuePicker({listValues, disabledOptions, value,
{
data: Object.values(listValues ?? {})
.filter((listValue, index) => !disabledOptions[index])
.sort(localeCompare)
.map((listValue) => ({
keyForList: listValue,
value: listValue,
Expand Down
34 changes: 18 additions & 16 deletions src/pages/workspace/reportFields/ReportFieldsListValuesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as ReportField from '@libs/actions/Policy/ReportField';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -85,22 +86,23 @@ function ReportFieldsListValuesPage({
}, [formDraft?.disabledListValues, formDraft?.listValues, policy?.fieldList, reportFieldID]);

const listValuesSections = useMemo(() => {
const data = listValues.map<ValueListItem>((value, index) => ({
value,
index,
text: value,
keyForList: value,
isSelected: selectedValues[value],
enabled: !disabledListValues[index] ?? true,
pendingAction: reportFieldID ? policy?.fieldList?.[ReportUtils.getReportFieldKey(reportFieldID)]?.pendingAction : null,
rightElement: (
<ListItemRightCaretWithLabel
shouldShowCaret={false}
labelText={disabledListValues[index] ? translate('workspace.common.disabled') : translate('workspace.common.enabled')}
/>
),
}));

const data = listValues
.map<ValueListItem>((value, index) => ({
value,
index,
text: value,
keyForList: value,
isSelected: selectedValues[value],
enabled: !disabledListValues[index] ?? true,
pendingAction: reportFieldID ? policy?.fieldList?.[ReportUtils.getReportFieldKey(reportFieldID)]?.pendingAction : null,
rightElement: (
<ListItemRightCaretWithLabel
shouldShowCaret={false}
labelText={disabledListValues[index] ? translate('workspace.common.disabled') : translate('workspace.common.enabled')}
/>
),
}))
.sort((a, b) => localeCompare(a.value, b.value));
return [{data, isDisabled: false}];
}, [disabledListValues, listValues, policy?.fieldList, reportFieldID, selectedValues, translate]);

Expand Down
35 changes: 19 additions & 16 deletions src/pages/workspace/reportFields/WorkspaceReportFieldsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import type {FullScreenNavigatorParamList} from '@libs/Navigation/types';
import * as PolicyUtils from '@libs/PolicyUtils';
Expand Down Expand Up @@ -100,22 +101,24 @@ function WorkspaceReportFieldsPage({

return [
{
data: Object.values(filteredPolicyFieldList).map((reportField) => ({
value: reportField.name,
fieldID: reportField.fieldID,
keyForList: String(reportField.fieldID),
orderWeight: reportField.orderWeight,
pendingAction: reportField.pendingAction,
isSelected: selectedReportFields.find((selectedReportField) => selectedReportField.name === reportField.name) !== undefined,
isDisabled: reportField.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
text: reportField.name,
rightElement: (
<ListItemRightCaretWithLabel
shouldShowCaret={false}
labelText={Str.recapitalize(translate(WorkspaceReportFieldUtils.getReportFieldTypeTranslationKey(reportField.type)))}
/>
),
})),
data: Object.values(filteredPolicyFieldList)
.sort((a, b) => localeCompare(a.name, b.name))
.map((reportField) => ({
value: reportField.name,
fieldID: reportField.fieldID,
keyForList: String(reportField.fieldID),
orderWeight: reportField.orderWeight,
pendingAction: reportField.pendingAction,
isSelected: selectedReportFields.find((selectedReportField) => selectedReportField.name === reportField.name) !== undefined,
isDisabled: reportField.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
text: reportField.name,
rightElement: (
<ListItemRightCaretWithLabel
shouldShowCaret={false}
labelText={Str.recapitalize(translate(WorkspaceReportFieldUtils.getReportFieldTypeTranslationKey(reportField.type)))}
/>
),
})),
isDisabled: false,
},
];
Expand Down

0 comments on commit 984fd39

Please sign in to comment.