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 currency selector to settings page #5713

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/components/ExpensiPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ const propTypes = {

/** Error text to display */
errorText: PropTypes.string,

/** Styles of the picker */
style: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]),
};

const defaultProps = {
label: '',
isDisabled: false,
hasError: false,
errorText: '',
style: [],
};

class ExpensiPicker extends PureComponent {
Expand All @@ -37,7 +41,7 @@ class ExpensiPicker extends PureComponent {

render() {
const {
label, isDisabled, ...pickerProps
label, isDisabled, style, ...pickerProps
} = this.props;
return (
<>
Expand All @@ -47,6 +51,7 @@ class ExpensiPicker extends PureComponent {
this.state.isOpen && styles.borderColorFocus,
isDisabled && styles.inputDisabled,
this.props.hasError && styles.borderColorDanger,
style,
]}
>
{label && (
Expand Down
3 changes: 3 additions & 0 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ export default {
editor: {
nameInputLabel: 'Name',
nameInputHelpText: 'This is the name you will see on your workspace.',
nameIsRequiredError: 'You need to define a name for your workspace',
currencyInputLabel: 'Default Currency',
currencyInputHelpText: 'All expenses on this workspace will be converted to this currency.',
save: 'Save',
genericFailureMessage: 'An error occurred updating the workspace, please try again.',
avatarUploadFailureMessage: 'An error occurred uploading the avatar, please try again.',
Expand Down
4 changes: 4 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Onyx.connect({
* @param {String} fullPolicy.name
* @param {String} fullPolicy.role
* @param {String} fullPolicy.type
* @param {String} fullPolicy.outputCurrency
* @returns {Object}
*/
function getSimplifiedPolicyObject(fullPolicy) {
Expand All @@ -38,6 +39,7 @@ function getSimplifiedPolicyObject(fullPolicy) {
name: fullPolicy.name,
role: fullPolicy.role,
type: fullPolicy.type,
outputCurrency: fullPolicy.outputCurrency,
};
}

Expand Down Expand Up @@ -105,6 +107,7 @@ function getPolicyList() {
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: {
employeeList: getSimplifiedEmployeeList(policy.value.employeeList),
avatarURL: lodashGet(policy, 'value.avatarURL', ''),
outputCurrency: policy.value.outputCurrency,
},
}), {});
updateAllPolicies(policyDataToStore);
Expand Down Expand Up @@ -235,6 +238,7 @@ function create(name = '') {
type: response.policy.type,
name: response.policy.name,
role: CONST.POLICY.ROLE.ADMIN,
outputCurrency: response.policy.outputCurrency,
});
}).then(() => {
Navigation.dismissModal();
Expand Down
141 changes: 89 additions & 52 deletions src/pages/workspace/WorkspaceSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import ONYXKEYS from '../../ONYXKEYS';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import ScreenWrapper from '../../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import Navigation from '../../libs/Navigation/Navigation';
import Permissions from '../../libs/Permissions';
import styles from '../../styles/styles';
import TextInputWithLabel from '../../components/TextInputWithLabel';
import Button from '../../components/Button';
import Text from '../../components/Text';
import compose from '../../libs/compose';
Expand All @@ -24,6 +21,11 @@ import AvatarWithImagePicker from '../../components/AvatarWithImagePicker';
import defaultTheme from '../../styles/themes/default';
import Growl from '../../libs/Growl';
import CONST from '../../CONST';
import ExpensiPicker from '../../components/ExpensiPicker';
import {getCurrencyList} from '../../libs/actions/PersonalDetails';
import ExpensiTextInput from '../../components/ExpensiTextInput/index';
import FixedFooter from '../../components/FixedFooter';
import WorkspacePageWithSections from './WorkspacePageWithSections';

const propTypes = {
/** List of betas */
Expand All @@ -43,14 +45,20 @@ class WorkspaceSettingsPage extends React.Component {
name: props.policy.name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

props.policy is missing from the propTypes. Can you please add it?

avatarURL: props.policy.avatarURL,
previewAvatarURL: props.policy.avatarURL,
currency: props.policy.outputCurrency,
};

this.submit = this.submit.bind(this);
this.onImageSelected = this.onImageSelected.bind(this);
this.onImageRemoved = this.onImageRemoved.bind(this);
this.getCurrencyItems = this.getCurrencyItems.bind(this);
this.uploadAvatarPromise = Promise.resolve();
}

componentDidMount() {
getCurrencyList();
}

onImageSelected(image) {
updateLocalPolicyValues(this.props.policy.id, {isAvatarUploading: true});
this.setState({previewAvatarURL: image.uri});
Expand All @@ -67,6 +75,19 @@ class WorkspaceSettingsPage extends React.Component {
this.setState({previewAvatarURL: '', avatarURL: ''});
}

/**
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line

* @returns {Object[]}
*/
getCurrencyItems() {
const currencyListKeys = _.keys(this.props.currencyList);
const a = _.map(currencyListKeys, currencyCode => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use a short variable and just use return _.map();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I was debugging this and added it for easy inspection, removing it

value: currencyCode,
label: `${currencyCode} - ${this.props.currencyList[currencyCode].symbol}`,
}));
return a;
}

submit() {
updateLocalPolicyValues(this.props.policy.id, {isPolicyUpdating: true});

Expand All @@ -75,8 +96,9 @@ class WorkspaceSettingsPage extends React.Component {
const name = this.state.name.trim();
const avatarURL = this.state.avatarURL;
const policyID = this.props.policy.id;
const currency = this.state.currency;

update(policyID, {name, avatarURL});
update(policyID, {name, avatarURL, outputCurrency: currency});
}).catch(() => {
updateLocalPolicyValues(this.props.policy.id, {isPolicyUpdating: false});
});
Expand All @@ -98,55 +120,69 @@ class WorkspaceSettingsPage extends React.Component {
|| (this.state.avatarURL === this.props.policy.avatarURL
&& this.state.name === this.props.policy.name);
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={this.props.translate('workspace.common.edit')}
onCloseButtonPress={Navigation.dismissModal}
/>

<View style={[styles.pageWrapper, styles.flex1, styles.pRelative]}>
<View style={[styles.w100, styles.flex1]}>
<AvatarWithImagePicker
isUploading={policy.isAvatarUploading}
avatarURL={this.state.previewAvatarURL}
size={CONST.AVATAR_SIZE.LARGE}
DefaultAvatar={() => (
<Icon
src={Workspace}
height={80}
width={80}
fill={defaultTheme.iconSuccessFill}
<WorkspacePageWithSections
headerText={this.props.translate('workspace.common.edit')}
route={this.props.route}
>
{(hasVBA) => (
<>
<View style={[styles.pageWrapper, styles.flex1, styles.pRelative]}>
<View style={[styles.w100, styles.flex1]}>
<AvatarWithImagePicker
isUploading={policy.isAvatarUploading}
avatarURL={this.state.previewAvatarURL}
size={CONST.AVATAR_SIZE.LARGE}
DefaultAvatar={() => (
<Icon
src={Workspace}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is supposed to be Camera

height={80}
width={80}
fill={defaultTheme.iconSuccessFill}
/>
)}
style={[styles.mb3]}
anchorPosition={{top: 172, right: 18}}
isUsingDefaultAvatar={!this.state.previewAvatarURL}
onImageSelected={this.onImageSelected}
onImageRemoved={this.onImageRemoved}
/>

<ExpensiTextInput
label={this.props.translate('workspace.editor.nameInputLabel')}
containerStyles={[styles.mt4]}
onChangeText={name => this.setState({name})}
value={this.state.name}
hasError={!this.state.name.trim().length}
errorText={this.state.name.trim().length ? '' : this.props.translate('workspace.editor.nameIsRequiredError')}
/>

<ExpensiPicker
label={this.props.translate('workspace.editor.currencyInputLabel')}
onChange={currency => this.setState({currency})}
items={this.getCurrencyItems()}
value={this.state.currency}
style={[styles.mt4]}
isDisabled={hasVBA}
/>
<Text style={[styles.mt2, styles.formHint]}>
{this.props.translate('workspace.editor.currencyInputHelpText')}
</Text>
</View>

<FixedFooter style={[styles.w100]}>
<Button
success
isLoading={policy.isPolicyUpdating}
isDisabled={isButtonDisabled}
text={this.props.translate('workspace.editor.save')}
onPress={this.submit}
pressOnEnter
/>
)}
style={[styles.mb3]}
anchorPosition={{top: 172, right: 18}}
isUsingDefaultAvatar={!this.state.previewAvatarURL}
onImageSelected={this.onImageSelected}
onImageRemoved={this.onImageRemoved}
/>

<TextInputWithLabel
label={this.props.translate('workspace.editor.nameInputLabel')}
value={this.state.name}
onChangeText={name => this.setState({name})}
onSubmitEditting={this.submit}
/>
<Text style={[styles.mt2, styles.formHint]}>
{this.props.translate('workspace.editor.nameInputHelpText')}
</Text>
</View>

<Button
success
isLoading={policy.isPolicyUpdating}
isDisabled={isButtonDisabled}
style={[styles.w100]}
text={this.props.translate('workspace.editor.save')}
onPress={this.submit}
pressOnEnter
/>
</View>
</ScreenWrapper>
</FixedFooter>
</View>
</>
)}
</WorkspacePageWithSections>
);
}
}
Expand All @@ -167,6 +203,7 @@ export default compose(
return `${ONYXKEYS.COLLECTION.POLICY}${policyID}`;
},
},
currencyList: {key: ONYXKEYS.CURRENCY_LIST},
}),
withLocalize,
)(WorkspaceSettingsPage);