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 all 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
85 changes: 85 additions & 0 deletions src/libs/MoneyRequestUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import lodashGet from 'lodash/get';
import _ from 'underscore';
import CONST from '../CONST';

/**
* Strip comma from the amount
*
* @param {String} amount
* @returns {String}
*/
function stripCommaFromAmount(amount) {
return amount.replace(/,/g, '');
}

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

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

/**
* Calculate the length of the amount with leading zeroes
*
* @param {String} amount
* @returns {Number}
*/
function calculateAmountLength(amount) {
const leadingZeroes = amount.match(/^0+/);
const leadingZeroesLength = lodashGet(leadingZeroes, '[0].length', 0);
const absAmount = parseFloat((stripCommaFromAmount(amount) * 100).toFixed(2)).toString();

if (/\D/.test(absAmount)) {
return CONST.IOU.AMOUNT_MAX_LENGTH + 1;
}

return leadingZeroesLength + (absAmount === '0' ? 2 : absAmount.length);
}

/**
* Check if amount is a decimal up to 3 digits
*
* @param {String} amount
* @returns {Boolean}
*/
function validateAmount(amount) {
const decimalNumberRegex = new RegExp(/^\d+(,\d+)*(\.\d{0,2})?$/, 'i');
return amount === '' || (decimalNumberRegex.test(amount) && calculateAmountLength(amount) <= 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}
*/
function replaceAllDigits(text, convertFn) {
return _.chain([...text])
.map((char) => {
try {
return convertFn(char);
} catch {
return char;
}
})
.join('')
.value();
}

export {stripCommaFromAmount, stripSpacesFromAmount, addLeadingZero, validateAmount, replaceAllDigits};
2 changes: 1 addition & 1 deletion src/libs/Navigation/AppNavigator/ModalStackNavigators.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const MoneyRequestModalStackNavigator = createModalStackNavigator([
},
{
getComponent: () => {
const MoneyRequestEditAmountPage = require('../../../pages/iou/steps/MoneyRequestAmount').default;
const MoneyRequestEditAmountPage = require('../../../pages/iou/steps/NewRequestAmountPage').default;
return MoneyRequestEditAmountPage;
},
name: 'Money_Request_Amount',
Expand Down
6 changes: 3 additions & 3 deletions src/pages/iou/MoneyRequestSelectorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import useLocalize from '../../hooks/useLocalize';
import * as IOUUtils from '../../libs/IOUUtils';
import Navigation from '../../libs/Navigation/Navigation';
import styles from '../../styles/styles';
import MoneyRequestAmount from './steps/MoneyRequestAmount';
import ReceiptSelector from './ReceiptSelector';
import * as IOU from '../../libs/actions/IOU';
import DragAndDropProvider from '../../components/DragAndDrop/Provider';
import usePermissions from '../../hooks/usePermissions';
import OnyxTabNavigator, {TopTab} from '../../libs/Navigation/OnyxTabNavigator';
import participantPropTypes from '../../components/participantPropTypes';
import NewRequestAmountPage from './steps/NewRequestAmountPage';

const propTypes = {
/** React Navigation route */
Expand Down Expand Up @@ -98,7 +98,7 @@ function MoneyRequestSelectorPage(props) {
>
<TopTab.Screen
name={CONST.TAB.MANUAL}
component={MoneyRequestAmount}
component={NewRequestAmountPage}
initialParams={{reportID, iouType}}
/>
<TopTab.Screen
Expand All @@ -108,7 +108,7 @@ function MoneyRequestSelectorPage(props) {
/>
</OnyxTabNavigator>
) : (
<MoneyRequestAmount route={props.route} />
<NewRequestAmountPage route={props.route} />
)}
</View>
</DragAndDropProvider>
Expand Down
Loading
Loading