@@ -14,48 +14,14 @@ import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
1414import * as ErrorUtils from '@libs/ErrorUtils' ;
1515import getIsNarrowLayout from '@libs/getIsNarrowLayout' ;
1616import { buildOnyxDataForPolicyDistanceRateUpdates } from '@libs/PolicyDistanceRatesUtils' ;
17- import { getDistanceRateCustomUnit , goBackWhenEnableFeature , removePendingFieldsFromCustomUnit } from '@libs/PolicyUtils' ;
18- import * as ReportUtils from '@libs/ReportUtils' ;
17+ import { goBackWhenEnableFeature , removePendingFieldsFromCustomUnit } from '@libs/PolicyUtils' ;
1918import CONST from '@src/CONST' ;
2019import ONYXKEYS from '@src/ONYXKEYS' ;
21- import type { Policy , Report , TransactionViolation } from '@src/types/onyx' ;
20+ import type { TransactionViolation } from '@src/types/onyx' ;
2221import type { ErrorFields } from '@src/types/onyx/OnyxCommon' ;
2322import type { CustomUnit , Rate } from '@src/types/onyx/Policy' ;
2423import type { OnyxData } from '@src/types/onyx/Request' ;
2524
26- const allPolicies : OnyxCollection < Policy > = { } ;
27- Onyx . connect ( {
28- key : ONYXKEYS . COLLECTION . POLICY ,
29- callback : ( val , key ) => {
30- if ( ! key ) {
31- return ;
32- }
33- if ( val === null || val === undefined ) {
34- // If we are deleting a policy, we have to check every report linked to that policy
35- // and unset the draft indicator (pencil icon) alongside removing any draft comments. Clearing these values will keep the newly archived chats from being displayed in the LHN.
36- // More info: https://github.com/Expensify/App/issues/14260
37- const policyID = key . replace ( ONYXKEYS . COLLECTION . POLICY , '' ) ;
38- const policyReports = ReportUtils . getAllPolicyReports ( policyID ) ;
39- const cleanUpMergeQueries : Record < `${typeof ONYXKEYS . COLLECTION . REPORT } ${string } `, NullishDeep < Report > > = { } ;
40- const cleanUpSetQueries : Record < `${typeof ONYXKEYS . COLLECTION . REPORT_DRAFT_COMMENT } ${string } ` | `${typeof ONYXKEYS . COLLECTION . REPORT_ACTIONS_DRAFTS } ${string } `, null > = { } ;
41- policyReports . forEach ( ( policyReport ) => {
42- if ( ! policyReport ) {
43- return ;
44- }
45- const { reportID} = policyReport ;
46- cleanUpSetQueries [ `${ ONYXKEYS . COLLECTION . REPORT_DRAFT_COMMENT } ${ reportID } ` ] = null ;
47- cleanUpSetQueries [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS_DRAFTS } ${ reportID } ` ] = null ;
48- } ) ;
49- Onyx . mergeCollection ( ONYXKEYS . COLLECTION . REPORT , cleanUpMergeQueries ) ;
50- Onyx . multiSet ( cleanUpSetQueries ) ;
51- delete allPolicies [ key ] ;
52- return ;
53- }
54-
55- allPolicies [ key ] = val ;
56- } ,
57- } ) ;
58-
5925/**
6026 * Takes array of customUnitRates and removes pendingFields and errorFields from each rate - we don't want to send those via API
6127 */
@@ -81,7 +47,7 @@ function openPolicyDistanceRatesPage(policyID?: string) {
8147 API . read ( READ_COMMANDS . OPEN_POLICY_DISTANCE_RATES_PAGE , params ) ;
8248}
8349
84- function enablePolicyDistanceRates ( policyID : string , enabled : boolean ) {
50+ function enablePolicyDistanceRates ( policyID : string , enabled : boolean , customUnit : CustomUnit | undefined ) {
8551 const onyxData : OnyxData = {
8652 optimisticData : [
8753 {
@@ -120,39 +86,34 @@ function enablePolicyDistanceRates(policyID: string, enabled: boolean) {
12086 ] ,
12187 } ;
12288
123- if ( ! enabled ) {
124- const policy = allPolicies ?. [ `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ] ;
125- const customUnit = getDistanceRateCustomUnit ( policy ) ;
126- if ( customUnit ) {
127- const customUnitID = customUnit . customUnitID ;
128-
129- const rateEntries = Object . entries ( customUnit . rates ?? { } ) ;
130- // find the rate to be enabled after disabling the distance rate feature
131- const rateEntryToBeEnabled = rateEntries . at ( 0 ) ;
89+ if ( ! enabled && customUnit ) {
90+ const customUnitID = customUnit . customUnitID ;
91+ const rateEntries = Object . entries ( customUnit . rates ?? { } ) ;
92+ // find the rate to be enabled after disabling the distance rate feature
93+ const rateEntryToBeEnabled = rateEntries . at ( 0 ) ;
13294
133- onyxData . optimisticData ?. push ( {
134- onyxMethod : Onyx . METHOD . MERGE ,
135- key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ,
136- value : {
137- customUnits : {
138- [ customUnitID ] : {
139- rates : Object . fromEntries (
140- rateEntries . map ( ( rateEntry ) => {
141- const [ rateID , rate ] = rateEntry ;
142- return [
143- rateID ,
144- {
145- ...rate ,
146- enabled : rateID === rateEntryToBeEnabled ?. at ( 0 ) ,
147- } ,
148- ] ;
149- } ) ,
150- ) ,
151- } ,
95+ onyxData . optimisticData ?. push ( {
96+ onyxMethod : Onyx . METHOD . MERGE ,
97+ key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ,
98+ value : {
99+ customUnits : {
100+ [ customUnitID ] : {
101+ rates : Object . fromEntries (
102+ rateEntries . map ( ( rateEntry ) => {
103+ const [ rateID , rate ] = rateEntry ;
104+ return [
105+ rateID ,
106+ {
107+ ...rate ,
108+ enabled : rateID === rateEntryToBeEnabled ?. at ( 0 ) ,
109+ } ,
110+ ] ;
111+ } ) ,
112+ ) ,
152113 } ,
153114 } ,
154- } ) ;
155- }
115+ } ,
116+ } ) ;
156117 }
157118
158119 const parameters : EnablePolicyDistanceRatesParams = { policyID, enabled} ;
0 commit comments