Skip to content

Commit

Permalink
fix: workspace currency can be changed without existing distance rates
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMuzyk committed Apr 3, 2024
1 parent 488664d commit b57de07
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,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;
}

Expand All @@ -1516,22 +1516,24 @@ function updateGeneralSettings(policyID: string, name: string, currency: string)
const finallyRates: Record<string, Rate> = {};
const failureRates: Record<string, Rate> = {};

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[] = [
Expand All @@ -1553,12 +1555,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string)
},
name,
outputCurrency: currency,
customUnits: {
[customUnitID]: {
...distanceUnit,
rates: optimisticRates,
...(customUnitID && {
customUnits: {
[customUnitID]: {
...distanceUnit,
rates: optimisticRates,
},
},
},
}),
},
},
];
Expand All @@ -1570,11 +1574,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string)
pendingFields: {
generalSettings: null,
},
customUnits: {
[customUnitID]: {
rates: finallyRates,
...(customUnitID && {
customUnits: {
[customUnitID]: {
...distanceUnit,
rates: finallyRates,
},
},
},
}),
},
},
];
Expand All @@ -1587,11 +1594,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,
},
},
},
}),
},
},
];
Expand Down

0 comments on commit b57de07

Please sign in to comment.