-
Notifications
You must be signed in to change notification settings - Fork 3.9k
refactor: Extract shared hooks for report preview action buttons #95941
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
Changes from all commits
f33f171
1e16d77
c7e3ef3
a84e24d
740c5c5
e8b9d80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /** | ||
| * Returns the confirm-approval handler shared by the report-preview Approve and Pay buttons, | ||
| * handling delegate-access restrictions, held expenses, and the approveMoneyRequest call. | ||
| */ | ||
| import {useDelegateNoAccessActions, useDelegateNoAccessState} from '@components/DelegateNoAccessModalProvider'; | ||
|
|
||
| import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import usePermissions from '@hooks/usePermissions'; | ||
|
|
||
| import {hasHeldExpensesFromTransactions as hasHeldExpensesReportUtils} from '@libs/ReportUtils'; | ||
|
|
||
| import {approveMoneyRequest} from '@userActions/IOU/ReportWorkflow'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Transaction} from '@src/types/onyx'; | ||
|
|
||
| import {isTrackIntentUserSelector} from '@selectors/Onboarding'; | ||
|
|
||
| import type useReportPreviewActionButtonData from './useReportPreviewActionButtonData'; | ||
|
|
||
| import {useReportPreviewActions, useReportPreviewActionState} from './MoneyRequestReportPreviewContext'; | ||
|
|
||
| function useConfirmApproveReportAction(actionButtonData: ReturnType<typeof useReportPreviewActionButtonData>, transactions: Transaction[], hasViolations: boolean) { | ||
| const currentUserDetails = useCurrentUserPersonalDetails(); | ||
| const {isBetaEnabled} = usePermissions(); | ||
| const {isDelegateAccessRestricted} = useDelegateNoAccessState(); | ||
| const {showDelegateNoAccessModal} = useDelegateNoAccessActions(); | ||
| const {shouldShowPayButton} = useReportPreviewActionState(); | ||
| const {startApprovedAnimation, onHoldMenuOpen} = useReportPreviewActions(); | ||
| const [betas] = useOnyx(ONYXKEYS.BETAS); | ||
| const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); | ||
|
|
||
| const {iouReport, policy, ownerLogin, userBillingGracePeriodEnds, iouReportNextStep, amountOwed, ownerBillingGracePeriodEnd, delegateEmail} = actionButtonData; | ||
|
|
||
| return () => { | ||
| if (isDelegateAccessRestricted) { | ||
| showDelegateNoAccessModal(); | ||
| } else if (hasHeldExpensesReportUtils(transactions)) { | ||
| onHoldMenuOpen(CONST.IOU.REPORT_ACTION_TYPE.APPROVE, undefined, shouldShowPayButton); | ||
| } else { | ||
| approveMoneyRequest({ | ||
| expenseReport: iouReport, | ||
| expenseReportPolicy: policy, | ||
| currentUserAccountIDParam: currentUserDetails.accountID, | ||
| currentUserEmailParam: currentUserDetails.email ?? '', | ||
| hasViolations, | ||
| isASAPSubmitBetaEnabled: isBetaEnabled(CONST.BETAS.ASAP_SUBMIT), | ||
| expenseReportCurrentNextStepDeprecated: iouReportNextStep, | ||
| betas, | ||
| userBillingGracePeriodEnds, | ||
| amountOwed, | ||
| ownerBillingGracePeriodEnd, | ||
| ownerLogin, | ||
| full: true, | ||
| onApproved: startApprovedAnimation, | ||
| delegateEmail, | ||
| isTrackIntentUser, | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export default useConfirmApproveReportAction; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * Subscribes to the Onyx data shared by the money-request report-preview action buttons | ||
| * (Submit / Approve / Pay) and returns it as a single object. | ||
| */ | ||
| import useOnyx from '@hooks/useOnyx'; | ||
|
OlimpiaZurek marked this conversation as resolved.
|
||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {personalDetailsLoginSelector} from '@src/selectors/PersonalDetails'; | ||
|
|
||
| import {delegateEmailSelector} from '@selectors/Account'; | ||
|
|
||
| function useReportPreviewActionButtonData(iouReportID: string | undefined) { | ||
| const [iouReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked, but these two aren't the same as the context values:
These are just single-record subscriptions (not a whole collection like transactions), so I'd keep them as |
||
| const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`); | ||
| const [ownerLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(iouReport?.ownerAccountID)}); | ||
|
OlimpiaZurek marked this conversation as resolved.
|
||
| const [userBillingGracePeriodEnds] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END); | ||
| const [iouReportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReportID}`); | ||
| const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED); | ||
| const [ownerBillingGracePeriodEnd] = useOnyx(ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END); | ||
| const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector}); | ||
|
|
||
| return { | ||
|
OlimpiaZurek marked this conversation as resolved.
|
||
| iouReport, | ||
| policy, | ||
| ownerLogin, | ||
| userBillingGracePeriodEnds, | ||
| iouReportNextStep, | ||
| amountOwed, | ||
| ownerBillingGracePeriodEnd, | ||
| delegateEmail, | ||
| }; | ||
| } | ||
|
|
||
| export default useReportPreviewActionButtonData; | ||
Uh oh!
There was an error while loading. Please reload this page.