@@ -8,11 +8,11 @@ import HeaderWithBackButton from '@components/HeaderWithBackButton';
8
8
import * as Expensicons from '@components/Icon/Expensicons' ;
9
9
import * as Illustrations from '@components/Icon/Illustrations' ;
10
10
import ScreenWrapper from '@components/ScreenWrapper' ;
11
- import ListItemRightCaretWithLabel from '@components/SelectionList/ListItemRightCaretWithLabel' ;
12
11
import TableListItem from '@components/SelectionList/TableListItem' ;
13
12
import type { ListItem } from '@components/SelectionList/types' ;
14
13
import SelectionListWithModal from '@components/SelectionListWithModal' ;
15
14
import CustomListHeader from '@components/SelectionListWithModal/CustomListHeader' ;
15
+ import Switch from '@components/Switch' ;
16
16
import Text from '@components/Text' ;
17
17
import useLocalize from '@hooks/useLocalize' ;
18
18
import useMobileSelectionMode from '@hooks/useMobileSelectionMode' ;
@@ -74,11 +74,11 @@ function PolicyDistanceRatesPage({
74
74
const dismissError = useCallback (
75
75
( item : RateForList ) => {
76
76
if ( customUnitRates [ item . value ] . errors ) {
77
- DistanceRate . clearDeleteDistanceRateError ( policyID , customUnit ?. customUnitID ?? '' , item . value ) ;
77
+ DistanceRate . clearDeleteDistanceRateError ( policyID , customUnit ?. customUnitID ?? CONST . EMPTY_STRING , item . value ) ;
78
78
return ;
79
79
}
80
80
81
- DistanceRate . clearCreateDistanceRateItemAndError ( policyID , customUnit ?. customUnitID ?? '' , item . value ) ;
81
+ DistanceRate . clearCreateDistanceRateItemAndError ( policyID , customUnit ?. customUnitID ?? CONST . EMPTY_STRING , item . value ) ;
82
82
} ,
83
83
[ customUnit ?. customUnitID , customUnitRates , policyID ] ,
84
84
) ;
@@ -98,16 +98,36 @@ function PolicyDistanceRatesPage({
98
98
setSelectedDistanceRates ( [ ] ) ;
99
99
} , [ isFocused ] ) ;
100
100
101
+ const updateDistanceRateEnabled = useCallback (
102
+ ( value : boolean , rateID : string ) => {
103
+ if ( ! customUnit ) {
104
+ return ;
105
+ }
106
+ const rate = customUnit ?. rates ?. [ rateID ] ;
107
+ // Rates can be disabled or deleted as long as in the remaining rates there is always at least one enabled rate and there are no pending delete action
108
+ const canDisableOrDeleteRate = Object . values ( customUnit ?. rates ?? { } ) . some (
109
+ ( distanceRate : Rate ) => distanceRate ?. enabled && rateID !== distanceRate ?. customUnitRateID && distanceRate ?. pendingAction !== CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE ,
110
+ ) ;
111
+
112
+ if ( ! rate ?. enabled || canDisableOrDeleteRate ) {
113
+ DistanceRate . setPolicyDistanceRatesEnabled ( policyID , customUnit , [ { ...rate , enabled : value } ] ) ;
114
+ } else {
115
+ setIsWarningModalVisible ( true ) ;
116
+ }
117
+ } ,
118
+ [ customUnit , policyID ] ,
119
+ ) ;
120
+
101
121
const distanceRatesList = useMemo < RateForList [ ] > (
102
122
( ) =>
103
123
Object . values ( customUnitRates )
104
124
. sort ( ( rateA , rateB ) => ( rateA ?. rate ?? 0 ) - ( rateB ?. rate ?? 0 ) )
105
125
. map ( ( value ) => ( {
106
- value : value . customUnitRateID ?? '' ,
126
+ value : value . customUnitRateID ?? CONST . EMPTY_STRING ,
107
127
text : `${ CurrencyUtils . convertAmountToDisplayString ( value . rate , value . currency ?? CONST . CURRENCY . USD ) } / ${ translate (
108
128
`common.${ customUnit ?. attributes ?. unit ?? CONST . CUSTOM_UNITS . DISTANCE_UNIT_MILES } ` ,
109
129
) } `,
110
- keyForList : value . customUnitRateID ?? '' ,
130
+ keyForList : value . customUnitRateID ?? CONST . EMPTY_STRING ,
111
131
isSelected : selectedDistanceRates . find ( ( rate ) => rate . customUnitRateID === value . customUnitRateID ) !== undefined && canSelectMultiple ,
112
132
isDisabled : value . pendingAction === CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE ,
113
133
pendingAction :
@@ -119,9 +139,16 @@ function PolicyDistanceRatesPage({
119
139
value . pendingFields ?. taxClaimablePercentage ??
120
140
( policy ?. pendingAction === CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ? policy ?. pendingAction : undefined ) ,
121
141
errors : value . errors ?? undefined ,
122
- rightElement : < ListItemRightCaretWithLabel labelText = { value . enabled ? translate ( 'workspace.common.enabled' ) : translate ( 'workspace.common.disabled' ) } /> ,
142
+ rightElement : (
143
+ < Switch
144
+ isOn = { ! ! value ?. enabled }
145
+ accessibilityLabel = { translate ( 'workspace.distanceRates.trackTax' ) }
146
+ onToggle = { ( newValue : boolean ) => updateDistanceRateEnabled ( newValue , value . customUnitRateID ) }
147
+ disabled = { value . pendingAction === CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE }
148
+ />
149
+ ) ,
123
150
} ) ) ,
124
- [ customUnit ?. attributes ?. unit , customUnitRates , selectedDistanceRates , translate , policy ?. pendingAction , canSelectMultiple ] ,
151
+ [ customUnitRates , translate , customUnit , selectedDistanceRates , canSelectMultiple , policy ?. pendingAction , updateDistanceRateEnabled ] ,
125
152
) ;
126
153
127
154
const addRate = ( ) => {
@@ -170,7 +197,7 @@ function PolicyDistanceRatesPage({
170
197
DistanceRate . deletePolicyDistanceRates (
171
198
policyID ,
172
199
customUnit ,
173
- selectedDistanceRates . map ( ( rate ) => rate . customUnitRateID ?? '' ) ,
200
+ selectedDistanceRates . map ( ( rate ) => rate . customUnitRateID ?? CONST . EMPTY_STRING ) ,
174
201
) ;
175
202
setSelectedDistanceRates ( [ ] ) ;
176
203
setIsDeleteModalVisible ( false ) ;
0 commit comments