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

Build optimistic custom unit when creating workspace #31170

Merged
merged 11 commits into from
Dec 3, 2023
14 changes: 9 additions & 5 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,9 +1045,10 @@ function generateCustomUnitID() {
}

/**
* @param {String} currency
* @returns {Object}
*/
function buildOptimisticCustomUnits() {
function buildOptimisticCustomUnits(currency) {
const customUnitID = generateCustomUnitID();
const customUnitRateID = generateCustomUnitID();
const customUnits = {
Expand All @@ -1062,6 +1063,7 @@ function buildOptimisticCustomUnits() {
customUnitRateID,
name: CONST.CUSTOM_UNITS.DEFAULT_RATE,
rate: CONST.CUSTOM_UNITS.MILEAGE_IRS_RATE * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET,
currency,
},
},
},
Expand All @@ -1084,7 +1086,8 @@ function buildOptimisticCustomUnits() {
*/
function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', policyID = generatePolicyID(), makeMeAdmin = false) {
const workspaceName = policyName || generateDefaultWorkspaceName(policyOwnerEmail);
const {customUnits} = buildOptimisticCustomUnits();
const outputCurrency = lodashGet(allPersonalDetails, [sessionAccountID, 'localCurrencyCode'], CONST.CURRENCY.USD);
const {customUnits} = buildOptimisticCustomUnits(outputCurrency);

const optimisticData = [
{
Expand All @@ -1097,7 +1100,7 @@ function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', pol
role: CONST.POLICY.ROLE.ADMIN,
owner: sessionEmail,
isPolicyExpenseChatEnabled: true,
outputCurrency: lodashGet(allPersonalDetails, [sessionAccountID, 'localCurrencyCode'], CONST.CURRENCY.USD),
outputCurrency,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
customUnits,
makeMeAdmin,
Expand Down Expand Up @@ -1130,7 +1133,8 @@ function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', pol
function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName = '', policyID = generatePolicyID()) {
const workspaceName = policyName || generateDefaultWorkspaceName(policyOwnerEmail);

const {customUnits, customUnitID, customUnitRateID} = buildOptimisticCustomUnits();
const outputCurrency = lodashGet(allPersonalDetails, [sessionAccountID, 'localCurrencyCode'], CONST.CURRENCY.USD);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the same line twice

const outputCurrency = lodashGet(allPersonalDetails, [sessionAccountID, 'localCurrencyCode'], CONST.CURRENCY.USD);

Can we move it to buildOptimisticCustomUnits? And for both cases (createWorkspace and createDraftInitialWorkspace) access it from customUnits[customUnitID].rates[customUnitRateID].currency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eVoloshchak I updated.

const {customUnits, customUnitID, customUnitRateID} = buildOptimisticCustomUnits(outputCurrency);

const {
announceChatReportID,
Expand Down Expand Up @@ -1176,7 +1180,7 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName
role: CONST.POLICY.ROLE.ADMIN,
owner: sessionEmail,
isPolicyExpenseChatEnabled: true,
outputCurrency: lodashGet(allPersonalDetails, [sessionAccountID, 'localCurrencyCode'], CONST.CURRENCY.USD),
outputCurrency,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
customUnits,
},
Expand Down
Loading