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

Rate unit page accessed from url #28004

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Changes from 2 commits
Commits
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
66 changes: 60 additions & 6 deletions src/pages/workspace/reimburse/WorkspaceRateAndUnitPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import {Keyboard, View} from 'react-native';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
import ONYXKEYS from '../../../ONYXKEYS';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import styles from '../../../styles/styles';
Expand All @@ -18,13 +19,19 @@ import Form from '../../../components/Form';
import Navigation from '../../../libs/Navigation/Navigation';
import ROUTES from '../../../ROUTES';
import getPermittedDecimalSeparator from '../../../libs/getPermittedDecimalSeparator';
import * as BankAccounts from '../../../libs/actions/BankAccounts';
import * as ReimbursementAccountProps from '../../ReimbursementAccount/reimbursementAccountPropTypes';

const propTypes = {
/** Bank account attached to free plan */
reimbursementAccount: ReimbursementAccountProps.reimbursementAccountPropTypes,

...policyPropTypes,
...withLocalizePropTypes,
};

const defaultProps = {
reimbursementAccount: {},
...policyDefaultProps,
};

Expand All @@ -33,6 +40,33 @@ class WorkspaceRateAndUnitPage extends React.Component {
super(props);
this.submit = this.submit.bind(this);
this.validate = this.validate.bind(this);

this.state = {
rate: 0,
unit: 'mi',
alitoshmatov marked this conversation as resolved.
Show resolved Hide resolved
};
}

componentDidMount() {
this.resetRateAndUnit();

if (lodashGet(this.props, 'policy.customUnits', []).length !== 0) {
return;
}
// When this page is accessed directly from url, the policy.customUnits data won't be available,
// and we should trigger Policy.openWorkspaceReimburseView to get the data

BankAccounts.setReimbursementAccountLoading(true);
Policy.openWorkspaceReimburseView(this.props.policy.id);
}

componentDidUpdate(prevProps) {
// We should update rate input when rate data is fetched
if (prevProps.reimbursementAccount.isLoading === this.props.reimbursementAccount.isLoading) {
return;
}

this.resetRateAndUnit();
}

getUnitRateValue(customUnitRate) {
Expand Down Expand Up @@ -62,6 +96,16 @@ class WorkspaceRateAndUnitPage extends React.Component {
return numValue.toFixed(3);
}

resetRateAndUnit() {
const distanceCustomUnit = _.find(lodashGet(this.props, 'policy.customUnits', {}), (unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE);
const distanceCustomRate = _.find(lodashGet(distanceCustomUnit, 'rates', {}), (rate) => rate.name === CONST.CUSTOM_UNITS.DEFAULT_RATE);

this.setState({
rate: this.getUnitRateValue(distanceCustomRate),
unit: lodashGet(distanceCustomUnit, 'attributes.unit', 'mi'),
alitoshmatov marked this conversation as resolved.
Show resolved Hide resolved
});
}

saveUnitAndRate(unit, rate) {
const distanceCustomUnit = _.find(lodashGet(this.props, 'policy.customUnits', {}), (u) => u.name === CONST.CUSTOM_UNITS.NAME_DISTANCE);
if (!distanceCustomUnit) {
Expand All @@ -84,8 +128,8 @@ class WorkspaceRateAndUnitPage extends React.Component {
Policy.updateWorkspaceCustomUnitAndRate(this.props.policy.id, distanceCustomUnit, newCustomUnit, this.props.policy.lastModified);
}

submit(values) {
this.saveUnitAndRate(values.unit, values.rate);
submit() {
this.saveUnitAndRate(this.state.unit, this.state.rate);
Keyboard.dismiss();
Navigation.goBack(ROUTES.getWorkspaceReimburseRoute(this.props.policy.id));
}
Expand Down Expand Up @@ -137,21 +181,22 @@ class WorkspaceRateAndUnitPage extends React.Component {
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
inputID="rate"
containerStyles={[styles.mt4]}
defaultValue={this.getUnitRateValue(distanceCustomRate)}
label={this.props.translate('workspace.reimburse.trackDistanceRate')}
accessibilityLabel={this.props.translate('workspace.reimburse.trackDistanceRate')}
placeholder={lodashGet(this.props, 'policy.outputCurrency', CONST.CURRENCY.USD)}
autoCompleteType="off"
autoCorrect={false}
keyboardType={CONST.KEYBOARD_TYPE.DECIMAL_PAD}
maxLength={12}
value={this.state.rate}
onChangeText={(value) => this.setState({rate: value})}
/>
<View style={[styles.mt4]}>
<Picker
inputID="unit"
defaultValue={lodashGet(distanceCustomUnit, 'attributes.unit', 'mi')}
value={this.state.unit}
label={this.props.translate('workspace.reimburse.trackDistanceUnit')}
items={this.getUnitItems()}
onInputChange={(value) => this.setState({unit: value})}
/>
</View>
</OfflineWithFeedback>
Expand All @@ -165,4 +210,13 @@ class WorkspaceRateAndUnitPage extends React.Component {
WorkspaceRateAndUnitPage.propTypes = propTypes;
WorkspaceRateAndUnitPage.defaultProps = defaultProps;

export default compose(withPolicy, withLocalize, withNetwork())(WorkspaceRateAndUnitPage);
export default compose(
withPolicy,
withLocalize,
withNetwork(),
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
}),
)(WorkspaceRateAndUnitPage);
Loading