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

Attach reportID to IOU when requesting from the global flow #27139

Merged
merged 12 commits into from
Sep 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import MoneyRequestParticipantsSplitSelector from './MoneyRequestParticipantsSpl
import MoneyRequestParticipantsSelector from './MoneyRequestParticipantsSelector';
import styles from '../../../../styles/styles';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import Navigation from '../../../../libs/Navigation/Navigation';
import compose from '../../../../libs/compose';
import * as DeviceCapabilities from '../../../../libs/DeviceCapabilities';
import HeaderWithBackButton from '../../../../components/HeaderWithBackButton';
import * as IOU from '../../../../libs/actions/IOU';
import * as MoneyRequestUtils from '../../../../libs/MoneyRequestUtils';
import {iouPropTypes, iouDefaultProps} from '../../propTypes';
import useLocalize from '../../../../hooks/useLocalize';

const propTypes = {
/** React Navigation route */
Expand All @@ -37,25 +36,39 @@ const propTypes = {

/** The current tab we have navigated to in the request modal. String that corresponds to the request type. */
selectedTab: PropTypes.oneOf([CONST.TAB.DISTANCE, CONST.TAB.MANUAL, CONST.TAB.SCAN]).isRequired,

...withLocalizePropTypes,
};

const defaultProps = {
iou: iouDefaultProps,
};

function MoneyRequestParticipantsPage(props) {
const {translate} = useLocalize();
const prevMoneyRequestId = useRef(props.iou.id);
const iouType = useRef(lodashGet(props.route, 'params.iouType', ''));
const reportID = useRef(lodashGet(props.route, 'params.reportID', ''));
const isNewReportIDSelectedLocally = useRef(false);
const optionsSelectorRef = useRef();
const isDistanceRequest = MoneyRequestUtils.isDistanceRequest(iouType.current, props.selectedTab);

const navigateToNextStep = () => {
const splitNavigateToNextStep = () => {
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current));
};

const moneyRequestNavigateToNextStep = (option) => {
isNewReportIDSelectedLocally.current = true;

if (!option.reportID) {
IOU.setMoneyRequestId(iouType.current);
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current));

return;
}

IOU.setMoneyRequestId(`${iouType.current}${option.reportID}`);
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, option.reportID));
};

const navigateBack = (forceFallback = false) => {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType.current, reportID.current), forceFallback);
};
Expand All @@ -64,15 +77,15 @@ function MoneyRequestParticipantsPage(props) {
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
if (prevMoneyRequestId.current !== props.iou.id) {
// The ID is cleared on completing a request. In that case, we will do nothing
if (!isDistanceRequest && props.iou.id) {
if (!isNewReportIDSelectedLocally.current && !isDistanceRequest && props.iou.id) {
navigateBack(true);
}
return;
}

// Reset the money request Onyx if the ID in Onyx does not match the ID from params
const moneyRequestId = `${iouType.current}${reportID.current}`;
const shouldReset = props.iou.id !== moneyRequestId;
const shouldReset = props.iou.id !== moneyRequestId && !isNewReportIDSelectedLocally.current;
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
}
Expand All @@ -94,20 +107,20 @@ function MoneyRequestParticipantsPage(props) {
{({safeAreaPaddingBottomStyle}) => (
<View style={styles.flex1}>
<HeaderWithBackButton
title={isDistanceRequest ? props.translate('common.distance') : props.translate('iou.cash')}
title={isDistanceRequest ? translate('common.distance') : translate('iou.cash')}
onBackButtonPress={navigateBack}
/>
{iouType.current === CONST.IOU.MONEY_REQUEST_TYPE.SPLIT ? (
<MoneyRequestParticipantsSplitSelector
onStepComplete={navigateToNextStep}
onStepComplete={splitNavigateToNextStep}
participants={props.iou.participants}
onAddParticipants={IOU.setMoneyRequestParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
/>
) : (
<MoneyRequestParticipantsSelector
ref={(el) => (optionsSelectorRef.current = el)}
onStepComplete={navigateToNextStep}
onStepComplete={moneyRequestNavigateToNextStep}
onAddParticipants={IOU.setMoneyRequestParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
iouType={iouType.current}
Expand All @@ -124,12 +137,11 @@ MoneyRequestParticipantsPage.displayName = 'IOUParticipantsPage';
MoneyRequestParticipantsPage.propTypes = propTypes;
MoneyRequestParticipantsPage.defaultProps = defaultProps;

export default compose(
withLocalize,
withOnyx({
iou: {key: ONYXKEYS.IOU},
selectedTab: {
key: `${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.RECEIPT_TAB_ID}`,
},
}),
)(MoneyRequestParticipantsPage);
export default withOnyx({
iou: {
key: ONYXKEYS.IOU,
},
selectedTab: {
key: `${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.RECEIPT_TAB_ID}`,
},
})(MoneyRequestParticipantsPage);
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class MoneyRequestParticipantsSelector extends Component {
*/
addSingleParticipant(option) {
this.props.onAddParticipants([{accountID: option.accountID, login: option.login, isPolicyExpenseChat: option.isPolicyExpenseChat, reportID: option.reportID, selected: true}]);
this.props.onStepComplete();
this.props.onStepComplete(option);
}

render() {
Expand Down