Skip to content

Commit

Permalink
Merge pull request Expensify#30873 from erquhart/fix/30505
Browse files Browse the repository at this point in the history
format request display amount on submit
  • Loading branch information
jasperhuangg authored Nov 16, 2023
2 parents 235ede8 + 2c19580 commit 2889742
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/pages/iou/steps/MoneyRequestAmountForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,20 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
}
};

const initializeAmount = useCallback((newAmount) => {
const frontendAmount = newAmount ? CurrencyUtils.convertToFrontendAmount(newAmount).toString() : '';
setCurrentAmount(frontendAmount);
setSelection({
start: frontendAmount.length,
end: frontendAmount.length,
});
}, []);

useEffect(() => {
if (!currency || !_.isNumber(amount)) {
return;
}
const amountAsStringForState = amount ? CurrencyUtils.convertToFrontendAmount(amount).toString() : '';
setCurrentAmount(amountAsStringForState);
setSelection({
start: amountAsStringForState.length,
end: amountAsStringForState.length,
});
initializeAmount(amount);
// we want to update the state only when the amount is changed
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [amount]);
Expand Down Expand Up @@ -219,8 +223,13 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
return;
}

// Update display amount string post-edit to ensure consistency with backend amount
// Reference: https://github.com/Expensify/App/issues/30505
const backendAmount = CurrencyUtils.convertToBackendAmount(Number.parseFloat(currentAmount));
initializeAmount(backendAmount);

onSubmitButtonPress(currentAmount);
}, [onSubmitButtonPress, currentAmount]);
}, [onSubmitButtonPress, currentAmount, initializeAmount]);

/**
* Input handler to check for a forward-delete key (or keyboard shortcut) press.
Expand Down

0 comments on commit 2889742

Please sign in to comment.