-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspaceSettingsPage.js
170 lines (158 loc) · 7.45 KB
/
WorkspaceSettingsPage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React from 'react';
import {Keyboard, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import ONYXKEYS from '../../ONYXKEYS';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import styles from '../../styles/styles';
import compose from '../../libs/compose';
import * as Policy from '../../libs/actions/Policy';
import * as Expensicons from '../../components/Icon/Expensicons';
import AvatarWithImagePicker from '../../components/AvatarWithImagePicker';
import CONST from '../../CONST';
import Picker from '../../components/Picker';
import TextInput from '../../components/TextInput';
import WorkspacePageWithSections from './WorkspacePageWithSections';
import withPolicy, {policyPropTypes, policyDefaultProps} from './withPolicy';
import {withNetwork} from '../../components/OnyxProvider';
import OfflineWithFeedback from '../../components/OfflineWithFeedback';
import Form from '../../components/Form';
import * as ReportUtils from '../../libs/ReportUtils';
import Avatar from '../../components/Avatar';
const propTypes = {
// The currency list constant object from Onyx
currencyList: PropTypes.objectOf(PropTypes.shape({
// Symbol for the currency
symbol: PropTypes.string,
})),
...policyPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
currencyList: {},
...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);
}
/**
* @returns {Object[]}
*/
getCurrencyItems() {
const currencyListKeys = _.keys(this.props.currencyList);
return _.map(currencyListKeys, currencyCode => ({
value: currencyCode,
label: `${currencyCode} - ${this.props.currencyList[currencyCode].symbol}`,
}));
}
submit(values) {
if (this.props.policy.isPolicyUpdating) {
return;
}
const outputCurrency = values.currency;
Policy.updateGeneralSettings(this.props.policy.id, values.name, outputCurrency);
Keyboard.dismiss();
}
validate(values) {
const errors = {};
const name = values.name.trim();
if (!name || !name.length) {
errors.name = this.props.translate('workspace.editor.nameIsRequiredError');
}
return errors;
}
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
>
<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')
}
/>
</View>
</OfflineWithFeedback>
</Form>
)}
</WorkspacePageWithSections>
);
}
}
WorkspaceSettingsPage.propTypes = propTypes;
WorkspaceSettingsPage.defaultProps = defaultProps;
export default compose(
withPolicy,
withOnyx({
currencyList: {key: ONYXKEYS.CURRENCY_LIST},
}),
withLocalize,
withNetwork(),
)(WorkspaceSettingsPage);