From e5786c93aadd84a750510fa1d2cccaf2216e4e33 Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 7 Aug 2024 17:42:46 +0700 Subject: [PATCH 1/5] feat: create usePolicy hook --- .../ConnectToNetSuiteButton/index.tsx | 5 ++--- .../ConnectToSageIntacctButton/index.tsx | 5 ++--- src/hooks/usePolicy.ts | 17 +++++++++++++++++ .../createCustomBottomTabNavigator/TopBar.tsx | 3 ++- .../WorkspaceAdminRestrictedAction.tsx | 5 ++--- .../Workspace/WorkspaceRestrictedActionPage.tsx | 3 ++- .../Workspace/WorkspaceUserRestrictedAction.tsx | 5 ++--- src/pages/Travel/ManageTrips.tsx | 3 ++- .../report/ReportActionItemContentCreated.tsx | 3 ++- src/pages/iou/request/IOURequestStartPage.tsx | 4 ++-- .../request/step/IOURequestStepCompanyInfo.tsx | 3 ++- .../export/SageIntacctDefaultVendorPage.tsx | 5 ++--- .../SageIntacctEditUserDimensionsPage.tsx | 5 ++--- .../import/SageIntacctMappingsTypePage.tsx | 5 ++--- .../import/SageIntacctToggleMappingsPage.tsx | 5 ++--- .../categories/CategorySettingsPage.tsx | 5 +++-- .../categories/WorkspaceCategoriesPage.tsx | 3 ++- .../distanceRates/PolicyDistanceRatesPage.tsx | 3 ++- .../expensifyCard/WorkspaceCardsListLabel.tsx | 3 ++- .../WorkspaceEditCardLimitTypePage.tsx | 3 ++- .../WorkspaceExpensifyCardListPage.tsx | 5 ++--- .../reportFields/WorkspaceReportFieldsPage.tsx | 3 ++- src/pages/workspace/tags/TagSettingsPage.tsx | 5 +++-- src/pages/workspace/tags/WorkspaceTagsPage.tsx | 3 ++- .../workspace/tags/WorkspaceViewTagsPage.tsx | 2 +- .../workspace/taxes/WorkspaceTaxCodePage.tsx | 4 ++-- 26 files changed, 68 insertions(+), 47 deletions(-) create mode 100644 src/hooks/usePolicy.ts diff --git a/src/components/ConnectToNetSuiteButton/index.tsx b/src/components/ConnectToNetSuiteButton/index.tsx index 928bc01f12c1..24c19689da23 100644 --- a/src/components/ConnectToNetSuiteButton/index.tsx +++ b/src/components/ConnectToNetSuiteButton/index.tsx @@ -1,12 +1,12 @@ import React, {useRef, useState} from 'react'; import type {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import AccountingConnectionConfirmationModal from '@components/AccountingConnectionConfirmationModal'; import Button from '@components/Button'; import * as Expensicons from '@components/Icon/Expensicons'; import PopoverMenu from '@components/PopoverMenu'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; import {removePolicyConnection} from '@libs/actions/connections'; @@ -15,7 +15,6 @@ import Navigation from '@libs/Navigation/Navigation'; import {isControlPolicy} from '@libs/PolicyUtils'; import type {AnchorPosition} from '@styles/index'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {ConnectToNetSuiteButtonProps} from './types'; @@ -23,7 +22,7 @@ function ConnectToNetSuiteButton({policyID, shouldDisconnectIntegrationBeforeCon const styles = useThemeStyles(); const {translate} = useLocalize(); const {isOffline} = useNetwork(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false); diff --git a/src/components/ConnectToSageIntacctButton/index.tsx b/src/components/ConnectToSageIntacctButton/index.tsx index 6c6523ad6e75..fa6b26216c4c 100644 --- a/src/components/ConnectToSageIntacctButton/index.tsx +++ b/src/components/ConnectToSageIntacctButton/index.tsx @@ -1,12 +1,12 @@ import React, {useRef, useState} from 'react'; import type {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import AccountingConnectionConfirmationModal from '@components/AccountingConnectionConfirmationModal'; import Button from '@components/Button'; import * as Expensicons from '@components/Icon/Expensicons'; import PopoverMenu from '@components/PopoverMenu'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; import {removePolicyConnection} from '@libs/actions/connections'; @@ -15,7 +15,6 @@ import Navigation from '@libs/Navigation/Navigation'; import {isControlPolicy} from '@libs/PolicyUtils'; import type {AnchorPosition} from '@styles/index'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {PolicyConnectionName} from '@src/types/onyx/Policy'; @@ -30,7 +29,7 @@ function ConnectToSageIntacctButton({policyID, shouldDisconnectIntegrationBefore const {translate} = useLocalize(); const {isOffline} = useNetwork(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false); diff --git a/src/hooks/usePolicy.ts b/src/hooks/usePolicy.ts new file mode 100644 index 000000000000..7c8bf1c4c6cf --- /dev/null +++ b/src/hooks/usePolicy.ts @@ -0,0 +1,17 @@ +import {useOnyx} from 'react-native-onyx'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; + +function usePolicy(policyID?: string) { + const getPolicyID = () => { + if (!policyID || policyID === CONST.POLICY.OWNER_EMAIL_FAKE || policyID === '-1') { + return '-1'; + } + return policyID; + }; + + const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getPolicyID()}`); + return policy; +} + +export default usePolicy; diff --git a/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/TopBar.tsx b/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/TopBar.tsx index 0f4c41b9cbfc..dc3ab4c48c08 100644 --- a/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/TopBar.tsx +++ b/src/libs/Navigation/AppNavigator/createCustomBottomTabNavigator/TopBar.tsx @@ -8,6 +8,7 @@ import {PressableWithoutFeedback} from '@components/Pressable'; import Tooltip from '@components/Tooltip'; import WorkspaceSwitcherButton from '@components/WorkspaceSwitcherButton'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; @@ -25,7 +26,7 @@ function TopBar({breadcrumbLabel, activeWorkspaceID, shouldDisplaySearch = true} const styles = useThemeStyles(); const theme = useTheme(); const {translate} = useLocalize(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activeWorkspaceID}`); + const policy = usePolicy(activeWorkspaceID); const [session] = useOnyx(ONYXKEYS.SESSION, {selector: (sessionValue) => sessionValue && {authTokenType: sessionValue.authTokenType}}); const isAnonymousUser = Session.isAnonymousUser(session); diff --git a/src/pages/RestrictedAction/Workspace/WorkspaceAdminRestrictedAction.tsx b/src/pages/RestrictedAction/Workspace/WorkspaceAdminRestrictedAction.tsx index b8880f372809..89b5dcdd8a2b 100644 --- a/src/pages/RestrictedAction/Workspace/WorkspaceAdminRestrictedAction.tsx +++ b/src/pages/RestrictedAction/Workspace/WorkspaceAdminRestrictedAction.tsx @@ -1,6 +1,5 @@ import React, {useCallback} from 'react'; import {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import Button from '@components/Button'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import * as Illustrations from '@components/Icon/Illustrations'; @@ -9,11 +8,11 @@ import ScreenWrapper from '@components/ScreenWrapper'; import ScrollView from '@components/ScrollView'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; import * as PolicyUtils from '@libs/PolicyUtils'; import variables from '@styles/variables'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; type WorkspaceAdminRestrictedActionProps = { @@ -22,7 +21,7 @@ type WorkspaceAdminRestrictedActionProps = { function WorkspaceAdminRestrictedAction({policyID}: WorkspaceAdminRestrictedActionProps) { const {translate} = useLocalize(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const styles = useThemeStyles(); const openAdminsReport = useCallback(() => { diff --git a/src/pages/RestrictedAction/Workspace/WorkspaceRestrictedActionPage.tsx b/src/pages/RestrictedAction/Workspace/WorkspaceRestrictedActionPage.tsx index 42b1e64d8282..a9cee3236ad0 100644 --- a/src/pages/RestrictedAction/Workspace/WorkspaceRestrictedActionPage.tsx +++ b/src/pages/RestrictedAction/Workspace/WorkspaceRestrictedActionPage.tsx @@ -1,6 +1,7 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React from 'react'; import {useOnyx} from 'react-native-onyx'; +import usePolicy from '@hooks/usePolicy'; import type {RestrictedActionParamList} from '@libs/Navigation/types'; import * as PolicyUtils from '@libs/PolicyUtils'; import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; @@ -18,7 +19,7 @@ function WorkspaceRestrictedActionPage({ }, }: WorkspaceRestrictedActionPageProps) { const [session] = useOnyx(ONYXKEYS.SESSION); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); // Workspace Owner if (PolicyUtils.isPolicyOwner(policy, session?.accountID ?? -1)) { diff --git a/src/pages/RestrictedAction/Workspace/WorkspaceUserRestrictedAction.tsx b/src/pages/RestrictedAction/Workspace/WorkspaceUserRestrictedAction.tsx index 4d2aabd8774e..386dd96523ed 100644 --- a/src/pages/RestrictedAction/Workspace/WorkspaceUserRestrictedAction.tsx +++ b/src/pages/RestrictedAction/Workspace/WorkspaceUserRestrictedAction.tsx @@ -1,6 +1,5 @@ import React, {useCallback} from 'react'; import {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import Button from '@components/Button'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import * as Illustrations from '@components/Icon/Illustrations'; @@ -9,11 +8,11 @@ import ScreenWrapper from '@components/ScreenWrapper'; import ScrollView from '@components/ScrollView'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; import * as ReportUtils from '@libs/ReportUtils'; import variables from '@styles/variables'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; type WorkspaceUserRestrictedActionProps = { @@ -22,7 +21,7 @@ type WorkspaceUserRestrictedActionProps = { function WorkspaceUserRestrictedAction({policyID}: WorkspaceUserRestrictedActionProps) { const {translate} = useLocalize(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const styles = useThemeStyles(); const openPolicyExpenseReport = useCallback(() => { diff --git a/src/pages/Travel/ManageTrips.tsx b/src/pages/Travel/ManageTrips.tsx index ee9e06e186af..c61166f10c6d 100644 --- a/src/pages/Travel/ManageTrips.tsx +++ b/src/pages/Travel/ManageTrips.tsx @@ -7,6 +7,7 @@ import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; import * as Illustrations from '@components/Icon/Illustrations'; import ScrollView from '@components/ScrollView'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; @@ -34,7 +35,7 @@ function ManageTrips() { const {translate} = useLocalize(); const [travelSettings] = useOnyx(ONYXKEYS.NVP_TRAVEL_SETTINGS); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`); + const policy = usePolicy(activePolicyID); const [ctaErrorMessage, setCtaErrorMessage] = useState(''); diff --git a/src/pages/home/report/ReportActionItemContentCreated.tsx b/src/pages/home/report/ReportActionItemContentCreated.tsx index 7d1f27920bdf..d5aa07c3b2db 100644 --- a/src/pages/home/report/ReportActionItemContentCreated.tsx +++ b/src/pages/home/report/ReportActionItemContentCreated.tsx @@ -13,6 +13,7 @@ import type {ShowContextMenuContextProps} from '@components/ShowContextMenuConte import SpacerView from '@components/SpacerView'; import UnreadActionIndicator from '@components/UnreadActionIndicator'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; @@ -52,7 +53,7 @@ function ReportActionItemContentCreated({contextValue, parentReportAction, trans const {report, action, transactionThreadReport} = contextValue; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report.policyID === CONST.POLICY.OWNER_EMAIL_FAKE ? '-1' : report.policyID ?? '-1'}`); + const policy = usePolicy(report.policyID === CONST.POLICY.OWNER_EMAIL_FAKE ? '-1' : report.policyID ?? '-1'); const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID ?? '-1'}`); const transactionCurrency = TransactionUtils.getCurrency(transaction); diff --git a/src/pages/iou/request/IOURequestStartPage.tsx b/src/pages/iou/request/IOURequestStartPage.tsx index 0b4b00674e34..3cffce981b73 100644 --- a/src/pages/iou/request/IOURequestStartPage.tsx +++ b/src/pages/iou/request/IOURequestStartPage.tsx @@ -9,6 +9,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import TabSelector from '@components/TabSelector/TabSelector'; import useLocalize from '@hooks/useLocalize'; import usePermissions from '@hooks/usePermissions'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as DeviceCapabilities from '@libs/DeviceCapabilities'; import * as KeyDownPressListener from '@libs/KeyboardShortcut/KeyDownPressListener'; @@ -40,8 +41,7 @@ function IOURequestStartPage({ const {translate} = useLocalize(); const [isDraggingOver, setIsDraggingOver] = useState(false); const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`); - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID || -1}`); + const policy = usePolicy(report?.policyID); const [selectedTab] = useOnyx(`${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.IOU_REQUEST_TYPE}`); // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${route?.params.transactionID || -1}`); diff --git a/src/pages/iou/request/step/IOURequestStepCompanyInfo.tsx b/src/pages/iou/request/step/IOURequestStepCompanyInfo.tsx index b1454b76aca6..53d42b42e908 100644 --- a/src/pages/iou/request/step/IOURequestStepCompanyInfo.tsx +++ b/src/pages/iou/request/step/IOURequestStepCompanyInfo.tsx @@ -9,6 +9,7 @@ import TextInput from '@components/TextInput'; import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as CurrencyUtils from '@libs/CurrencyUtils'; import playSound, {SOUNDS} from '@libs/Sound'; @@ -36,7 +37,7 @@ function IOURequestStepCompanyInfo({route, report, transaction}: IOURequestStepC const {inputCallbackRef} = useAutoFocusInput(); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${IOU.getIOURequestPolicyID(transaction, report)}`); + const policy = usePolicy(IOU.getIOURequestPolicyID(transaction, report)); const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${IOU.getIOURequestPolicyID(transaction, report)}`); const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${IOU.getIOURequestPolicyID(transaction, report)}`); diff --git a/src/pages/workspace/accounting/intacct/export/SageIntacctDefaultVendorPage.tsx b/src/pages/workspace/accounting/intacct/export/SageIntacctDefaultVendorPage.tsx index ca9a7c61ce12..dea44fe68856 100644 --- a/src/pages/workspace/accounting/intacct/export/SageIntacctDefaultVendorPage.tsx +++ b/src/pages/workspace/accounting/intacct/export/SageIntacctDefaultVendorPage.tsx @@ -1,7 +1,6 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useCallback, useMemo} from 'react'; import {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import BlockingView from '@components/BlockingViews/BlockingView'; import * as Illustrations from '@components/Icon/Illustrations'; import RadioListItem from '@components/SelectionList/RadioListItem'; @@ -9,6 +8,7 @@ import type {SelectorType} from '@components/SelectionScreen'; import SelectionScreen from '@components/SelectionScreen'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; @@ -18,7 +18,6 @@ import variables from '@styles/variables'; import {updateSageIntacctDefaultVendor} from '@userActions/connections/SageIntacct'; import * as Policy from '@userActions/Policy/Policy'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {Connections} from '@src/types/onyx/Policy'; @@ -30,7 +29,7 @@ function SageIntacctDefaultVendorPage({route}: SageIntacctDefaultVendorPageProps const {translate} = useLocalize(); const policyID = route.params.policyID ?? '-1'; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const {config} = policy?.connections?.intacct ?? {}; const {export: exportConfig} = policy?.connections?.intacct?.config ?? {}; diff --git a/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx b/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx index e9965de0d1ae..15a22c30ce54 100644 --- a/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx +++ b/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx @@ -1,7 +1,6 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useCallback, useState} from 'react'; import {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import ConfirmModal from '@components/ConfirmModal'; import ConnectionLayout from '@components/ConnectionLayout'; import FormProvider from '@components/Form/FormProvider'; @@ -12,6 +11,7 @@ import MenuItem from '@components/MenuItem'; import OfflineWithFeedback from '@components/OfflineWithFeedback'; import TextInput from '@components/TextInput'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import {clearSageIntacctErrorField, editSageIntacctUserDimensions, removeSageIntacctUserDimensions} from '@libs/actions/connections/SageIntacct'; import * as ErrorUtils from '@libs/ErrorUtils'; @@ -19,7 +19,6 @@ import Navigation from '@libs/Navigation/Navigation'; import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; import {settingsPendingAction} from '@libs/PolicyUtils'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import INPUT_IDS from '@src/types/form/SageIntacctDimensionsForm'; @@ -32,7 +31,7 @@ function SageIntacctEditUserDimensionsPage({route}: SageIntacctEditUserDimension const {translate} = useLocalize(); const editedUserDimensionName: string = route.params.dimensionName; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID ?? '-1'}`); + const policy = usePolicy(route.params.policyID); const policyID: string = policy?.id ?? '-1'; const config = policy?.connections?.intacct?.config; const userDimensions = policy?.connections?.intacct?.config?.mappings?.dimensions; diff --git a/src/pages/workspace/accounting/intacct/import/SageIntacctMappingsTypePage.tsx b/src/pages/workspace/accounting/intacct/import/SageIntacctMappingsTypePage.tsx index cfa09fa0dc50..736aba970e7b 100644 --- a/src/pages/workspace/accounting/intacct/import/SageIntacctMappingsTypePage.tsx +++ b/src/pages/workspace/accounting/intacct/import/SageIntacctMappingsTypePage.tsx @@ -1,10 +1,10 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useCallback, useMemo} from 'react'; -import {useOnyx} from 'react-native-onyx'; import RadioListItem from '@components/SelectionList/RadioListItem'; import SelectionScreen from '@components/SelectionScreen'; import type {SelectorType} from '@components/SelectionScreen'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import {updateSageIntacctMappingValue} from '@libs/actions/connections/SageIntacct'; import * as ErrorUtils from '@libs/ErrorUtils'; @@ -13,7 +13,6 @@ import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; import {settingsPendingAction} from '@libs/PolicyUtils'; import * as Policy from '@userActions/Policy/Policy'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {SageIntacctMappingName, SageIntacctMappingValue} from '@src/types/onyx/Policy'; @@ -25,7 +24,7 @@ function SageIntacctMappingsTypePage({route}: SageIntacctMappingsTypePageProps) const styles = useThemeStyles(); const mappingName: SageIntacctMappingName = route.params.mapping; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID ?? '-1'}`); + const policy = usePolicy(route.params.policyID); const policyID = policy?.id ?? '-1'; const {config} = policy?.connections?.intacct ?? {}; diff --git a/src/pages/workspace/accounting/intacct/import/SageIntacctToggleMappingsPage.tsx b/src/pages/workspace/accounting/intacct/import/SageIntacctToggleMappingsPage.tsx index d4f502227144..9d74fa5cee97 100644 --- a/src/pages/workspace/accounting/intacct/import/SageIntacctToggleMappingsPage.tsx +++ b/src/pages/workspace/accounting/intacct/import/SageIntacctToggleMappingsPage.tsx @@ -1,12 +1,12 @@ import type {StackScreenProps} from '@react-navigation/stack'; import {Str} from 'expensify-common'; import React, {useState} from 'react'; -import {useOnyx} from 'react-native-onyx'; import ConnectionLayout from '@components/ConnectionLayout'; import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription'; import OfflineWithFeedback from '@components/OfflineWithFeedback'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import {clearSageIntacctErrorField, updateSageIntacctMappingValue} from '@libs/actions/connections/SageIntacct'; import * as ErrorUtils from '@libs/ErrorUtils'; @@ -16,7 +16,6 @@ import {areSettingsInErrorFields, settingsPendingAction} from '@libs/PolicyUtils import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOptionRow'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {SageIntacctMappingName, SageIntacctMappingValue} from '@src/types/onyx/Policy'; @@ -49,7 +48,7 @@ function SageIntacctToggleMappingsPage({route}: SageIntacctToggleMappingsPagePro const {translate} = useLocalize(); const styles = useThemeStyles(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID ?? '-1'}`); + const policy = usePolicy(route.params.policyID); const mappingName: SageIntacctMappingName = route.params.mapping; const policyID: string = policy?.id ?? '-1'; diff --git a/src/pages/workspace/categories/CategorySettingsPage.tsx b/src/pages/workspace/categories/CategorySettingsPage.tsx index d1d605759d36..836803f9f8fc 100644 --- a/src/pages/workspace/categories/CategorySettingsPage.tsx +++ b/src/pages/workspace/categories/CategorySettingsPage.tsx @@ -1,7 +1,7 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useEffect, useState} from 'react'; import {View} from 'react-native'; -import {useOnyx, withOnyx} from 'react-native-onyx'; +import {withOnyx} from 'react-native-onyx'; import type {OnyxEntry} from 'react-native-onyx'; import ConfirmModal from '@components/ConfirmModal'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; @@ -13,6 +13,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import Switch from '@components/Switch'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; @@ -40,7 +41,7 @@ function CategorySettingsPage({route, policyCategories, navigation}: CategorySet const {translate} = useLocalize(); const [deleteCategoryConfirmModalVisible, setDeleteCategoryConfirmModalVisible] = useState(false); const backTo = route.params?.backTo; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`); + const policy = usePolicy(route.params.policyID); const policyCategory = policyCategories?.[route.params.categoryName] ?? Object.values(policyCategories ?? {}).find((category) => category.previousCategoryName === route.params.categoryName); diff --git a/src/pages/workspace/categories/WorkspaceCategoriesPage.tsx b/src/pages/workspace/categories/WorkspaceCategoriesPage.tsx index 0c3f23e29c61..f0358a097268 100644 --- a/src/pages/workspace/categories/WorkspaceCategoriesPage.tsx +++ b/src/pages/workspace/categories/WorkspaceCategoriesPage.tsx @@ -23,6 +23,7 @@ import TextLink from '@components/TextLink'; import useEnvironment from '@hooks/useEnvironment'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -60,7 +61,7 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) { const {environmentURL} = useEnvironment(); const policyId = route.params.policyID ?? '-1'; const backTo = route.params?.backTo; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyId}`); + const policy = usePolicy(policyId); const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyId}`); const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE); const isConnectedToAccounting = Object.keys(policy?.connections ?? {}).length > 0; diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx index 2eaf8673e29c..8db53fb715b6 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx @@ -17,6 +17,7 @@ import SelectionListWithModal from '@components/SelectionListWithModal'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -51,7 +52,7 @@ function PolicyDistanceRatesPage({ const [isWarningModalVisible, setIsWarningModalVisible] = useState(false); const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false); const isFocused = useIsFocused(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE); const canSelectMultiple = shouldUseNarrowLayout ? selectionMode?.isEnabled : true; diff --git a/src/pages/workspace/expensifyCard/WorkspaceCardsListLabel.tsx b/src/pages/workspace/expensifyCard/WorkspaceCardsListLabel.tsx index 59a6b168d0bc..c0566d71f686 100644 --- a/src/pages/workspace/expensifyCard/WorkspaceCardsListLabel.tsx +++ b/src/pages/workspace/expensifyCard/WorkspaceCardsListLabel.tsx @@ -12,6 +12,7 @@ import Popover from '@components/Popover'; import {PressableWithFeedback} from '@components/Pressable'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -38,7 +39,7 @@ type WorkspaceCardsListLabelProps = { function WorkspaceCardsListLabel({type, value, style}: WorkspaceCardsListLabelProps) { const route = useRoute>(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`); + const policy = usePolicy(route.params.policyID); const styles = useThemeStyles(); const {windowWidth} = useWindowDimensions(); const {shouldUseNarrowLayout} = useResponsiveLayout(); diff --git a/src/pages/workspace/expensifyCard/WorkspaceEditCardLimitTypePage.tsx b/src/pages/workspace/expensifyCard/WorkspaceEditCardLimitTypePage.tsx index 8ceef5fe809e..0ea31da33d92 100644 --- a/src/pages/workspace/expensifyCard/WorkspaceEditCardLimitTypePage.tsx +++ b/src/pages/workspace/expensifyCard/WorkspaceEditCardLimitTypePage.tsx @@ -8,6 +8,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import SelectionList from '@components/SelectionList'; import RadioListItem from '@components/SelectionList/RadioListItem'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as CurrencyUtils from '@libs/CurrencyUtils'; import Navigation from '@navigation/Navigation'; @@ -41,7 +42,7 @@ function WorkspaceEditCardLimitTypePage({route}: WorkspaceEditCardLimitTypePageP const styles = useThemeStyles(); const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${policyID}_${CONST.EXPENSIFY_CARD.BANK}`); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const card = cardsList?.[cardID] ?? mockedCard; const areApprovalsConfigured = !isEmptyObject(policy?.approver) && policy?.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL; diff --git a/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx b/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx index 742a5c2a290e..6cd62a5b6189 100644 --- a/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx +++ b/src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx @@ -4,7 +4,6 @@ import React, {useCallback, useMemo} from 'react'; import type {ListRenderItemInfo} from 'react-native'; import {FlatList, View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; -import {useOnyx} from 'react-native-onyx'; import Button from '@components/Button'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import * as Expensicons from '@components/Icon/Expensicons'; @@ -13,6 +12,7 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback'; import {PressableWithFeedback} from '@components/Pressable'; import ScreenWrapper from '@components/ScreenWrapper'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useThemeStyles from '@hooks/useThemeStyles'; import localeCompare from '@libs/LocaleCompare'; @@ -20,7 +20,6 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import Navigation from '@navigation/Navigation'; import type {FullScreenNavigatorParamList} from '@navigation/types'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {Card, WorkspaceCardsList} from '@src/types/onyx'; @@ -71,7 +70,7 @@ function WorkspaceExpensifyCardListPage({route}: WorkspaceExpensifyCardListPageP const styles = useThemeStyles(); const policyID = route.params.policyID; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const policyCurrency = useMemo(() => policy?.outputCurrency ?? CONST.CURRENCY.USD, [policy]); diff --git a/src/pages/workspace/reportFields/WorkspaceReportFieldsPage.tsx b/src/pages/workspace/reportFields/WorkspaceReportFieldsPage.tsx index 9c77d123514a..5e2cfaad5315 100644 --- a/src/pages/workspace/reportFields/WorkspaceReportFieldsPage.tsx +++ b/src/pages/workspace/reportFields/WorkspaceReportFieldsPage.tsx @@ -23,6 +23,7 @@ import TextLink from '@components/TextLink'; import useEnvironment from '@hooks/useEnvironment'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -62,7 +63,7 @@ function WorkspaceReportFieldsPage({ const {translate} = useLocalize(); const isFocused = useIsFocused(); const {environmentURL} = useEnvironment(); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE); const filteredPolicyFieldList = useMemo(() => { if (!policy?.fieldList) { diff --git a/src/pages/workspace/tags/TagSettingsPage.tsx b/src/pages/workspace/tags/TagSettingsPage.tsx index 2a03c7c8a275..ce57a7269598 100644 --- a/src/pages/workspace/tags/TagSettingsPage.tsx +++ b/src/pages/workspace/tags/TagSettingsPage.tsx @@ -1,7 +1,7 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useEffect, useMemo} from 'react'; import {View} from 'react-native'; -import {useOnyx, withOnyx} from 'react-native-onyx'; +import {withOnyx} from 'react-native-onyx'; import type {OnyxEntry} from 'react-native-onyx'; import ConfirmModal from '@components/ConfirmModal'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; @@ -13,6 +13,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import Switch from '@components/Switch'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; @@ -39,7 +40,7 @@ function TagSettingsPage({route, policyTags, navigation}: TagSettingsPageProps) const styles = useThemeStyles(); const {translate} = useLocalize(); const policyTag = useMemo(() => PolicyUtils.getTagList(policyTags, route.params.orderWeight), [policyTags, route.params.orderWeight]); - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`); + const policy = usePolicy(route.params.policyID); const [isDeleteTagModalOpen, setIsDeleteTagModalOpen] = React.useState(false); diff --git a/src/pages/workspace/tags/WorkspaceTagsPage.tsx b/src/pages/workspace/tags/WorkspaceTagsPage.tsx index 845689c1e722..59a3d423e901 100644 --- a/src/pages/workspace/tags/WorkspaceTagsPage.tsx +++ b/src/pages/workspace/tags/WorkspaceTagsPage.tsx @@ -22,6 +22,7 @@ import TextLink from '@components/TextLink'; import useEnvironment from '@hooks/useEnvironment'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -52,7 +53,7 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) { const [isDeleteTagsConfirmModalVisible, setIsDeleteTagsConfirmModalVisible] = useState(false); const isFocused = useIsFocused(); const policyID = route.params.policyID ?? '-1'; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`); const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE); const {environmentURL} = useEnvironment(); diff --git a/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx b/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx index 9955d531f856..8abb5d210d68 100644 --- a/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx +++ b/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx @@ -51,7 +51,7 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) { const [isDeleteTagsConfirmModalVisible, setIsDeleteTagsConfirmModalVisible] = useState(false); const isFocused = useIsFocused(); const policyID = route.params.policyID ?? '-1'; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`); const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE); const currentTagListName = useMemo(() => PolicyUtils.getTagListName(policyTags, route.params.orderWeight), [policyTags, route.params.orderWeight]); diff --git a/src/pages/workspace/taxes/WorkspaceTaxCodePage.tsx b/src/pages/workspace/taxes/WorkspaceTaxCodePage.tsx index d32961a9d78b..e9005a8a58f2 100644 --- a/src/pages/workspace/taxes/WorkspaceTaxCodePage.tsx +++ b/src/pages/workspace/taxes/WorkspaceTaxCodePage.tsx @@ -1,7 +1,6 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useCallback} from 'react'; import {View} from 'react-native'; -import {useOnyx} from 'react-native-onyx'; import FormProvider from '@components/Form/FormProvider'; import InputWrapper from '@components/Form/InputWrapper'; import type {FormOnyxValues} from '@components/Form/types'; @@ -10,6 +9,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import TextInput from '@components/TextInput'; import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useLocalize from '@hooks/useLocalize'; +import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import {setPolicyTaxCode, validateTaxCode} from '@libs/actions/TaxRate'; import Navigation from '@libs/Navigation/Navigation'; @@ -29,7 +29,7 @@ function WorkspaceTaxCodePage({route}: WorkspaceTaxCodePageProps) { const policyID = route.params.policyID ?? '-1'; const currentTaxCode = route.params.taxID; - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); + const policy = usePolicy(policyID); const {inputCallbackRef} = useAutoFocusInput(); const setTaxCode = useCallback( From 7498f6acc429f20197abf54722adfbf5bedf0040 Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 7 Aug 2024 18:08:26 +0700 Subject: [PATCH 2/5] fix: typecheck --- .../intacct/import/SageIntacctEditUserDimensionsPage.tsx | 1 + src/pages/workspace/tags/WorkspaceViewTagsPage.tsx | 1 + 2 files changed, 2 insertions(+) diff --git a/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx b/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx index 15a22c30ce54..7bbb25f3d7cb 100644 --- a/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx +++ b/src/pages/workspace/accounting/intacct/import/SageIntacctEditUserDimensionsPage.tsx @@ -19,6 +19,7 @@ import Navigation from '@libs/Navigation/Navigation'; import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; import {settingsPendingAction} from '@libs/PolicyUtils'; import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import INPUT_IDS from '@src/types/form/SageIntacctDimensionsForm'; diff --git a/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx b/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx index 8abb5d210d68..1f3d799c6c77 100644 --- a/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx +++ b/src/pages/workspace/tags/WorkspaceViewTagsPage.tsx @@ -17,6 +17,7 @@ import SelectionListWithModal from '@components/SelectionListWithModal'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; From 5b0d5a371c15a1d59dd0a86e0ff1eca263043a95 Mon Sep 17 00:00:00 2001 From: daledah Date: Thu, 8 Aug 2024 00:22:45 +0700 Subject: [PATCH 3/5] fix: move getPolicyId to outside of usePolicy --- src/hooks/usePolicy.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/hooks/usePolicy.ts b/src/hooks/usePolicy.ts index 7c8bf1c4c6cf..f08b1b323022 100644 --- a/src/hooks/usePolicy.ts +++ b/src/hooks/usePolicy.ts @@ -2,15 +2,15 @@ import {useOnyx} from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -function usePolicy(policyID?: string) { - const getPolicyID = () => { - if (!policyID || policyID === CONST.POLICY.OWNER_EMAIL_FAKE || policyID === '-1') { - return '-1'; - } - return policyID; - }; +function getPolicyIDOrDefault(policyID?: string) { + if (!policyID || policyID === CONST.POLICY.OWNER_EMAIL_FAKE) { + return '-1'; + } + return policyID; +} - const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getPolicyID()}`); +function usePolicy(policyID?: string) { + const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getPolicyIDOrDefault(policyID)}`); return policy; } From b15eb1d47ea8dda8ce07e74c014448d3b16be9cd Mon Sep 17 00:00:00 2001 From: daledah Date: Thu, 8 Aug 2024 00:22:57 +0700 Subject: [PATCH 4/5] fix: move getPolicyId to outside of usePolicy --- src/hooks/usePolicy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/usePolicy.ts b/src/hooks/usePolicy.ts index f08b1b323022..97e98b79be79 100644 --- a/src/hooks/usePolicy.ts +++ b/src/hooks/usePolicy.ts @@ -8,7 +8,7 @@ function getPolicyIDOrDefault(policyID?: string) { } return policyID; } - +g function usePolicy(policyID?: string) { const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getPolicyIDOrDefault(policyID)}`); return policy; From 763ecd10e9899e2ad3f5a9593719d50fa1f0877c Mon Sep 17 00:00:00 2001 From: daledah Date: Thu, 8 Aug 2024 00:23:40 +0700 Subject: [PATCH 5/5] fix: typo --- src/hooks/usePolicy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/usePolicy.ts b/src/hooks/usePolicy.ts index 97e98b79be79..f08b1b323022 100644 --- a/src/hooks/usePolicy.ts +++ b/src/hooks/usePolicy.ts @@ -8,7 +8,7 @@ function getPolicyIDOrDefault(policyID?: string) { } return policyID; } -g + function usePolicy(policyID?: string) { const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getPolicyIDOrDefault(policyID)}`); return policy;