From 51b13bd9698dd15457beb527e780cc4131f42b4e Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Fri, 27 Dec 2024 18:44:21 +0530 Subject: [PATCH] Fix eslint --- src/components/DestinationPicker.tsx | 6 +++++- src/libs/PerDiemRequestUtils.ts | 14 ++++++++++---- src/pages/iou/request/IOURequestStartPage.tsx | 2 +- .../iou/request/step/IOURequestStepDestination.tsx | 13 ++++++++----- .../step/IOURequestStepPerDiemWorkspace.tsx | 5 ++++- .../iou/request/step/IOURequestStepSubrate.tsx | 8 ++++---- src/pages/iou/request/step/IOURequestStepTime.tsx | 2 +- .../request/step/withFullTransactionOrNotFound.tsx | 2 +- .../request/step/withWritableReportOrNotFound.tsx | 4 ++-- .../workspace/perDiem/ImportedPerDiemPage.tsx | 4 ++-- 10 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/components/DestinationPicker.tsx b/src/components/DestinationPicker.tsx index 1063d35edf0c..ea0acf4a1c58 100644 --- a/src/components/DestinationPicker.tsx +++ b/src/components/DestinationPicker.tsx @@ -34,9 +34,13 @@ function DestinationPicker({selectedDestination, policyID, onSubmit}: Destinatio const selectedRate = customUnit?.rates?.[selectedDestination]; + if (!selectedRate?.customUnitRateID) { + return []; + } + return [ { - rateID: selectedRate?.customUnitRateID ?? '', + rateID: selectedRate.customUnitRateID, name: selectedRate?.name ?? '', currency: selectedRate?.currency ?? CONST.CURRENCY.USD, isSelected: true, diff --git a/src/libs/PerDiemRequestUtils.ts b/src/libs/PerDiemRequestUtils.ts index d34c53adc38c..ad82bc606716 100644 --- a/src/libs/PerDiemRequestUtils.ts +++ b/src/libs/PerDiemRequestUtils.ts @@ -26,7 +26,7 @@ Onyx.connect({ function getCustomUnitID(reportID: string) { const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`]; - const policy = PolicyUtils.getPolicy(report?.policyID ?? parentReport?.policyID ?? '-1'); + const policy = PolicyUtils.getPolicy(report?.policyID ?? parentReport?.policyID); let customUnitID: string = CONST.CUSTOM_UNITS.FAKE_P2P_ID; let category: string | undefined; @@ -103,7 +103,7 @@ function getDestinationListSections({ }): DestinationTreeSection[] { const sortedDestinations: Destination[] = lodashSortBy(destinations, 'name').map((rate) => ({ name: rate.name ?? '', - rateID: rate.customUnitRateID ?? '', + rateID: rate.customUnitRateID, currency: rate.currency ?? CONST.CURRENCY.USD, })); const destinationSections: DestinationTreeSection[] = []; @@ -187,13 +187,19 @@ function getDestinationListSections({ } function getDestinationForDisplay(customUnit: CustomUnit | undefined, transaction: OnyxEntry) { - const customUnitRateID = transaction?.comment?.customUnit?.customUnitRateID ?? ''; + const customUnitRateID = transaction?.comment?.customUnit?.customUnitRateID; + if (!customUnitRateID) { + return ''; + } const selectedDestination = customUnit?.rates?.[customUnitRateID]; return selectedDestination?.name ?? ''; } function getSubratesFields(customUnit: CustomUnit | undefined, transaction: OnyxEntry) { - const customUnitRateID = transaction?.comment?.customUnit?.customUnitRateID ?? ''; + const customUnitRateID = transaction?.comment?.customUnit?.customUnitRateID; + if (!customUnitRateID) { + return []; + } const selectedDestination = customUnit?.rates?.[customUnitRateID]; const countSubrates = selectedDestination?.subRates?.length ?? 0; const currentSubrates = transaction?.comment?.customUnit?.subRates ?? []; diff --git a/src/pages/iou/request/IOURequestStartPage.tsx b/src/pages/iou/request/IOURequestStartPage.tsx index ffe5e793741e..36351b0bc7fb 100644 --- a/src/pages/iou/request/IOURequestStartPage.tsx +++ b/src/pages/iou/request/IOURequestStartPage.tsx @@ -50,7 +50,7 @@ function IOURequestStartPage({ const [session] = useOnyx(ONYXKEYS.SESSION); const isLoadingSelectedTab = shouldUseTab ? isLoadingOnyxValue(selectedTabResult) : false; // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${route?.params.transactionID || -1}`); + const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${route?.params.transactionID || CONST.DEFAULT_NUMBER_ID}`); const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); const {canUseCombinedTrackSubmit, canUsePerDiem} = usePermissions(); diff --git a/src/pages/iou/request/step/IOURequestStepDestination.tsx b/src/pages/iou/request/step/IOURequestStepDestination.tsx index 1a2b8814954d..cdbb525453b9 100644 --- a/src/pages/iou/request/step/IOURequestStepDestination.tsx +++ b/src/pages/iou/request/step/IOURequestStepDestination.tsx @@ -47,7 +47,7 @@ function IOURequestStepDestination({ }: IOURequestStepDestinationProps) { const [policy, policyMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${explictPolicyID ?? IOU.getIOURequestPolicyID(transaction, report)}`); const {accountID} = useCurrentUserPersonalDetails(); - const policyExpenseReport = ReportUtils.getPolicyExpenseChat(accountID, policy?.id ?? '-1'); + const policyExpenseReport = policy?.id ? ReportUtils.getPolicyExpenseChat(accountID, policy.id) : undefined; const customUnit = PolicyUtils.getPerDiemCustomUnit(policy); const selectedDestination = transaction?.comment?.customUnit?.customUnitRateID; @@ -70,10 +70,13 @@ function IOURequestStepDestination({ }; const updateDestination = (destination: ListItem & {currency: string}) => { + if (isEmptyObject(customUnit)) { + return; + } if (selectedDestination !== destination.keyForList) { if (openedFromStartPage) { IOU.setMoneyRequestParticipantsFromReport(transactionID, policyExpenseReport); - IOU.setCustomUnitID(transactionID, customUnit?.customUnitID ?? ''); + IOU.setCustomUnitID(transactionID, customUnit.customUnitID); IOU.setMoneyRequestCategory(transactionID, customUnit?.defaultCategory ?? ''); } IOU.setCustomUnitRateID(transactionID, destination.keyForList ?? ''); @@ -132,7 +135,7 @@ function IOURequestStepDestination({ style={[styles.w100]} onPress={() => { InteractionManager.runAfterInteractions(() => { - Navigation.navigate(ROUTES.WORKSPACE_PER_DIEM.getRoute(policy?.id ?? '-1')); + Navigation.navigate(ROUTES.WORKSPACE_PER_DIEM.getRoute(policy.id)); }); }} text={translate('workspace.perDiem.editPerDiemRates')} @@ -142,10 +145,10 @@ function IOURequestStepDestination({ )} )} - {!shouldShowEmptyState && !isLoading && !shouldShowOfflineView && ( + {!shouldShowEmptyState && !isLoading && !shouldShowOfflineView && !!policy?.id && ( )} diff --git a/src/pages/iou/request/step/IOURequestStepPerDiemWorkspace.tsx b/src/pages/iou/request/step/IOURequestStepPerDiemWorkspace.tsx index 34f7dbbb8e26..812b71f0a8eb 100644 --- a/src/pages/iou/request/step/IOURequestStepPerDiemWorkspace.tsx +++ b/src/pages/iou/request/step/IOURequestStepPerDiemWorkspace.tsx @@ -61,7 +61,10 @@ function IOURequestStepPerDiemWorkspace({ }, [allPolicies, currentUserLogin, selectedWorkspace]); const selectWorkspace = (item: WorkspaceListItem) => { - const policyExpenseReportID = ReportUtils.getPolicyExpenseChat(accountID, item.value)?.reportID ?? '-1'; + const policyExpenseReportID = ReportUtils.getPolicyExpenseChat(accountID, item.value)?.reportID; + if (!policyExpenseReportID) { + return; + } const selectedPolicy = PolicyUtils.getPolicy(item.value, allPolicies); const perDiemUnit = PolicyUtils.getPerDiemCustomUnit(selectedPolicy); IOU.setMoneyRequestParticipants(transactionID, [ diff --git a/src/pages/iou/request/step/IOURequestStepSubrate.tsx b/src/pages/iou/request/step/IOURequestStepSubrate.tsx index 2e052764fc8b..a0969c7715af 100644 --- a/src/pages/iou/request/step/IOURequestStepSubrate.tsx +++ b/src/pages/iou/request/step/IOURequestStepSubrate.tsx @@ -1,7 +1,6 @@ import {useNavigation} from '@react-navigation/native'; import React, {useState} from 'react'; import {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import type {OnyxEntry} from 'react-native-onyx'; import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; import ConfirmModal from '@components/ConfirmModal'; @@ -18,6 +17,7 @@ import ValuePicker from '@components/ValuePicker'; import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useLocalize from '@hooks/useLocalize'; import usePermissions from '@hooks/usePermissions'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; import * as ErrorUtils from '@libs/ErrorUtils'; @@ -69,7 +69,7 @@ function IOURequestStepWaypoint({ }: IOURequestStepWaypointProps) { const styles = useThemeStyles(); const {canUseCombinedTrackSubmit} = usePermissions(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID ?? '-1'}`); + const policy = usePolicy(report?.policyID); const customUnit = PolicyUtils.getPerDiemCustomUnit(policy); const {windowWidth} = useWindowDimensions(); const [isDeleteStopModalOpen, setIsDeleteStopModalOpen] = useState(false); @@ -79,9 +79,9 @@ function IOURequestStepWaypoint({ const {translate} = useLocalize(); const {inputCallbackRef} = useAutoFocusInput(); const parsedIndex = parseInt(pageIndex, 10); - const selectedDestination = transaction?.comment?.customUnit?.customUnitRateID ?? ''; + const selectedDestination = transaction?.comment?.customUnit?.customUnitRateID; const allSubrates = transaction?.comment?.customUnit?.subRates ?? []; - const allPossibleSubrates = customUnit?.rates?.[selectedDestination]?.subRates ?? []; + const allPossibleSubrates = selectedDestination ? customUnit?.rates?.[selectedDestination]?.subRates ?? [] : []; const currentSubrate: CommentSubrate | undefined = allSubrates.at(parsedIndex) ?? undefined; const totalSubrateCount = allPossibleSubrates.length; const filledSubrateCount = allSubrates.length; diff --git a/src/pages/iou/request/step/IOURequestStepTime.tsx b/src/pages/iou/request/step/IOURequestStepTime.tsx index c8c793e70566..d5186dd0f36e 100644 --- a/src/pages/iou/request/step/IOURequestStepTime.tsx +++ b/src/pages/iou/request/step/IOURequestStepTime.tsx @@ -78,7 +78,7 @@ function IOURequestStepTime({ const newStart = DateUtils.combineDateAndTime(value.startTime, value.startDate); const newEnd = DateUtils.combineDateAndTime(value.endTime, value.endDate); - IOU.setMoneyRequestDateAttribute(transaction?.transactionID ?? '-1', newStart, newEnd); + IOU.setMoneyRequestDateAttribute(transactionID, newStart, newEnd); if (backTo) { navigateBack(); diff --git a/src/pages/iou/request/step/withFullTransactionOrNotFound.tsx b/src/pages/iou/request/step/withFullTransactionOrNotFound.tsx index a1cc2b6f9ebd..5aa581426b50 100644 --- a/src/pages/iou/request/step/withFullTransactionOrNotFound.tsx +++ b/src/pages/iou/request/step/withFullTransactionOrNotFound.tsx @@ -54,7 +54,7 @@ export default function , ref: ForwardedRef) { const {route} = props; - const transactionID = route.params.transactionID ?? -1; + const transactionID = route.params.transactionID; const userAction = 'action' in route.params && route.params.action ? route.params.action : CONST.IOU.ACTION.CREATE; const shouldUseTransactionDraft = IOUUtils.shouldUseTransactionDraft(userAction); diff --git a/src/pages/iou/request/step/withWritableReportOrNotFound.tsx b/src/pages/iou/request/step/withWritableReportOrNotFound.tsx index a3ec612a5bab..9ee3740be2a1 100644 --- a/src/pages/iou/request/step/withWritableReportOrNotFound.tsx +++ b/src/pages/iou/request/step/withWritableReportOrNotFound.tsx @@ -56,9 +56,9 @@ export default function , ref: ForwardedRef) { const {route} = props; - const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID ?? '-1'}`); + const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID}`); const [isLoadingApp = true] = useOnyx(ONYXKEYS.IS_LOADING_APP); - const [reportDraft] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${route.params.reportID ?? '-1'}`); + const [reportDraft] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${route.params.reportID}`); const iouTypeParamIsInvalid = !Object.values(CONST.IOU.TYPE) .filter((type) => shouldIncludeDeprecatedIOUType || (type !== CONST.IOU.TYPE.REQUEST && type !== CONST.IOU.TYPE.SEND)) diff --git a/src/pages/workspace/perDiem/ImportedPerDiemPage.tsx b/src/pages/workspace/perDiem/ImportedPerDiemPage.tsx index 88ad41a8aeb7..77df21162641 100644 --- a/src/pages/workspace/perDiem/ImportedPerDiemPage.tsx +++ b/src/pages/workspace/perDiem/ImportedPerDiemPage.tsx @@ -98,7 +98,7 @@ function ImportedPerDiemPage({route}: ImportedPerDiemPageProps) { const importPerDiemRates = useCallback(() => { setIsValidationEnabled(true); const errors = validate(); - if (Object.keys(errors).length > 0) { + if (Object.keys(errors).length > 0 || !perDiemCustomUnit?.customUnitID) { return; } @@ -122,7 +122,7 @@ function ImportedPerDiemPage({route}: ImportedPerDiemPageProps) { if (perDiemUnits) { setIsImportingPerDiemRates(true); - PerDiem.importPerDiemRates(policyID, perDiemCustomUnit?.customUnitID ?? '', perDiemUnits, rowsLength); + PerDiem.importPerDiemRates(policyID, perDiemCustomUnit.customUnitID, perDiemUnits, rowsLength); } }, [validate, spreadsheet?.columns, spreadsheet?.data, containsHeader, policyID, perDiemCustomUnit?.customUnitID]);