Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: moved report field options to their own file #52311

Merged
Merged
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
108 changes: 0 additions & 108 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ type GetOptionsConfig = {
taxRates?: TaxRatesWithDefault;
policy?: OnyxEntry<Policy>;
transaction?: OnyxEntry<Transaction>;
includePolicyReportFieldOptions?: boolean;
policyReportFieldOptions?: string[];
recentlyUsedPolicyReportFieldOptions?: string[];
transactionViolations?: OnyxCollection<TransactionViolation[]>;
includeInvoiceRooms?: boolean;
includeDomainEmail?: boolean;
Expand Down Expand Up @@ -217,7 +214,6 @@ type Options = {
categoryOptions: CategoryTreeSection[];
tagOptions: CategorySection[];
taxRatesOptions: CategorySection[];
policyReportFieldOptions?: CategorySection[] | null;
};

type PreviewConfig = {showChatPreviewLine?: boolean; forcePolicyNamePreview?: boolean; showPersonalDetails?: boolean};
Expand Down Expand Up @@ -1309,81 +1305,6 @@ function hasEnabledTags(policyTagList: Array<PolicyTagLists[keyof PolicyTagLists
return hasEnabledOptions(policyTagValueList);
}

/**
* Transforms the provided report field options into option objects.
*
* @param reportFieldOptions - an initial report field options array
*/
function getReportFieldOptions(reportFieldOptions: string[]): Option[] {
return reportFieldOptions.map((name) => ({
text: name,
keyForList: name,
searchText: name,
tooltipText: name,
isDisabled: false,
}));
}

/**
* Build the section list for report field options
*/
function getReportFieldOptionsSection(options: string[], recentlyUsedOptions: string[], selectedOptions: Array<Partial<ReportUtils.OptionData>>, searchInputValue: string) {
const reportFieldOptionsSections = [];
const selectedOptionKeys = selectedOptions.map(({text, keyForList, name}) => text ?? keyForList ?? name ?? '').filter((o) => !!o);
let indexOffset = 0;

if (searchInputValue) {
const searchOptions = options.filter((option) => option.toLowerCase().includes(searchInputValue.toLowerCase()));

reportFieldOptionsSections.push({
// "Search" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(searchOptions),
});

return reportFieldOptionsSections;
}

const filteredRecentlyUsedOptions = recentlyUsedOptions.filter((recentlyUsedOption) => !selectedOptionKeys.includes(recentlyUsedOption));
const filteredOptions = options.filter((option) => !selectedOptionKeys.includes(option));

if (selectedOptionKeys.length) {
reportFieldOptionsSections.push({
// "Selected" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(selectedOptionKeys),
});

indexOffset += selectedOptionKeys.length;
}

if (filteredRecentlyUsedOptions.length > 0) {
reportFieldOptionsSections.push({
// "Recent" section
title: Localize.translateLocal('common.recent'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredRecentlyUsedOptions),
});

indexOffset += filteredRecentlyUsedOptions.length;
}

reportFieldOptionsSections.push({
// "All" section when items amount more than the threshold
title: Localize.translateLocal('common.all'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredOptions),
});

return reportFieldOptionsSections;
}

/**
* Sorts tax rates alphabetically by name.
*/
Expand Down Expand Up @@ -1727,9 +1648,6 @@ function getOptions(
policy,
transaction,
includeSelfDM = false,
includePolicyReportFieldOptions = false,
policyReportFieldOptions = [],
recentlyUsedPolicyReportFieldOptions = [],
includeInvoiceRooms = false,
includeDomainEmail = false,
action,
Expand Down Expand Up @@ -1779,20 +1697,6 @@ function getOptions(
};
}

if (includePolicyReportFieldOptions) {
const transformedPolicyReportFieldOptions = getReportFieldOptionsSection(policyReportFieldOptions, recentlyUsedPolicyReportFieldOptions, selectedOptions, searchInputValue);
return {
recentReports: [],
personalDetails: [],
userToInvite: null,
currentUserOption: null,
categoryOptions: [],
tagOptions: [],
taxRatesOptions: [],
policyReportFieldOptions: transformedPolicyReportFieldOptions,
};
}

const parsedPhoneNumber = PhoneNumber.parsePhoneNumber(LoginUtils.appendCountryCode(Str.removeSMSDomain(searchInputValue)));
const searchValue = parsedPhoneNumber.possible ? parsedPhoneNumber.number?.e164 ?? '' : searchInputValue.toLowerCase();
const topmostReportId = Navigation.getTopmostReportId() ?? '-1';
Expand Down Expand Up @@ -2145,9 +2049,6 @@ type FilteredOptionsParams = {
taxRates?: TaxRatesWithDefault;
maxRecentReportsToShow?: number;
includeSelfDM?: boolean;
includePolicyReportFieldOptions?: boolean;
policyReportFieldOptions?: string[];
recentlyUsedPolicyReportFieldOptions?: string[];
includeInvoiceRooms?: boolean;
action?: IOUAction;
sortByReportTypeInSearch?: boolean;
Expand Down Expand Up @@ -2186,9 +2087,6 @@ function getFilteredOptions(params: FilteredOptionsParamsWithDefaultSearchValue
maxRecentReportsToShow = CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
taxRates = {} as TaxRatesWithDefault,
includeSelfDM = false,
includePolicyReportFieldOptions = false,
policyReportFieldOptions = [],
recentlyUsedPolicyReportFieldOptions = [],
includeInvoiceRooms = false,
action,
sortByReportTypeInSearch = false,
Expand All @@ -2215,9 +2113,6 @@ function getFilteredOptions(params: FilteredOptionsParamsWithDefaultSearchValue
includeTaxRates,
taxRates,
includeSelfDM,
includePolicyReportFieldOptions,
policyReportFieldOptions,
recentlyUsedPolicyReportFieldOptions,
includeInvoiceRooms,
action,
sortByReportTypeInSearch,
Expand Down Expand Up @@ -2260,9 +2155,6 @@ function getAttendeeOptions(
maxRecentReportsToShow: 0,
taxRates: {} as TaxRatesWithDefault,
includeSelfDM: false,
includePolicyReportFieldOptions: false,
policyReportFieldOptions: [],
recentlyUsedPolicyReportFieldOptions: [],
includeInvoiceRooms,
action,
sortByReportTypeInSearch,
Expand Down
90 changes: 90 additions & 0 deletions src/libs/ReportFieldOptionsListUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as Localize from './Localize';
import type {Option} from './OptionsListUtils';
import type * as ReportUtils from './ReportUtils';

/**
* Transforms the provided report field options into option objects.
*
* @param reportFieldOptions - an initial report field options array
*/
function getReportFieldOptions(reportFieldOptions: string[]): Option[] {
return reportFieldOptions.map((name) => ({
text: name,
keyForList: name,
searchText: name,
tooltipText: name,
isDisabled: false,
}));
}

/**
* Build the section list for report field options
*/
function getReportFieldOptionsSection({
options,
recentlyUsedOptions,
selectedOptions,
searchValue,
}: {
options: string[];
recentlyUsedOptions: string[];
selectedOptions: Array<Partial<ReportUtils.OptionData>>;
searchValue: string;
}) {
const reportFieldOptionsSections = [];
const selectedOptionKeys = selectedOptions.map(({text, keyForList, name}) => text ?? keyForList ?? name ?? '').filter((o) => !!o);
let indexOffset = 0;

if (searchValue) {
const searchOptions = options.filter((option) => option.toLowerCase().includes(searchValue.toLowerCase()));

reportFieldOptionsSections.push({
// "Search" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(searchOptions),
});

return reportFieldOptionsSections;
}

const filteredRecentlyUsedOptions = recentlyUsedOptions.filter((recentlyUsedOption) => !selectedOptionKeys.includes(recentlyUsedOption));
const filteredOptions = options.filter((option) => !selectedOptionKeys.includes(option));

if (selectedOptionKeys.length) {
reportFieldOptionsSections.push({
// "Selected" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(selectedOptionKeys),
});

indexOffset += selectedOptionKeys.length;
}

if (filteredRecentlyUsedOptions.length > 0) {
reportFieldOptionsSections.push({
// "Recent" section
title: Localize.translateLocal('common.recent'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredRecentlyUsedOptions),
});

indexOffset += filteredRecentlyUsedOptions.length;
}

reportFieldOptionsSections.push({
// "All" section when items amount more than the threshold
title: Localize.translateLocal('common.all'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredOptions),
});

return reportFieldOptionsSections;
}

export {getReportFieldOptionsSection, getReportFieldOptions};
15 changes: 6 additions & 9 deletions src/pages/EditReportFieldDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import localeCompare from '@libs/LocaleCompare';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportFieldOptionsListUtils from '@libs/ReportFieldOptionsListUtils';
import ONYXKEYS from '@src/ONYXKEYS';

type EditReportFieldDropdownPageComponentProps = {
Expand Down Expand Up @@ -58,7 +59,7 @@ function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptio
const [sections, headerMessage] = useMemo(() => {
const validFieldOptions = fieldOptions?.filter((option) => !!option)?.sort(localeCompare);

const {policyReportFieldOptions} = OptionsListUtils.getFilteredOptions({
const policyReportFieldOptions = ReportFieldOptionsListUtils.getReportFieldOptionsSection({
searchValue: debouncedSearchValue,
selectedOptions: [
{
Expand All @@ -67,21 +68,17 @@ function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptio
text: fieldValue,
},
],

includeP2P: false,
canInviteUser: false,
includePolicyReportFieldOptions: true,
policyReportFieldOptions: validFieldOptions,
recentlyUsedPolicyReportFieldOptions: recentlyUsedOptions,
options: validFieldOptions,
recentlyUsedOptions,
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB - Should we re-order these to match the params better, and not have to name them? I could go either way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry not sure what you mean, you mean that we rename validFieldOptions to options so we don't have to specify the object property - or to not use an object for the getReportFieldOptionsSection but separate parameters?

I think we have no style guideline on that but as soon as a function takes more than three parameters I usually create an object type as single parameter. For me this helps with readability, understanding what other options you can pass without opening the function declaration (intelli sense) and avoid having to pass undefined as a parameter explicitly when when we want to skip passing a certain parameter

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah i think that's fine! I just meant that the order they're listed in the function definition is different than the order we're passing them here. But it's not a huge deal, no need to change

});

const policyReportFieldData = policyReportFieldOptions?.[0]?.data ?? [];
const policyReportFieldData = policyReportFieldOptions.at(0)?.data ?? [];
const header = OptionsListUtils.getHeaderMessageForNonUserList(policyReportFieldData.length > 0, debouncedSearchValue);

return [policyReportFieldOptions, header];
}, [recentlyUsedOptions, debouncedSearchValue, fieldValue, fieldOptions]);

const selectedOptionKey = useMemo(() => (sections?.[0]?.data ?? []).filter((option) => option.searchText === fieldValue)?.at(0)?.keyForList, [sections, fieldValue]);
const selectedOptionKey = useMemo(() => (sections.at(0)?.data ?? []).filter((option) => option.searchText === fieldValue)?.at(0)?.keyForList, [sections, fieldValue]);
return (
<SelectionList
textInputValue={searchValue}
Expand Down
Loading