From cd62597f9e336eb9cbb3965750c373818b5ceb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Wed, 2 Aug 2023 10:22:46 +0200 Subject: [PATCH] remove IOU with invalid data from initial values https://github.com/Expensify/App/issues/23951 --- src/components/Form.js | 1 - src/libs/actions/IOU.js | 18 ++++--- src/pages/iou/steps/MoneyRequestAmountPage.js | 51 ++++++++++--------- .../MoneyRequestParticipantsPage.js | 13 +++-- src/setup/index.js | 4 -- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/components/Form.js b/src/components/Form.js index 4f4c7527cf1d..ed765ac435c1 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -82,7 +82,6 @@ const defaultProps = { isSubmitButtonVisible: true, formState: { isLoading: false, - errors: null, }, draftValues: {}, enabledWhenOffline: false, diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index f0905eca0a89..868ef1bdba64 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -1438,46 +1438,52 @@ function payMoneyRequest(paymentType, chatReport, iouReport) { * Initialize money request info and navigate to the MoneyRequest page * @param {String} iouType * @param {String} reportID + * @returns {Promise} */ function startMoneyRequest(iouType, reportID = '') { - resetMoneyRequestInfo(`${iouType}${reportID}`).then(() => { + return resetMoneyRequestInfo(`${iouType}${reportID}`).then(() => { Navigation.navigate(ROUTES.getMoneyRequestRoute(iouType, reportID)); }); } /** * @param {String} id + * @returns {Promise} */ function setMoneyRequestId(id) { - Onyx.merge(ONYXKEYS.IOU, {id}); + return Onyx.merge(ONYXKEYS.IOU, {id}); } /** * @param {Number} amount + * @returns {Promise} */ function setMoneyRequestAmount(amount) { - Onyx.merge(ONYXKEYS.IOU, {amount}); + return Onyx.merge(ONYXKEYS.IOU, {amount}); } /** * @param {String} currency + * @returns {Promise} */ function setMoneyRequestCurrency(currency) { - Onyx.merge(ONYXKEYS.IOU, {currency}); + return Onyx.merge(ONYXKEYS.IOU, {currency}); } /** * @param {String} comment + * @returns {Promise} */ function setMoneyRequestDescription(comment) { - Onyx.merge(ONYXKEYS.IOU, {comment: comment.trim()}); + return Onyx.merge(ONYXKEYS.IOU, {comment: comment.trim()}); } /** * @param {Object[]} participants + * @returns {Promise} */ function setMoneyRequestParticipants(participants) { - Onyx.merge(ONYXKEYS.IOU, {participants}); + return Onyx.merge(ONYXKEYS.IOU, {participants}); } export { diff --git a/src/pages/iou/steps/MoneyRequestAmountPage.js b/src/pages/iou/steps/MoneyRequestAmountPage.js index e25f7acb0553..337610f48217 100755 --- a/src/pages/iou/steps/MoneyRequestAmountPage.js +++ b/src/pages/iou/steps/MoneyRequestAmountPage.js @@ -379,14 +379,13 @@ function MoneyRequestAmountPage(props) { const navigateToNextPage = () => { const amountInSmallestCurrencyUnits = CurrencyUtils.convertToSmallestUnit(selectedCurrencyCode, Number.parseFloat(amount)); - IOU.setMoneyRequestAmount(amountInSmallestCurrencyUnits); - IOU.setMoneyRequestCurrency(selectedCurrencyCode); - saveAmountToState(selectedCurrencyCode, amountInSmallestCurrencyUnits); + const iouUpdates = [IOU.setMoneyRequestAmount(amountInSmallestCurrencyUnits), IOU.setMoneyRequestCurrency(selectedCurrencyCode)]; if (isEditing.current) { - Navigation.goBack(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current)); - return; + return Promise.all(iouUpdates).then(() => { + Navigation.goBack(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current)); + }); } const moneyRequestID = `${iouType.current}${reportID.current}`; @@ -394,28 +393,32 @@ function MoneyRequestAmountPage(props) { // If the money request ID in Onyx does not match the ID from params, we want to start a new request // with the ID from params. We need to clear the participants in case the new request is initiated from FAB. if (shouldReset) { - IOU.setMoneyRequestId(moneyRequestID); - IOU.setMoneyRequestDescription(''); - IOU.setMoneyRequestParticipants([]); + iouUpdates.push(IOU.setMoneyRequestId(moneyRequestID), IOU.setMoneyRequestDescription('')); + if (!props.report.reportID) { + iouUpdates.push(IOU.setMoneyRequestParticipants([])); + } } - // If a request is initiated on a report, skip the participants selection step and navigate to the confirmation page. - if (props.report.reportID) { - // Reinitialize the participants when the money request ID in Onyx does not match the ID from params - if (_.isEmpty(props.iou.participants) || shouldReset) { - const currentUserAccountID = props.currentUserPersonalDetails.accountID; - const participants = ReportUtils.isPolicyExpenseChat(props.report) - ? [{reportID: props.report.reportID, isPolicyExpenseChat: true, selected: true}] - : _.chain(props.report.participantAccountIDs) - .filter((accountID) => currentUserAccountID !== accountID) - .map((accountID) => ({accountID, selected: true})) - .value(); - IOU.setMoneyRequestParticipants(participants); - } - Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current)); - return; + // Reinitialize the participants when the money request ID in Onyx does not match the ID from params + if (props.report.reportID && (_.isEmpty(props.iou.participants) || shouldReset)) { + const currentUserAccountID = props.currentUserPersonalDetails.accountID; + const participants = ReportUtils.isPolicyExpenseChat(props.report) + ? [{reportID: props.report.reportID, isPolicyExpenseChat: true, selected: true}] + : _.chain(props.report.participantAccountIDs) + .filter((accountID) => currentUserAccountID !== accountID) + .map((accountID) => ({accountID, selected: true})) + .value(); + iouUpdates.push(IOU.setMoneyRequestParticipants(participants)); } - Navigation.navigate(ROUTES.getMoneyRequestParticipantsRoute(iouType.current)); + + Promise.all(iouUpdates).then(() => { + // If a request is initiated on a report, skip the participants selection step and navigate to the confirmation page. + if (props.report.reportID) { + Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current)); + return; + } + Navigation.navigate(ROUTES.getMoneyRequestParticipantsRoute(iouType.current)); + }); }; const formattedAmount = replaceAllDigits(amount, toLocaleDigit); diff --git a/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js b/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js index ce26079761eb..a8deb3646413 100644 --- a/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js +++ b/src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js @@ -49,8 +49,15 @@ function MoneyRequestParticipantsPage(props) { const iouType = useRef(lodashGet(props.route, 'params.iouType', '')); const reportID = useRef(lodashGet(props.route, 'params.reportID', '')); + const addingParticipantsPromiseRef = useRef(Promise.resolve()); + const onAddParticipants = (participants) => { + addingParticipantsPromiseRef.current = IOU.setMoneyRequestParticipants(participants); + }; + const navigateToNextStep = () => { - Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current)); + addingParticipantsPromiseRef.current.then(() => { + Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current)); + }); }; const navigateBack = (forceFallback = false) => { @@ -98,13 +105,13 @@ function MoneyRequestParticipantsPage(props) { ) : ( diff --git a/src/setup/index.js b/src/setup/index.js index dee506302e81..94aa27707012 100644 --- a/src/setup/index.js +++ b/src/setup/index.js @@ -33,10 +33,6 @@ export default function () { [ONYXKEYS.SESSION]: {loading: false}, [ONYXKEYS.ACCOUNT]: CONST.DEFAULT_ACCOUNT_DATA, [ONYXKEYS.NETWORK]: {isOffline: false}, - [ONYXKEYS.IOU]: { - loading: false, - error: false, - }, [ONYXKEYS.IS_SIDEBAR_LOADED]: false, [ONYXKEYS.SHOULD_SHOW_COMPOSE_INPUT]: true, },