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

Add offline message to IOU and Split bill and allow currency selection while offline #4019

Merged
merged 14 commits into from
Jul 22, 2021
46 changes: 32 additions & 14 deletions src/components/IOUConfirmationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ import SafeAreaInsetPropTypes from '../pages/SafeAreaInsetPropTypes';
import withWindowDimensions, {windowDimensionsPropTypes} from './withWindowDimensions';
import compose from '../libs/compose';
import FixedFooter from './FixedFooter';
import CONST from '../CONST';

const propTypes = {
/** Callback to inform parent modal of success */
onConfirm: PropTypes.func.isRequired,

// User's currency preference
selectedCurrency: PropTypes.shape({
// Currency code for the selected currency
currencyCode: PropTypes.string,

// Currency symbol for the selected currency
currencySymbol: PropTypes.string,
}).isRequired,

// Callback to update comment from IOUModal
onUpdateComment: PropTypes.func,

Expand Down Expand Up @@ -80,20 +72,37 @@ const propTypes = {

/** Primary login of the user */
login: PropTypes.string,
}).isRequired,

// Selected Currency Code of the current IOU
selectedCurrencyCode: PropTypes.string,

// Currency Symbol of the Selected Currency
selectedCurrencySymbol: PropTypes.string,
}),

/** Holds data related to IOU view state, rather than the underlying IOU data. */
iou: PropTypes.shape({

/** Whether or not the IOU step is loading (creating the IOU Report) */
loading: PropTypes.bool,
}),

/** Information about the network */
network: PropTypes.shape({
/** Is the network currently offline or not */
isOffline: PropTypes.bool,
}),
};

const defaultProps = {
iou: {},
onUpdateComment: null,
comment: '',
network: {},
myPersonalDetails: {
selectedCurrencyCode: CONST.CURRENCY.USD,
selectedCurrencySymbol: '$',
},
};

// Gives minimum height to offset the height of
Expand All @@ -115,14 +124,14 @@ class IOUConfirmationList extends Component {
this.props.myPersonalDetails,
this.props.numberFormat(this.calculateAmount() / 100, {
style: 'currency',
currency: this.props.selectedCurrency.currencyCode,
currency: this.props.myPersonalDetails.selectedCurrencyCode,
}),
);

const formattedParticipants = getIOUConfirmationOptionsFromParticipants(this.props.participants,
this.props.numberFormat(this.calculateAmount() / 100, {
style: 'currency',
currency: this.props.selectedCurrency.currencyCode,
currency: this.props.myPersonalDetails.selectedCurrencyCode,
}));

sections.push({
Expand All @@ -141,7 +150,7 @@ class IOUConfirmationList extends Component {
const formattedParticipants = getIOUConfirmationOptionsFromParticipants(this.props.participants,
this.props.numberFormat(this.props.iouAmount, {
style: 'currency',
currency: this.props.selectedCurrency.currencyCode,
currency: this.props.myPersonalDetails.selectedCurrencyCode,
}));

sections.push({
Expand Down Expand Up @@ -237,7 +246,7 @@ class IOUConfirmationList extends Component {
this.props.hasMultipleParticipants ? 'iou.split' : 'iou.request', {
amount: this.props.numberFormat(
this.props.iouAmount,
{style: 'currency', currency: this.props.selectedCurrency.currencyCode},
{style: 'currency', currency: this.props.myPersonalDetails.selectedCurrencyCode},
),
},
);
Expand Down Expand Up @@ -274,8 +283,14 @@ class IOUConfirmationList extends Component {
</View>
</ScrollView>
<FixedFooter>
{this.props.network.isOffline && (
<Text style={[styles.formError, styles.pb2]}>
{this.props.translate('session.offlineMessage')}
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
</Text>
)}
<Button
success
isDisabled={this.props.network.isOffline}
style={[styles.w100]}
isLoading={this.props.iou.loading}
text={buttonText}
Expand All @@ -301,5 +316,8 @@ export default compose(
myPersonalDetails: {
key: ONYXKEYS.MY_PERSONAL_DETAILS,
},
network: {
key: ONYXKEYS.NETWORK,
},
}),
)(IOUConfirmationList);
28 changes: 28 additions & 0 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,32 @@ function rejectTransaction({
});
}

/**
* Resets IOU currency to preferredCurrency
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {Object} params.selectedCurrencyCode
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
* @param {Object} params.selectedCurrencySymbol
*/
function setSelectedCurrency({selectedCurrencyCode, selectedCurrencySymbol}) {
Onyx.merge(ONYXKEYS.MY_PERSONAL_DETAILS, {
selectedCurrencyCode,
selectedCurrencySymbol,
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
});
}

/**
* Resets IOU currency to preferredCurrency
*
* @param {Object} params.selectedCurrencyCode
* @param {Object} params.selectedCurrencySymbol
*/
function resetSelectedCurrency({selectedCurrencyCode, selectedCurrencySymbol}) {
Onyx.merge(ONYXKEYS.MY_PERSONAL_DETAILS, {
selectedCurrencyCode,
selectedCurrencySymbol,
});
}
luacmartins marked this conversation as resolved.
Show resolved Hide resolved

/**
* @private
*
Expand Down Expand Up @@ -239,4 +265,6 @@ export {
createIOUSplit,
rejectTransaction,
payIOUReport,
resetSelectedCurrency,
setSelectedCurrency,
};
44 changes: 26 additions & 18 deletions src/pages/iou/IOUCurrencySelection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component} from 'react';
import {SectionList, View} from 'react-native';
import PropTypes from 'prop-types';
import Onyx, {withOnyx} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import styles from '../../styles/styles';
import {getCurrencyList} from '../../libs/actions/PersonalDetails';
Expand All @@ -20,6 +20,7 @@ import CONST from '../../CONST';
import KeyboardAvoidingView from '../../components/KeyboardAvoidingView';
import Button from '../../components/Button';
import FixedFooter from '../../components/FixedFooter';
import {setSelectedCurrency} from '../../libs/actions/IOU';

/**
* IOU Currency selection for selecting currency
Expand All @@ -34,6 +35,12 @@ const propTypes = {

// Currency Symbol of the Preferred Currency
preferredCurrencySymbol: PropTypes.string,

// Preferred Currency Code of the current user
selectedCurrencyCode: PropTypes.string,

// Currency Symbol of the Preferred Currency
selectedCurrencySymbol: PropTypes.string,
}),

// The currency list constant object from Onyx
Expand All @@ -51,7 +58,12 @@ const propTypes = {
};

const defaultProps = {
myPersonalDetails: {preferredCurrencyCode: CONST.CURRENCY.USD, preferredCurrencySymbol: '$'},
myPersonalDetails: {
preferredCurrencyCode: CONST.CURRENCY.USD,
preferredCurrencySymbol: '$',
selectedCurrencyCode: CONST.CURRENCY.USD,
selectedCurrencySymbol: '$',
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
},
currencyList: {},
};

Expand All @@ -65,10 +77,6 @@ class IOUCurrencySelection extends Component {
this.state = {
searchValue: '',
currencyData: currencyOptions,
selectedCurrency: {
currencyCode: this.props.myPersonalDetails.preferredCurrencyCode,
currencySymbol: this.props.myPersonalDetails.preferredCurrencySymbol,
},
};
this.getCurrencyOptions = this.getCurrencyOptions.bind(this);
this.toggleOption = this.toggleOption.bind(this);
Expand Down Expand Up @@ -117,15 +125,13 @@ class IOUCurrencySelection extends Component {
/**
* Function which renders a row in the list
*
* @param {String} currencyCode
* @param {String} selectedCurrencyCode
*
*/
toggleOption(currencyCode) {
this.setState({
selectedCurrency: {
currencyCode,
currencySymbol: this.props.currencyList[currencyCode].symbol,
},
toggleOption(selectedCurrencyCode) {
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
setSelectedCurrency({
selectedCurrencyCode,
selectedCurrencySymbol: this.props.currencyList[selectedCurrencyCode].symbol,
});
}

Expand All @@ -150,9 +156,9 @@ class IOUCurrencySelection extends Component {
* @return {void}
*/
confirmCurrencySelection() {
Onyx.merge(ONYXKEYS.MY_PERSONAL_DETAILS, {
preferredCurrencyCode: this.state.selectedCurrency.currencyCode,
preferredCurrencySymbol: this.state.selectedCurrency.currencySymbol,
setSelectedCurrency({
selectedCurrencyCode: this.props.myPersonalDetails.selectedCurrencyCode,
selectedCurrencySymbol: this.props.myPersonalDetails.selectedCurrencySymbol,
});
Navigation.goBack();
}
Expand Down Expand Up @@ -193,7 +199,9 @@ class IOUCurrencySelection extends Component {
mode="compact"
option={item}
onSelectRow={() => this.toggleOption(item.currencyCode)}
isSelected={item.currencyCode === this.state.selectedCurrency.currencyCode}
isSelected={
item.currencyCode === this.props.myPersonalDetails.selectedCurrencyCode
}
showSelectedState
hideAdditionalOptionStates
/>
Expand All @@ -212,7 +220,7 @@ class IOUCurrencySelection extends Component {
<FixedFooter>
<Button
success
isDisabled={!this.state.selectedCurrency?.currencyCode}
isDisabled={!this.props.myPersonalDetails.selectedCurrencyCode}
style={[styles.w100]}
text={this.props.translate('iou.confirm')}
onPress={this.confirmCurrencySelection}
Expand Down
Loading