diff --git a/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx b/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx index 258a4e842756..b8af141f6933 100644 --- a/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx +++ b/src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx @@ -1,20 +1,21 @@ import type {StackScreenProps} from '@react-navigation/stack'; import React, {useEffect, useMemo, useRef, useState} from 'react'; -import {View} from 'react-native'; +import {ActivityIndicator, View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import {withOnyx} from 'react-native-onyx'; import Button from '@components/Button'; import type {DropdownOption, WorkspaceDistanceRatesBulkActionType} from '@components/ButtonWithDropdownMenu/types'; +import ConfirmModal from '@components/ConfirmModal'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import Icon from '@components/Icon'; import * as Expensicons from '@components/Icon/Expensicons'; import * as Illustrations from '@components/Icon/Illustrations'; -import Modal from '@components/Modal'; import ScreenWrapper from '@components/ScreenWrapper'; import SelectionList from '@components/SelectionList'; import TableListItem from '@components/SelectionList/TableListItem'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; +import useNetwork from '@hooks/useNetwork'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; @@ -22,12 +23,12 @@ import * as CurrencyUtils from '@libs/CurrencyUtils'; import type {CentralPaneNavigatorParamList} from '@navigation/types'; import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper'; import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper'; -import {openPolicyDistanceRatesPage} from '@userActions/Policy'; +import * as Policy from '@userActions/Policy'; import ButtonWithDropdownMenu from '@src/components/ButtonWithDropdownMenu'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; -import type Policy from '@src/types/onyx/Policy'; +import type * as OnyxTypes from '@src/types/onyx'; import type {CustomUnit, Rate} from '@src/types/onyx/Policy'; type RateForList = { @@ -40,7 +41,7 @@ type RateForList = { type PolicyDistanceRatesPageOnyxProps = { /** Policy details */ - policy: OnyxEntry; + policy: OnyxEntry; }; type PolicyDistanceRatesPageProps = PolicyDistanceRatesPageOnyxProps & StackScreenProps; @@ -60,9 +61,16 @@ function PolicyDistanceRatesPage({policy, route}: PolicyDistanceRatesPageProps) ); const customUnitRates: Record = useMemo(() => customUnit?.rates ?? {}, [customUnit]); + function fetchDistanceRates() { + Policy.openPolicyDistanceRatesPage(route.params.policyID); + } + + const {isOffline} = useNetwork({onReconnect: fetchDistanceRates}); + useEffect(() => { - openPolicyDistanceRatesPage(route.params.policyID); - }, [route.params.policyID]); + fetchDistanceRates(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); const distanceRatesList = useMemo( () => @@ -103,7 +111,7 @@ function PolicyDistanceRatesPage({policy, route}: PolicyDistanceRatesPageProps) }; const disableRates = () => { - if (selectedDistanceRates.length !== customUnitRates.length) { + if (selectedDistanceRates.length !== Object.values(customUnitRates).length) { // run enableWorkspaceDistanceRates for all selected rows } @@ -111,15 +119,11 @@ function PolicyDistanceRatesPage({policy, route}: PolicyDistanceRatesPageProps) }; const enableRates = () => { - if (selectedDistanceRates.length !== customUnitRates.length) { - // run enableWorkspaceDistanceRates for all selected rows - } - - setIsWarningModalVisible(true); + // run enableWorkspaceDistanceRates for all selected rows }; const deleteRates = () => { - if (selectedDistanceRates.length !== customUnitRates.length) { + if (selectedDistanceRates.length !== Object.values(customUnitRates).length) { // run deleteWorkspaceDistanceRates for all selected rows } @@ -182,6 +186,8 @@ function PolicyDistanceRatesPage({policy, route}: PolicyDistanceRatesPageProps) return options; }; + const isLoading = !isOffline && customUnit === undefined; + const headerButtons = ( {selectedDistanceRates.length === 0 ? ( @@ -241,32 +247,34 @@ function PolicyDistanceRatesPage({policy, route}: PolicyDistanceRatesPageProps) {translate('workspace.distanceRates.centrallyManage')} - - + )} + {Object.values(customUnitRates).length > 0 && ( + + )} + setIsWarningModalVisible(false)} isVisible={isWarningModalVisible} - onClose={() => setIsWarningModalVisible(false)} - > - - {translate('workspace.distanceRates.oopsNotSoFast')} - {translate('workspace.distanceRates.workspaceNeeds')} -