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

Migrate WorkspaceSettingsPage.js to function component #18299

Merged
merged 7 commits into from
May 9, 2023
Merged
Changes from 5 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
186 changes: 90 additions & 96 deletions src/pages/workspace/WorkspaceSettingsPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useCallback} from 'react';
import {Keyboard, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -39,129 +39,123 @@ const defaultProps = {
...policyDefaultProps,
};

class WorkspaceSettingsPage extends React.Component {
constructor(props) {
super(props);

this.getCurrencyItems = this.getCurrencyItems.bind(this);
this.submit = this.submit.bind(this);
this.validate = this.validate.bind(this);
}
function WorkspaceSettingsPage(props) {
const nameIsRequiredError = props.translate('workspace.editor.nameIsRequiredError');

/**
* @returns {Object[]}
*/
getCurrencyItems() {
const currencyListKeys = _.keys(this.props.currencyList);
function getCurrencyItems() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be useCallback as well? Or is it okay as is?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably reach for useMemo() here because we are calling getCurrencyItems() and by using useMemo() we will avoid having to compute the value again.

Copy link
Contributor

Choose a reason for hiding this comment

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

const currencyItems = useMemo(() => ... 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah nice call!

const currencyListKeys = _.keys(props.currencyList);
return _.map(currencyListKeys, currencyCode => ({
value: currencyCode,
label: `${currencyCode} - ${this.props.currencyList[currencyCode].symbol}`,
label: `${currencyCode} - ${props.currencyList[currencyCode].symbol}`,
}));
}

submit(values) {
if (this.props.policy.isPolicyUpdating) {
const submit = useCallback((values) => {
if (props.policy.isPolicyUpdating) {
return;
}
const outputCurrency = values.currency;
Policy.updateGeneralSettings(this.props.policy.id, values.name.trim(), outputCurrency);
Policy.updateGeneralSettings(props.policy.id, values.name.trim(), outputCurrency);
Keyboard.dismiss();
Navigation.navigate(ROUTES.getWorkspaceInitialRoute(this.props.policy.id));
}
Navigation.navigate(ROUTES.getWorkspaceInitialRoute(props.policy.id));
}, [props.policy.id, props.policy.isPolicyUpdating]);

validate(values) {
const validate = useCallback((values) => {
const errors = {};
const name = values.name.trim();

if (!name || !name.length) {
errors.name = this.props.translate('workspace.editor.nameIsRequiredError');
errors.name = nameIsRequiredError;
}

return errors;
}
}, [nameIsRequiredError]);

const policyName = lodashGet(props.policy, 'name', '');

render() {
const policyName = lodashGet(this.props.policy, 'name', '');
return (
<WorkspacePageWithSections
headerText={this.props.translate('workspace.common.settings')}
route={this.props.route}
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_SETTINGS}
>
{hasVBA => (
<Form
formID={ONYXKEYS.FORMS.WORKSPACE_SETTINGS_FORM}
submitButtonText={this.props.translate('workspace.editor.save')}
style={[styles.mh5, styles.flexGrow1]}
scrollContextEnabled
validate={this.validate}
onSubmit={this.submit}
enabledWhenOffline
return (
<WorkspacePageWithSections
headerText={props.translate('workspace.common.settings')}
route={props.route}
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_SETTINGS}
>
{hasVBA => (
<Form
formID={ONYXKEYS.FORMS.WORKSPACE_SETTINGS_FORM}
submitButtonText={props.translate('workspace.editor.save')}
style={[styles.mh5, styles.flexGrow1]}
scrollContextEnabled
validate={validate}
onSubmit={submit}
enabledWhenOffline
>
<OfflineWithFeedback
pendingAction={lodashGet(props.policy, 'pendingFields.avatar', null)}
errors={lodashGet(props.policy, 'errorFields.avatar', null)}
onClose={() => Policy.clearAvatarErrors(props.policy.id)}
>
<OfflineWithFeedback
pendingAction={lodashGet(this.props.policy, 'pendingFields.avatar', null)}
errors={lodashGet(this.props.policy, 'errorFields.avatar', null)}
onClose={() => Policy.clearAvatarErrors(this.props.policy.id)}
>
<AvatarWithImagePicker
isUploading={this.props.policy.isAvatarUploading}
source={lodashGet(this.props.policy, 'avatar')}
size={CONST.AVATAR_SIZE.LARGE}
DefaultAvatar={() => (
<Avatar
containerStyles={styles.avatarLarge}
imageStyles={[styles.avatarLarge, styles.alignSelfCenter]}
source={this.props.policy.avatar ? this.props.policy.avatar : ReportUtils.getDefaultWorkspaceAvatar(policyName)}
fallbackIcon={Expensicons.FallbackWorkspaceAvatar}
size={CONST.AVATAR_SIZE.LARGE}
name={policyName}
type={CONST.ICON_TYPE_WORKSPACE}
/>
)}
type={CONST.ICON_TYPE_WORKSPACE}
fallbackIcon={Expensicons.FallbackWorkspaceAvatar}
style={[styles.mb3]}
anchorPosition={{top: 172, right: 18}}
isUsingDefaultAvatar={!lodashGet(this.props.policy, 'avatar', null)}
onImageSelected={file => Policy.updateWorkspaceAvatar(lodashGet(this.props.policy, 'id', ''), file)}
onImageRemoved={() => Policy.deleteWorkspaceAvatar(lodashGet(this.props.policy, 'id', ''))}
editorMaskImage={Expensicons.ImageCropSquareMask}
/>
</OfflineWithFeedback>
<OfflineWithFeedback
pendingAction={lodashGet(this.props.policy, 'pendingFields.generalSettings')}
>
<TextInput
inputID="name"
label={this.props.translate('workspace.editor.nameInputLabel')}
containerStyles={[styles.mt4]}
defaultValue={this.props.policy.name}
maxLength={CONST.WORKSPACE_NAME_CHARACTER_LIMIT}
/>
<View style={[styles.mt4]}>
<Picker
inputID="currency"
label={this.props.translate('workspace.editor.currencyInputLabel')}
items={this.getCurrencyItems()}
isDisabled={hasVBA}
defaultValue={this.props.policy.outputCurrency}
hintText={
hasVBA
? this.props.translate('workspace.editor.currencyInputDisabledText')
: this.props.translate('workspace.editor.currencyInputHelpText')
}
<AvatarWithImagePicker
isUploading={props.policy.isAvatarUploading}
source={lodashGet(props.policy, 'avatar')}
size={CONST.AVATAR_SIZE.LARGE}
DefaultAvatar={() => (
<Avatar
containerStyles={styles.avatarLarge}
imageStyles={[styles.avatarLarge, styles.alignSelfCenter]}
source={props.policy.avatar ? props.policy.avatar : ReportUtils.getDefaultWorkspaceAvatar(policyName)}
fallbackIcon={Expensicons.FallbackWorkspaceAvatar}
size={CONST.AVATAR_SIZE.LARGE}
name={policyName}
type={CONST.ICON_TYPE_WORKSPACE}
/>
</View>
</OfflineWithFeedback>
</Form>
)}
</WorkspacePageWithSections>
);
}
)}
type={CONST.ICON_TYPE_WORKSPACE}
fallbackIcon={Expensicons.FallbackWorkspaceAvatar}
style={[styles.mb3]}
anchorPosition={{top: 172, right: 18}}
isUsingDefaultAvatar={!lodashGet(props.policy, 'avatar', null)}
onImageSelected={file => Policy.updateWorkspaceAvatar(lodashGet(props.policy, 'id', ''), file)}
onImageRemoved={() => Policy.deleteWorkspaceAvatar(lodashGet(props.policy, 'id', ''))}
editorMaskImage={Expensicons.ImageCropSquareMask}
/>
</OfflineWithFeedback>
<OfflineWithFeedback
pendingAction={lodashGet(props.policy, 'pendingFields.generalSettings')}
>
<TextInput
inputID="name"
label={props.translate('workspace.editor.nameInputLabel')}
containerStyles={[styles.mt4]}
defaultValue={props.policy.name}
maxLength={CONST.WORKSPACE_NAME_CHARACTER_LIMIT}
/>
<View style={[styles.mt4]}>
<Picker
inputID="currency"
label={props.translate('workspace.editor.currencyInputLabel')}
items={getCurrencyItems()}
isDisabled={hasVBA}
defaultValue={props.policy.outputCurrency}
hintText={
hasVBA
? props.translate('workspace.editor.currencyInputDisabledText')
: props.translate('workspace.editor.currencyInputHelpText')
}
/>
</View>
</OfflineWithFeedback>
</Form>
)}
</WorkspacePageWithSections>
);
}

WorkspaceSettingsPage.propTypes = propTypes;
WorkspaceSettingsPage.defaultProps = defaultProps;
WorkspaceSettingsPage.displayName = 'WorkspaceSettingsPage';

export default compose(
withPolicy,
Expand Down