Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: workspace currency can be changed without existing distance rates #39484

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading