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

Handle hold unhold search #49605

Merged
merged 9 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 7 additions & 3 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,13 @@ const ROUTES = {
},
},
MONEY_REQUEST_HOLD_REASON: {
route: ':type/edit/reason/:transactionID?',
getRoute: (type: ValueOf<typeof CONST.POLICY.TYPE>, transactionID: string, reportID: string, backTo: string) =>
`${type}/edit/reason/${transactionID}?backTo=${backTo}&reportID=${reportID}` as const,
route: ':type/edit/reason/:transactionID?/:searchHash?',
getRoute: (type: ValueOf<typeof CONST.POLICY.TYPE>, transactionID: string, reportID: string, backTo: string, searchHash?: number) => {
const route = searchHash
? (`${type}/edit/reason/${transactionID}/${searchHash}/?backTo=${backTo}&reportID=${reportID}` as const)
: (`${type}/edit/reason/${transactionID}/?backTo=${backTo}&reportID=${reportID}` as const);
return route;
},
},
MONEY_REQUEST_CREATE: {
route: ':action/:iouType/start/:transactionID/:reportID',
Expand Down
5 changes: 3 additions & 2 deletions src/components/PromotedActionsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type PromotedActionsType = Record<BasePromotedActions, (report: OnyxReport) => P
reportID?: string;
isDelegateAccessRestricted: boolean;
setIsNoDelegateAccessMenuVisible: (isVisible: boolean) => void;
currentSearchHash?: number;
}) => PromotedAction;
};

Expand Down Expand Up @@ -78,7 +79,7 @@ const PromotedActions = {
}
},
}),
hold: ({isTextHold, reportAction, reportID, isDelegateAccessRestricted, setIsNoDelegateAccessMenuVisible}) => ({
hold: ({isTextHold, reportAction, reportID, isDelegateAccessRestricted, setIsNoDelegateAccessMenuVisible, currentSearchHash}) => ({
key: CONST.PROMOTED_ACTIONS.HOLD,
icon: Expensicons.Stopwatch,
text: Localize.translateLocal(`iou.${isTextHold ? 'hold' : 'unhold'}`),
Expand All @@ -99,7 +100,7 @@ const PromotedActions = {
return;
}

ReportUtils.changeMoneyRequestHoldStatus(reportAction, ROUTES.SEARCH_REPORT.getRoute(targetedReportID));
ReportUtils.changeMoneyRequestHoldStatus(reportAction, ROUTES.SEARCH_REPORT.getRoute(targetedReportID), currentSearchHash);
},
}),
} satisfies PromotedActionsType;
Expand Down
10 changes: 6 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3120,7 +3120,7 @@ function canHoldUnholdReportAction(reportAction: OnyxInputOrEntry<ReportAction>)
return {canHoldRequest, canUnholdRequest};
}

const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>, backTo?: string): void => {
const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>, backTo?: string, searchHash?: number): void => {
if (!ReportActionsUtils.isMoneyRequestAction(reportAction)) {
return;
}
Expand All @@ -3137,11 +3137,13 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>, bac
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport.policyID}`] ?? null;

if (isOnHold) {
IOU.unholdRequest(transactionID, reportAction.childReportID ?? '');
IOU.unholdRequest(transactionID, reportAction.childReportID ?? '', searchHash);
} else {
const activeRoute = encodeURIComponent(Navigation.getActiveRouteWithoutParams());
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Navigation.navigate(ROUTES.MONEY_REQUEST_HOLD_REASON.getRoute(policy?.type ?? CONST.POLICY.TYPE.PERSONAL, transactionID, reportAction.childReportID ?? '', backTo || activeRoute));
Navigation.navigate(
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
ROUTES.MONEY_REQUEST_HOLD_REASON.getRoute(policy?.type ?? CONST.POLICY.TYPE.PERSONAL, transactionID, reportAction.childReportID ?? '', backTo || activeRoute, searchHash),
);
}
};

Expand Down
61 changes: 59 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type ReportAction from '@src/types/onyx/ReportAction';
import type {OnyxData} from '@src/types/onyx/Request';
import {SearchTransaction} from '@src/types/onyx/SearchResults';

Check failure on line 67 in src/libs/actions/IOU.ts

View workflow job for this annotation

GitHub Actions / ESLint check

All imports in the declaration are only used as types. Use `import type`
import type {Comment, Receipt, ReceiptSource, Routes, SplitShares, TransactionChanges, WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as CachedPDFPaths from './CachedPDFPaths';
Expand Down Expand Up @@ -7835,7 +7836,7 @@
/**
* Put expense on HOLD
*/
function putOnHold(transactionID: string, comment: string, reportID: string) {
function putOnHold(transactionID: string, comment: string, reportID: string, searchHash?: number) {
const currentTime = DateUtils.getDBTime();
const createdReportAction = ReportUtils.buildOptimisticHoldReportAction(currentTime);
const createdReportActionComment = ReportUtils.buildOptimisticHoldReportActionComment(comment, DateUtils.addMillisecondsFromDateTime(currentTime, 1));
Expand Down Expand Up @@ -7892,6 +7893,34 @@
},
];

// If we are holding from the search page, we optimistically update the snapshot data that search uses so that it is kept in sync
if (searchHash) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${searchHash}`,
value: {
data: {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: {
canHold: false,
canUnhold: true,
},
},
} as Record<string, Record<string, Partial<SearchTransaction>>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the same type

const onyxUpdate: Record<string, Record<string, Partial<SearchTransaction>>> = {

});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${searchHash}`,
value: {
data: {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: {
canHold: true,
canUnhold: false,
},
},
} as Record<string, Record<string, Partial<SearchTransaction>>>,
});
}

API.write(
'HoldRequest',
{
Expand All @@ -7907,7 +7936,7 @@
/**
* Remove expense from HOLD
*/
function unholdRequest(transactionID: string, reportID: string) {
function unholdRequest(transactionID: string, reportID: string, searchHash?: number) {
const createdReportAction = ReportUtils.buildOptimisticUnHoldReportAction();
const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`];

Expand Down Expand Up @@ -7965,6 +7994,34 @@
},
];

// If we are unholding from the search page, we optimistically update the snapshot data that search uses so that it is kept in sync
if (searchHash) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${searchHash}`,
value: {
data: {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: {
canHold: true,
canUnhold: false,
},
},
} as Record<string, Record<string, Partial<SearchTransaction>>>,
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${searchHash}`,
value: {
data: {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: {
canHold: false,
canUnhold: true,
},
},
} as Record<string, Record<string, Partial<SearchTransaction>>>,
});
}

API.write(
'UnHoldRequest',
{
Expand Down
16 changes: 15 additions & 1 deletion src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import PromotedActionsBar, {PromotedActions} from '@components/PromotedActionsBa
import RoomHeaderAvatars from '@components/RoomHeaderAvatars';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import {useSearchContext} from '@components/Search/SearchContext';
import Text from '@components/Text';
import useDelegateUserDetails from '@hooks/useDelegateUserDetails';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -87,6 +88,7 @@ function ReportDetailsPage({policies, report, route}: ReportDetailsPageProps) {
const [parentReportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.parentReportID || '-1'}`);
const {reportActions} = usePaginatedReportActions(report.reportID || '-1');
/* eslint-enable @typescript-eslint/prefer-nullish-coalescing */
const {currentSearchHash} = useSearchContext();

const transactionThreadReportID = useMemo(
() => ReportActionsUtils.getOneTransactionThreadReportID(report.reportID, reportActions ?? [], isOffline),
Expand Down Expand Up @@ -571,6 +573,7 @@ function ReportDetailsPage({policies, report, route}: ReportDetailsPageProps) {
reportID: transactionThreadReportID ? report.reportID : moneyRequestAction?.childReportID ?? '-1',
isDelegateAccessRestricted,
setIsNoDelegateAccessMenuVisible,
currentSearchHash,
}),
);
}
Expand All @@ -582,7 +585,18 @@ function ReportDetailsPage({policies, report, route}: ReportDetailsPageProps) {
result.push(PromotedActions.share(report, backTo));

return result;
}, [report, moneyRequestAction, canJoin, isExpenseReport, shouldShowHoldAction, canHoldUnholdReportAction.canHoldRequest, transactionThreadReportID, isDelegateAccessRestricted, backTo]);
}, [
report,
moneyRequestAction,
currentSearchHash,
canJoin,
isExpenseReport,
shouldShowHoldAction,
canHoldUnholdReportAction.canHoldRequest,
transactionThreadReportID,
isDelegateAccessRestricted,
backTo,
]);

const nameSectionExpenseIOU = (
<View style={[styles.reportDetailsRoomInfo, styles.mw100]}>
Expand Down
6 changes: 4 additions & 2 deletions src/pages/iou/HoldReasonPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type HoldReasonPageRouteParams = {

/** Link to previous page */
backTo: Route;

searchHash?: number;
};

type HoldReasonPageProps = {
Expand All @@ -34,7 +36,7 @@ type HoldReasonPageProps = {
function HoldReasonPage({route}: HoldReasonPageProps) {
const {translate} = useLocalize();

const {transactionID, reportID, backTo} = route.params;
const {transactionID, reportID, backTo, searchHash} = route.params;

const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID || -1}`);

Expand All @@ -51,7 +53,7 @@ function HoldReasonPage({route}: HoldReasonPageProps) {
return;
}

IOU.putOnHold(transactionID, values.comment, reportID);
IOU.putOnHold(transactionID, values.comment, reportID, searchHash);
Navigation.navigate(backTo);
};

Expand Down
Loading