Skip to content

Add move option to search selected menu #62270

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

Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6486,6 +6486,7 @@ const CONST = {
HOLD: 'hold',
UNHOLD: 'unhold',
DELETE: 'delete',
CHANGE_REPORT: 'changeReport',
},
TRANSACTION_TYPE: {
CASH: 'cash',
Expand Down
1 change: 1 addition & 0 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const ROUTES = {
},
},
TRANSACTION_HOLD_REASON_RHP: 'search/hold',
MOVE_TRANSACTIONS_SEARCH_RHP: 'search/move-transactions',

// This is a utility route used to go to the user's concierge chat, or the sign-in page if the user's not authenticated
CONCIERGE: 'concierge',
Expand Down
1 change: 1 addition & 0 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const SCREENS = {
SAVED_SEARCH_RENAME_RHP: 'Search_Saved_Search_Rename_RHP',
ADVANCED_FILTERS_IN_RHP: 'Search_Advanced_Filters_In_RHP',
TRANSACTION_HOLD_REASON_RHP: 'Search_Transaction_Hold_Reason_RHP',
TRANSACTIONS_CHANGE_REPORT_SEARCH_RHP: 'Search_Transactions_Change_Report_RHP',
},
SETTINGS: {
ROOT: 'Settings_Root',
Expand Down
37 changes: 29 additions & 8 deletions src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {canUseTouchScreen} from '@libs/DeviceCapabilities';
import Log from '@libs/Log';
import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute';
import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNavigation/types';
import {generateReportID} from '@libs/ReportUtils';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {canEditFieldOfMoneyRequest, generateReportID} from '@libs/ReportUtils';
import {buildSearchQueryString} from '@libs/SearchQueryUtils';
import {
getListItem,
Expand All @@ -44,6 +45,7 @@ import EmptySearchView from '@pages/Search/EmptySearchView';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {ReportAction} from '@src/types/onyx';
import type SearchResults from '@src/types/onyx/SearchResults';
import {useSearchContext} from './SearchContext';
import SearchList from './SearchList';
Expand All @@ -59,7 +61,7 @@ type SearchProps = {
handleSearch: (value: SearchParams) => void;
};

function mapTransactionItemToSelectedEntry(item: TransactionListItemType): [string, SelectedTransactionInfo] {
function mapTransactionItemToSelectedEntry(item: TransactionListItemType, reportActions: ReportAction[]): [string, SelectedTransactionInfo] {
return [
item.keyForList,
{
Expand All @@ -68,6 +70,7 @@ function mapTransactionItemToSelectedEntry(item: TransactionListItemType): [stri
canHold: item.canHold,
isHeld: isOnHold(item),
canUnhold: item.canUnhold,
canChangeReport: canEditFieldOfMoneyRequest(getIOUActionForTransactionID(reportActions, item.transactionID), CONST.EDIT_REQUEST_FIELD.REPORT),
action: item.action,
reportID: item.reportID,
policyID: item.policyID,
Expand Down Expand Up @@ -105,7 +108,7 @@ function mapToItemWithSelectionInfo(item: SearchListItem, selectedTransactions:
};
}

function prepareTransactionsList(item: TransactionListItemType, selectedTransactions: SelectedTransactions) {
function prepareTransactionsList(item: TransactionListItemType, selectedTransactions: SelectedTransactions, reportActions: ReportAction[]) {
if (selectedTransactions[item.keyForList]?.isSelected) {
const {[item.keyForList]: omittedTransaction, ...transactions} = selectedTransactions;

Expand All @@ -120,6 +123,7 @@ function prepareTransactionsList(item: TransactionListItemType, selectedTransact
canHold: item.canHold,
isHeld: isOnHold(item),
canUnhold: item.canUnhold,
canChangeReport: canEditFieldOfMoneyRequest(getIOUActionForTransactionID(reportActions, item.transactionID), CONST.EDIT_REQUEST_FIELD.REPORT),
action: item.action,
reportID: item.reportID,
policyID: item.policyID,
Expand Down Expand Up @@ -158,6 +162,13 @@ function Search({queryJSON, currentSearchResults, lastNonEmptySearchResults, onS
const previousTransactions = usePrevious(transactions);
const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {canBeMissing: true});
const previousReportActions = usePrevious(reportActions);
const reportActionsArray = useMemo(
() =>
Object.values(reportActions ?? {})
.filter((reportAction) => !!reportAction)
.flatMap((fillteredReportActions) => Object.values(fillteredReportActions ?? {})),
[reportActions],
);
const {translate} = useLocalize();
const searchListRef = useRef<SelectionListHandle | null>(null);

Expand Down Expand Up @@ -256,6 +267,7 @@ function Search({queryJSON, currentSearchResults, lastNonEmptySearchResults, onS
canHold: transaction.canHold,
isHeld: isOnHold(transaction),
canUnhold: transaction.canUnhold,
canChangeReport: canEditFieldOfMoneyRequest(getIOUActionForTransactionID(reportActionsArray, transaction.transactionID), CONST.EDIT_REQUEST_FIELD.REPORT),
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
isSelected: isExportMode || selectedTransactions[transaction.transactionID].isSelected,
canDelete: transaction.canDelete,
Expand All @@ -278,6 +290,7 @@ function Search({queryJSON, currentSearchResults, lastNonEmptySearchResults, onS
canHold: transaction.canHold,
isHeld: isOnHold(transaction),
canUnhold: transaction.canUnhold,
canChangeReport: canEditFieldOfMoneyRequest(getIOUActionForTransactionID(reportActionsArray, transaction.transactionID), CONST.EDIT_REQUEST_FIELD.REPORT),
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
isSelected: isExportMode || selectedTransactions[transaction.transactionID].isSelected,
canDelete: transaction.canDelete,
Expand Down Expand Up @@ -344,7 +357,7 @@ function Search({queryJSON, currentSearchResults, lastNonEmptySearchResults, onS
return;
}

setSelectedTransactions(prepareTransactionsList(item, selectedTransactions), data);
setSelectedTransactions(prepareTransactionsList(item, selectedTransactions, reportActionsArray), data);
return;
}

Expand All @@ -362,12 +375,12 @@ function Search({queryJSON, currentSearchResults, lastNonEmptySearchResults, onS
setSelectedTransactions(
{
...selectedTransactions,
...Object.fromEntries(item.transactions.map(mapTransactionItemToSelectedEntry)),
...Object.fromEntries(item.transactions.map((transactionItem) => mapTransactionItemToSelectedEntry(transactionItem, reportActionsArray))),
},
data,
);
},
[data, selectedTransactions, setSelectedTransactions],
[data, reportActionsArray, selectedTransactions, setSelectedTransactions],
);

const openReport = useCallback(
Expand Down Expand Up @@ -523,12 +536,20 @@ function Search({queryJSON, currentSearchResults, lastNonEmptySearchResults, onS
}

if (areItemsOfReportType) {
setSelectedTransactions(Object.fromEntries((data as ReportListItemType[]).flatMap((item) => item.transactions.map(mapTransactionItemToSelectedEntry))), data);
setSelectedTransactions(
Object.fromEntries(
(data as ReportListItemType[]).flatMap((item) => item.transactions.map((transactionItem) => mapTransactionItemToSelectedEntry(transactionItem, reportActionsArray))),
),
data,
);

return;
}

setSelectedTransactions(Object.fromEntries((data as TransactionListItemType[]).map(mapTransactionItemToSelectedEntry)), data);
setSelectedTransactions(
Object.fromEntries((data as TransactionListItemType[]).map((transactionItem) => mapTransactionItemToSelectedEntry(transactionItem, reportActionsArray))),
data,
);
};

const onSortPress = (column: SearchColumnType, order: SortOrder) => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type SelectedTransactionInfo = {
/** If the transaction can be put on hold */
canHold: boolean;

/** If the transaction can be moved to other report */
canChangeReport: boolean;

/** Whether the transaction is currently held */
isHeld: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ const SearchReportModalStackNavigator = createModalStackNavigator<SearchReportPa
[SCREENS.SEARCH.REPORT_RHP]: () => require<ReactComponentModule>('../../../../pages/home/ReportScreen').default,
[SCREENS.SEARCH.MONEY_REQUEST_REPORT_HOLD_TRANSACTIONS]: () => require<ReactComponentModule>('../../../../pages/Search/SearchMoneyRequestReportHoldReasonPage').default,
[SCREENS.SEARCH.TRANSACTION_HOLD_REASON_RHP]: () => require<ReactComponentModule>('../../../../pages/Search/SearchHoldReasonPage').default,
[SCREENS.SEARCH.TRANSACTIONS_CHANGE_REPORT_SEARCH_RHP]: () => require<ReactComponentModule>('../../../../pages/Search/SearchTransactionsChangeReport').default,
},
() => ({
animation: Animations.NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const SEARCH_TO_RHP: Partial<Record<keyof SearchFullscreenNavigatorParamList, st
[SCREENS.SEARCH.ROOT]: [
SCREENS.SEARCH.REPORT_RHP,
SCREENS.SEARCH.TRANSACTION_HOLD_REASON_RHP,
SCREENS.SEARCH.TRANSACTIONS_CHANGE_REPORT_SEARCH_RHP,
SCREENS.SEARCH.ADVANCED_FILTERS_RHP,
SCREENS.SEARCH.ADVANCED_FILTERS_CURRENCY_RHP,
SCREENS.SEARCH.ADVANCED_FILTERS_DATE_RHP,
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
[SCREENS.SEARCH.REPORT_RHP]: ROUTES.SEARCH_REPORT.route,
[SCREENS.SEARCH.MONEY_REQUEST_REPORT_HOLD_TRANSACTIONS]: ROUTES.SEARCH_MONEY_REQUEST_REPORT_HOLD_TRANSACTIONS.route,
[SCREENS.SEARCH.TRANSACTION_HOLD_REASON_RHP]: ROUTES.TRANSACTION_HOLD_REASON_RHP,
[SCREENS.SEARCH.TRANSACTIONS_CHANGE_REPORT_SEARCH_RHP]: ROUTES.MOVE_TRANSACTIONS_SEARCH_RHP,
},
},
[SCREENS.RIGHT_MODAL.SEARCH_ADVANCED_FILTERS]: {
Expand Down
14 changes: 14 additions & 0 deletions src/pages/Search/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ function SearchPage({route}: SearchPageProps) {
});
}

const canAllTransactionsBeMoved = selectedTransactionsKeys.every((id) => selectedTransactions[id].canChangeReport);

if (canAllTransactionsBeMoved) {
options.push({
text: translate('iou.moveExpenses', {count: selectedTransactionsKeys.length}),
icon: Expensicons.DocumentMerge,
value: CONST.SEARCH.BULK_ACTION_TYPES.CHANGE_REPORT,
onSelected: () => {
const editRoute = ROUTES.MOVE_TRANSACTIONS_SEARCH_RHP;
Navigation.navigate(editRoute);
},
});
}

const shouldShowDeleteOption = !isOffline && selectedTransactionsKeys.every((id) => selectedTransactions[id].canDelete);

if (shouldShowDeleteOption) {
Expand Down
54 changes: 54 additions & 0 deletions src/pages/Search/SearchTransactionsChangeReport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, {useMemo} from 'react';
import {useOnyx} from 'react-native-onyx';
import {useSearchContext} from '@components/Search/SearchContext';
import type {ListItem} from '@components/SelectionList/types';
import {changeTransactionsReport} from '@libs/actions/Transaction';
import Navigation from '@libs/Navigation/Navigation';
import IOURequestEditReportCommon from '@pages/iou/request/step/IOURequestEditReportCommon';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report} from '@src/types/onyx';

type ReportListItem = ListItem & {
/** reportID of the report */
value: string;
};

function SearchTransactionsChangeReport() {
const {selectedTransactions, clearSelectedTransactions} = useSearchContext();
const selectedTransactionsKeys = useMemo(() => Object.keys(selectedTransactions), [selectedTransactions]);

const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false});
const transactionsReports = useMemo(() => {
const reports = Object.values(selectedTransactions).reduce((acc, transaction) => {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transaction.reportID}`];
if (report) {
acc.add(report);
}
return acc;
}, new Set<Report>());
return [...reports];
}, [allReports, selectedTransactions]);

const selectReport = (item: ReportListItem) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi! This is causing #62500 - we need to add similar logic to this so that it doesn't try to move the report if it's being moved to the report it's already on.

if (selectedTransactionsKeys.length === 0) {
return;
}

changeTransactionsReport(selectedTransactionsKeys, item.value);
clearSelectedTransactions();

Navigation.goBack();
};

return (
<IOURequestEditReportCommon
backTo={undefined}
transactionsReports={transactionsReports}
selectReport={selectReport}
/>
);
}

SearchTransactionsChangeReport.displayName = 'SearchTransactionsChangeReport';

export default SearchTransactionsChangeReport;
2 changes: 1 addition & 1 deletion src/pages/iou/request/step/IOURequestEditReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
return (
<IOURequestEditReportCommon
backTo={backTo}
transactionReport={transactionReport}
transactionsReports={transactionReport ? [transactionReport] : []}
selectReport={selectReport}
/>
);
Expand Down
28 changes: 21 additions & 7 deletions src/pages/iou/request/step/IOURequestEditReportCommon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,37 @@ const reportSelector = (report: OnyxEntry<Report>): OnyxEntry<Report> =>

type Props = {
backTo: Route | undefined;
transactionReport: OnyxEntry<Report>;
transactionsReports: Report[];
selectReport: (item: ReportListItem) => void;
};

function IOURequestEditReportCommon({backTo, transactionReport, selectReport}: Props) {
function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}: Props) {
const {translate} = useLocalize();
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: (reports) => mapOnyxCollectionItems(reports, reportSelector), canBeMissing: true});
const [allPoliciesID] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (policies) => mapOnyxCollectionItems(policies, (policy) => policy?.id), canBeMissing: false});

const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');

const expenseReports = getOutstandingReportsForUser(transactionReport?.policyID, transactionReport?.ownerAccountID ?? currentUserPersonalDetails.accountID, allReports ?? {});
const expenseReports = useMemo(
() =>
Object.values(allPoliciesID ?? {}).flatMap((policyID) => {
if (!policyID) {
return [];
}
const reports = getOutstandingReportsForUser(policyID, currentUserPersonalDetails.accountID, allReports ?? {});
return reports;
}),
[allPoliciesID, allReports, currentUserPersonalDetails.accountID],
);

const reportOptions: ReportListItem[] = useMemo(() => {
if (!allReports) {
return [];
}

const isTransactionReportCorrect = expenseReports.some((report) => report?.reportID === transactionReport?.reportID);
const onlyReport = transactionsReports.length === 1 ? transactionsReports.at(0) : undefined;

return expenseReports
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0)
.filter((report) => !debouncedSearchValue || report?.reportName?.toLowerCase().includes(debouncedSearchValue.toLowerCase()))
Expand All @@ -63,9 +77,9 @@ function IOURequestEditReportCommon({backTo, transactionReport, selectReport}: P
text: report.reportName,
value: report.reportID,
keyForList: report.reportID,
isSelected: isTransactionReportCorrect ? report.reportID === transactionReport?.reportID : expenseReports.at(0)?.reportID === report.reportID,
isSelected: onlyReport && report.reportID === onlyReport?.reportID,
}));
}, [allReports, debouncedSearchValue, expenseReports, transactionReport?.reportID]);
}, [allReports, debouncedSearchValue, expenseReports, transactionsReports]);

const navigateBack = () => {
Navigation.goBack(backTo);
Expand All @@ -90,7 +104,7 @@ function IOURequestEditReportCommon({backTo, transactionReport, selectReport}: P
textInputLabel={expenseReports.length >= CONST.STANDARD_LIST_ITEM_LIMIT ? translate('common.search') : undefined}
shouldSingleExecuteRowSelect
headerMessage={headerMessage}
initiallyFocusedOptionKey={transactionReport?.reportID}
initiallyFocusedOptionKey={transactionsReports.length === 1 ? transactionsReports.at(0)?.reportID : undefined}
ListItem={UserListItem}
/>
</StepScreenWrapper>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/iou/request/step/IOURequestStepReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
return (
<IOURequestEditReportCommon
backTo={backTo}
transactionReport={transactionReport}
transactionsReports={transactionReport ? [transactionReport] : []}
selectReport={selectReport}
/>
);
Expand Down
Loading