diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 977a5c4087d0..778b519b9180 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -1515,7 +1515,7 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) const distanceUnit = Object.values(policy?.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); const customUnitID = distanceUnit?.customUnitID; - if (!policy || !customUnitID) { + if (!policy) { return; } @@ -1524,22 +1524,24 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) const finallyRates: Record = {}; const failureRates: Record = {}; - for (const rateID of Object.keys(currentRates)) { - optimisticRates[rateID] = { - ...currentRates[rateID], - pendingFields: {currency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, - currency, - }; - finallyRates[rateID] = { - ...currentRates[rateID], - pendingFields: {currency: null}, - currency, - }; - failureRates[rateID] = { - ...currentRates[rateID], - pendingFields: {currency: null}, - errorFields: {currency: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage')}, - }; + if (customUnitID) { + for (const rateID of Object.keys(currentRates)) { + optimisticRates[rateID] = { + ...currentRates[rateID], + pendingFields: {currency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, + currency, + }; + finallyRates[rateID] = { + ...currentRates[rateID], + pendingFields: {currency: null}, + currency, + }; + failureRates[rateID] = { + ...currentRates[rateID], + pendingFields: {currency: null}, + errorFields: {currency: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage')}, + }; + } } const optimisticData: OnyxUpdate[] = [ @@ -1561,12 +1563,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) }, name, outputCurrency: currency, - customUnits: { - [customUnitID]: { - ...distanceUnit, - rates: optimisticRates, + ...(customUnitID && { + customUnits: { + [customUnitID]: { + ...distanceUnit, + rates: optimisticRates, + }, }, - }, + }), }, }, ]; @@ -1578,11 +1582,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) pendingFields: { generalSettings: null, }, - customUnits: { - [customUnitID]: { - rates: finallyRates, + ...(customUnitID && { + customUnits: { + [customUnitID]: { + ...distanceUnit, + rates: finallyRates, + }, }, - }, + }), }, }, ]; @@ -1595,11 +1602,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) errorFields: { generalSettings: ErrorUtils.getMicroSecondOnyxError('workspace.editor.genericFailureMessage'), }, - customUnits: { - [customUnitID]: { - rates: failureRates, + ...(customUnitID && { + customUnits: { + [customUnitID]: { + ...distanceUnit, + rates: failureRates, + }, }, - }, + }), }, }, ];