Skip to content

Commit

Permalink
remove IOU with invalid data from initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg committed Aug 2, 2023
1 parent 8558d74 commit cd62597
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ const defaultProps = {
isSubmitButtonVisible: true,
formState: {
isLoading: false,
errors: null,
},
draftValues: {},
enabledWhenOffline: false,
Expand Down
18 changes: 12 additions & 6 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 27 additions & 24 deletions src/pages/iou/steps/MoneyRequestAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,43 +379,46 @@ 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}`;
const shouldReset = props.iou.id !== moneyRequestID;
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -98,13 +105,13 @@ function MoneyRequestParticipantsPage(props) {
<MoneyRequestParticipantsSplitSelector
onStepComplete={navigateToNextStep}
participants={props.iou.participants}
onAddParticipants={IOU.setMoneyRequestParticipants}
onAddParticipants={onAddParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
/>
) : (
<MoneyRequestParticipantsSelector
onStepComplete={navigateToNextStep}
onAddParticipants={IOU.setMoneyRequestParticipants}
onAddParticipants={onAddParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
iouType={iouType.current}
/>
Expand Down
4 changes: 0 additions & 4 deletions src/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit cd62597

Please sign in to comment.