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

Refactor/23149 money request page #23979

Merged
merged 39 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c9c2851
refactor: wip - create separate new and edit pages
koko57 Jul 27, 2023
bac5cc2
refactor: wip - remove refs
koko57 Jul 27, 2023
3b25e19
refactor: move util functions to a separate file, remove old MoneyReq…
koko57 Jul 27, 2023
bb7af06
refactor: wip - remove unnecessary code in page and form components
koko57 Jul 28, 2023
432880a
Merge branch 'main' into refactor/23149-money-request-page
koko57 Jul 31, 2023
868f979
refactor: wip - proptypes for the form, cleanup
koko57 Jul 31, 2023
098e31d
fix: fix input forwardedRef proptype
koko57 Jul 31, 2023
b530026
refactor: wip - proptypes, cleanup
koko57 Jul 31, 2023
9d71022
refactor: remove old money request page
koko57 Aug 1, 2023
9a0e29b
fix: fix linting error
koko57 Aug 1, 2023
4d6228f
fix: apply requested changes
koko57 Aug 1, 2023
27c7323
fix: apply requested changes
koko57 Aug 1, 2023
829af04
fix: fix forwardedRef
koko57 Aug 1, 2023
9c1e0c9
fix: minor fix
koko57 Aug 1, 2023
dfb4aa0
fix: minor fix
koko57 Aug 1, 2023
fd54d6f
Merge branch 'main' into refactor/23149-money-request-page
koko57 Aug 1, 2023
fe12a74
fix: fix currency changing problem
koko57 Aug 1, 2023
fbf5e4c
fix: restore handling several tabs logic
koko57 Aug 1, 2023
26331e2
fix: fix linting error
koko57 Aug 1, 2023
742de9f
fix: run prettier
koko57 Aug 1, 2023
10333ff
fix: rename money utils functions param, move getNewSelection to Mone…
koko57 Aug 1, 2023
6c2393d
fix: restore modal closing logic, remove current property
koko57 Aug 1, 2023
6212539
fix: add missing dep
koko57 Aug 1, 2023
a0f0e72
refactor: move screen wrapper and header to parent component
koko57 Aug 1, 2023
8f749e4
fix: restore inner refs
koko57 Aug 1, 2023
e1b552c
fix: apply requested changes
koko57 Aug 1, 2023
ec7d54c
fix: apply requested changes
koko57 Aug 1, 2023
013193d
fix: apply requested changes
koko57 Aug 1, 2023
586105c
fix: minor fix
koko57 Aug 1, 2023
33a9812
fix: bug fix
koko57 Aug 1, 2023
4d58a9d
refactor: wrap fullpagenotfoundview into screenwrapper
koko57 Aug 1, 2023
863d37b
fix: resolve conflicts
koko57 Aug 3, 2023
45754a8
fix: change the old money amount page for the new one
koko57 Aug 3, 2023
5b3fa95
fix: remove old money amount page
koko57 Aug 3, 2023
d0fd9dc
fix: resolve conflicts
koko57 Aug 3, 2023
f1b908f
fix: render screen wrapper only when editing amount
koko57 Aug 3, 2023
53a6f6e
fix: change title for step for iou.amount
koko57 Aug 3, 2023
81d595f
fix: apply requested changes
koko57 Aug 3, 2023
014b326
fix: apply requested changes
koko57 Aug 4, 2023
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
2 changes: 1 addition & 1 deletion src/components/AmountTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const propTypes = {
formattedAmount: PropTypes.string.isRequired,

/** A ref to forward to amount text input */
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(React.Component)})]),
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(Element)})]),

/** Function to call when amount in text input is changed */
onChangeAmount: PropTypes.func.isRequired,
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextInputWithCurrencySymbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as CurrencyUtils from '../libs/CurrencyUtils';

const propTypes = {
/** A ref to forward to amount text input */
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(React.Component)})]),
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(Element)})]),

/** Formatted amount in local currency */
formattedAmount: PropTypes.string.isRequired,
Expand Down
93 changes: 93 additions & 0 deletions src/libs/MoneyRequestUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import lodashGet from 'lodash/get';
import CONST from '../CONST';
import _ from 'underscore';

/**
* Returns the new selection object based on the updated amount's length
*
* @param {Object} oldSelection
* @param {Number} prevLength
* @param {Number} newLength
* @returns {Object}
*/
const getNewSelection = (oldSelection, prevLength, newLength) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems too specific to belong to Utils.

const cursorPosition = oldSelection.end + (newLength - prevLength);
return {start: cursorPosition, end: cursorPosition};
};

/**
* Strip comma from the amount
*
* @param {String} newAmount
* @returns {String}
*/
const stripCommaFromAmount = (newAmount) => newAmount.replace(/,/g, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB, can we rename the parameter to amount? (same for other functions)


/**
* Strip spaces from the amount
*
* @param {String} newAmount
* @returns {String}
*/
const stripSpacesFromAmount = (newAmount) => newAmount.replace(/\s+/g, '');

/**
* Adds a leading zero to the amount if user entered just the decimal separator
*
* @param {String} newAmount - Changed amount from user input
* @returns {String}
*/
const addLeadingZero = (newAmount) => (newAmount === '.' ? '0.' : newAmount);

/**
* @param {String} newAmount
koko57 marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Number}
*/
const calculateAmountLength = (newAmount) => {
const leadingZeroes = newAmount.match(/^0+/);
const leadingZeroesLength = lodashGet(leadingZeroes, '[0].length', 0);
const absAmount = parseFloat((stripCommaFromAmount(newAmount) * 100).toFixed(2)).toString();

// The following logic will prevent users from pasting an amount that is excessively long in length,
// which would result in the 'absAmount' value being expressed in scientific notation or becoming infinity.
if (/\D/.test(absAmount)) {
return CONST.IOU.AMOUNT_MAX_LENGTH + 1;
}

// Return the sum of leading zeroes length and absolute amount length(including fraction digits).
// When the absolute amount is 0, add 2 to the leading zeroes length to represent fraction digits.
return leadingZeroesLength + (absAmount === '0' ? 2 : absAmount.length);
};

/**
* Check if amount is a decimal up to 3 digits
*
* @param {String} newAmount
* @returns {Boolean}
*/
const validateAmount = (newAmount) => {
const decimalNumberRegex = new RegExp(/^\d+(,\d+)*(\.\d{0,2})?$/, 'i');
return newAmount === '' || (decimalNumberRegex.test(newAmount) && calculateAmountLength(newAmount) <= CONST.IOU.AMOUNT_MAX_LENGTH);
};

/**
* Replaces each character by calling `convertFn`. If `convertFn` throws an error, then
* the original character will be preserved.
*
* @param {String} text
* @param {Function} convertFn - `fromLocaleDigit` or `toLocaleDigit`
* @returns {String}
*/
const replaceAllDigits = (text, convertFn) =>
_.chain([...text])
.map((char) => {
try {
return convertFn(char);
} catch {
return char;
}
})
.join('')
.value();

export {getNewSelection, stripCommaFromAmount, stripSpacesFromAmount, addLeadingZero, validateAmount, replaceAllDigits};
4 changes: 2 additions & 2 deletions src/libs/Navigation/AppNavigator/ModalStackNavigators.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ function createModalStackNavigator(screens) {
const MoneyRequestModalStackNavigator = createModalStackNavigator([
{
getComponent: () => {
const MoneyRequestAmountPage = require('../../../pages/iou/steps/MoneyRequestAmountPage').default;
const MoneyRequestAmountPage = require('../../../pages/iou/steps/NewRequestAmountPage').default;
return MoneyRequestAmountPage;
},
name: 'Money_Request',
},
{
getComponent: () => {
const MoneyRequestEditAmountPage = require('../../../pages/iou/steps/MoneyRequestAmountPage').default;
const MoneyRequestEditAmountPage = require('../../../pages/iou/steps/NewRequestAmountPage').default;
return MoneyRequestEditAmountPage;
},
name: 'Money_Request_Amount',
Expand Down
Loading