|
| 1 | +import React, {useCallback} from 'react'; |
| 2 | +import type {FormOnyxValues} from '@components/Form/types'; |
| 3 | +import {useSearchStateContext} from '@components/Search/SearchContext'; |
| 4 | +import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; |
| 5 | +import useLocalize from '@hooks/useLocalize'; |
| 6 | +import useOnboardingTaskInformation from '@hooks/useOnboardingTaskInformation'; |
| 7 | +import useOnyx from '@hooks/useOnyx'; |
| 8 | +import usePermissions from '@hooks/usePermissions'; |
| 9 | +import usePolicyForTransaction from '@hooks/usePolicyForTransaction'; |
| 10 | +import useRestartOnReceiptFailure from '@hooks/useRestartOnReceiptFailure'; |
| 11 | +import {getIOURequestPolicyID, setMoneyRequestCategory} from '@libs/actions/IOU'; |
| 12 | +import {setDraftSplitTransaction} from '@libs/actions/IOU/Split'; |
| 13 | +import {updateMoneyRequestCategory} from '@libs/actions/IOU/UpdateMoneyRequest'; |
| 14 | +import {createPolicyCategory} from '@libs/actions/Policy/Category'; |
| 15 | +import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; |
| 16 | +import Navigation from '@libs/Navigation/Navigation'; |
| 17 | +import {hasTags} from '@libs/PolicyUtils'; |
| 18 | +import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; |
| 19 | +import CategoryForm from '@pages/workspace/categories/CategoryForm'; |
| 20 | +import CONST from '@src/CONST'; |
| 21 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 22 | +import ROUTES from '@src/ROUTES'; |
| 23 | +import type SCREENS from '@src/SCREENS'; |
| 24 | +import StepScreenWrapper from './StepScreenWrapper'; |
| 25 | +import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; |
| 26 | +import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; |
| 27 | +import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; |
| 28 | +import withWritableReportOrNotFound from './withWritableReportOrNotFound'; |
| 29 | + |
| 30 | +type IOURequestStepCategoryCreateProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_CATEGORY_CREATE> & |
| 31 | + WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_CATEGORY_CREATE>; |
| 32 | + |
| 33 | +function IOURequestStepCategoryCreate({ |
| 34 | + report: reportReal, |
| 35 | + reportDraft, |
| 36 | + route: { |
| 37 | + params: {transactionID, action, iouType, reportID, backTo}, |
| 38 | + }, |
| 39 | + transaction, |
| 40 | +}: IOURequestStepCategoryCreateProps) { |
| 41 | + const {translate} = useLocalize(); |
| 42 | + const currentUserPersonalDetails = useCurrentUserPersonalDetails(); |
| 43 | + const {isBetaEnabled} = usePermissions(); |
| 44 | + const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); |
| 45 | + const {currentSearchHash} = useSearchStateContext(); |
| 46 | + |
| 47 | + const isEditing = action === CONST.IOU.ACTION.EDIT; |
| 48 | + const isEditingSplit = (iouType === CONST.IOU.TYPE.SPLIT || iouType === CONST.IOU.TYPE.SPLIT_EXPENSE) && isEditing; |
| 49 | + |
| 50 | + const policyIdReal = getIOURequestPolicyID(transaction, reportReal); |
| 51 | + const policyIdDraft = getIOURequestPolicyID(transaction, reportDraft); |
| 52 | + const {policy} = usePolicyForTransaction({ |
| 53 | + transaction, |
| 54 | + reportPolicyID: policyIdReal ?? policyIdDraft, |
| 55 | + action, |
| 56 | + iouType, |
| 57 | + isPerDiemRequest: false, |
| 58 | + }); |
| 59 | + const policyID = policy?.id; |
| 60 | + |
| 61 | + const [splitDraftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`); |
| 62 | + const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`); |
| 63 | + const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`); |
| 64 | + const [policyRecentlyUsedCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`); |
| 65 | + const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(reportReal?.parentReportID ?? reportDraft?.parentReportID)}`); |
| 66 | + const [parentReportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${getNonEmptyStringOnyxID(reportReal?.parentReportID ?? reportDraft?.parentReportID)}`); |
| 67 | + |
| 68 | + const report = reportReal ?? reportDraft; |
| 69 | + |
| 70 | + useRestartOnReceiptFailure(transaction, reportID, iouType, action); |
| 71 | + |
| 72 | + const policyHasTags = hasTags(policyTags); |
| 73 | + |
| 74 | + const { |
| 75 | + taskReport: setupCategoryTaskReport, |
| 76 | + taskParentReport: setupCategoryTaskParentReport, |
| 77 | + isOnboardingTaskParentReportArchived: isSetupCategoryTaskParentReportArchived, |
| 78 | + hasOutstandingChildTask, |
| 79 | + parentReportAction, |
| 80 | + } = useOnboardingTaskInformation(CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES); |
| 81 | + |
| 82 | + const { |
| 83 | + taskReport: setupCategoriesAndTagsTaskReport, |
| 84 | + taskParentReport: setupCategoriesAndTagsTaskParentReport, |
| 85 | + isOnboardingTaskParentReportArchived: isSetupCategoriesAndTagsTaskParentReportArchived, |
| 86 | + hasOutstandingChildTask: setupCategoriesAndTagsHasOutstandingChildTask, |
| 87 | + parentReportAction: setupCategoriesAndTagsParentReportAction, |
| 88 | + } = useOnboardingTaskInformation(CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES_AND_TAGS); |
| 89 | + |
| 90 | + const createCategory = useCallback( |
| 91 | + (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => { |
| 92 | + const categoryName = values.categoryName.trim(); |
| 93 | + |
| 94 | + if (!policyID) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // 1. Create the category in the workspace (optimistic update, queued API call). |
| 99 | + createPolicyCategory({ |
| 100 | + policyID, |
| 101 | + categoryName, |
| 102 | + isSetupCategoriesTaskParentReportArchived: isSetupCategoryTaskParentReportArchived, |
| 103 | + setupCategoryTaskReport, |
| 104 | + setupCategoryTaskParentReport, |
| 105 | + currentUserAccountID: currentUserPersonalDetails.accountID, |
| 106 | + hasOutstandingChildTask, |
| 107 | + parentReportAction, |
| 108 | + setupCategoriesAndTagsTaskReport, |
| 109 | + setupCategoriesAndTagsTaskParentReport, |
| 110 | + isSetupCategoriesAndTagsTaskParentReportArchived, |
| 111 | + setupCategoriesAndTagsHasOutstandingChildTask, |
| 112 | + setupCategoriesAndTagsParentReportAction, |
| 113 | + policyHasTags, |
| 114 | + }); |
| 115 | + |
| 116 | + // 2. Apply the newly created category to the transaction. |
| 117 | + const policyCategoriesWithNewCategory = { |
| 118 | + ...policyCategories, |
| 119 | + [categoryName]: { |
| 120 | + name: categoryName, |
| 121 | + enabled: true, |
| 122 | + errors: null, |
| 123 | + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, |
| 124 | + }, |
| 125 | + }; |
| 126 | + |
| 127 | + if (isEditingSplit && transaction) { |
| 128 | + setDraftSplitTransaction(transaction.transactionID, splitDraftTransaction, {category: categoryName}, policy); |
| 129 | + } else if (isEditing && report) { |
| 130 | + updateMoneyRequestCategory({ |
| 131 | + transactionID: transaction?.transactionID ?? transactionID, |
| 132 | + transactionThreadReport: report, |
| 133 | + parentReport, |
| 134 | + parentReportNextStep, |
| 135 | + category: categoryName, |
| 136 | + policy, |
| 137 | + policyTagList: policyTags, |
| 138 | + policyCategories: policyCategoriesWithNewCategory, |
| 139 | + policyRecentlyUsedCategories, |
| 140 | + currentUserAccountIDParam: currentUserPersonalDetails.accountID, |
| 141 | + currentUserEmailParam: currentUserPersonalDetails.login ?? '', |
| 142 | + isASAPSubmitBetaEnabled, |
| 143 | + hash: currentSearchHash, |
| 144 | + }); |
| 145 | + } else { |
| 146 | + setMoneyRequestCategory(transactionID, categoryName, policy); |
| 147 | + } |
| 148 | + |
| 149 | + if (isEditing) { |
| 150 | + Navigation.goBack(backTo); |
| 151 | + } else { |
| 152 | + Navigation.goBack(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(action, iouType, transactionID, reportID)); |
| 153 | + } |
| 154 | + }, |
| 155 | + [ |
| 156 | + action, |
| 157 | + backTo, |
| 158 | + currentSearchHash, |
| 159 | + currentUserPersonalDetails.accountID, |
| 160 | + currentUserPersonalDetails.login, |
| 161 | + hasOutstandingChildTask, |
| 162 | + isASAPSubmitBetaEnabled, |
| 163 | + isEditing, |
| 164 | + isEditingSplit, |
| 165 | + isSetupCategoriesAndTagsTaskParentReportArchived, |
| 166 | + isSetupCategoryTaskParentReportArchived, |
| 167 | + iouType, |
| 168 | + parentReport, |
| 169 | + parentReportAction, |
| 170 | + parentReportNextStep, |
| 171 | + policy, |
| 172 | + policyCategories, |
| 173 | + policyHasTags, |
| 174 | + policyID, |
| 175 | + policyRecentlyUsedCategories, |
| 176 | + policyTags, |
| 177 | + report, |
| 178 | + reportID, |
| 179 | + setupCategoriesAndTagsHasOutstandingChildTask, |
| 180 | + setupCategoriesAndTagsParentReportAction, |
| 181 | + setupCategoriesAndTagsTaskParentReport, |
| 182 | + setupCategoriesAndTagsTaskReport, |
| 183 | + setupCategoryTaskParentReport, |
| 184 | + setupCategoryTaskReport, |
| 185 | + splitDraftTransaction, |
| 186 | + transaction, |
| 187 | + transactionID, |
| 188 | + ], |
| 189 | + ); |
| 190 | + |
| 191 | + return ( |
| 192 | + <AccessOrNotFoundWrapper |
| 193 | + accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]} |
| 194 | + policyID={policyID} |
| 195 | + featureName={CONST.POLICY.MORE_FEATURES.ARE_CATEGORIES_ENABLED} |
| 196 | + > |
| 197 | + <StepScreenWrapper |
| 198 | + headerTitle={translate('workspace.categories.addCategory')} |
| 199 | + onBackButtonPress={() => Navigation.goBack()} |
| 200 | + shouldShowWrapper |
| 201 | + testID="IOURequestStepCategoryCreate" |
| 202 | + > |
| 203 | + <CategoryForm |
| 204 | + onSubmit={createCategory} |
| 205 | + policyCategories={policyCategories} |
| 206 | + /> |
| 207 | + </StepScreenWrapper> |
| 208 | + </AccessOrNotFoundWrapper> |
| 209 | + ); |
| 210 | +} |
| 211 | + |
| 212 | +const IOURequestStepCategoryCreateWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepCategoryCreate); |
| 213 | +const IOURequestStepCategoryCreateWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepCategoryCreateWithFullTransactionOrNotFound); |
| 214 | +export default IOURequestStepCategoryCreateWithWritableReportOrNotFound; |
0 commit comments