-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspaceNewRoomPage.js
187 lines (165 loc) · 6.86 KB
/
WorkspaceNewRoomPage.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import * as Report from '../../libs/actions/Report';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import Navigation from '../../libs/Navigation/Navigation';
import ScreenWrapper from '../../components/ScreenWrapper';
import styles from '../../styles/styles';
import RoomNameInput from '../../components/RoomNameInput';
import Picker from '../../components/Picker';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import Text from '../../components/Text';
import Permissions from '../../libs/Permissions';
import Log from '../../libs/Log';
import * as ValidationUtils from '../../libs/ValidationUtils';
import Form from '../../components/Form';
const propTypes = {
/** All reports shared with the user */
reports: PropTypes.shape({
/** The report name */
reportName: PropTypes.string,
/** The report type */
type: PropTypes.string,
/** ID of the policy */
policyID: PropTypes.string,
}).isRequired,
/** List of betas available to current user */
betas: PropTypes.arrayOf(PropTypes.string),
...withLocalizePropTypes,
};
const defaultProps = {
betas: [],
};
class WorkspaceNewRoomPage extends React.Component {
constructor(props) {
super(props);
this.state = {
visibilityDescription: this.props.translate('newRoomPage.restrictedDescription'),
};
this.validate = this.validate.bind(this);
this.submit = this.submit.bind(this);
this.updateVisibilityDescription = this.updateVisibilityDescription.bind(this);
}
/**
* @param {Object} values - form input values passed by the Form component
*/
submit(values) {
const policyID = this.props.policies[`${ONYXKEYS.COLLECTION.POLICY}${values.policyID}`];
Report.addPolicyReport(policyID, values.roomName, values.visibility);
}
/**
* @param {String} visibility - form input value passed by the Form component
*/
updateVisibilityDescription(visibility) {
const visibilityDescription = this.props.translate(`newRoomPage.${visibility}Description`);
if (visibilityDescription === this.state.visibilityDescription) {
return;
}
this.setState({visibilityDescription});
}
/**
* @param {Object} values - form input values passed by the Form component
* @returns {Boolean}
*/
validate(values) {
const errors = {};
// We error if the user doesn't enter a room name or left blank
if (!values.roomName || values.roomName === CONST.POLICY.ROOM_PREFIX) {
errors.roomName = this.props.translate('newRoomPage.pleaseEnterRoomName');
}
// We error if the room name already exists.
if (ValidationUtils.isExistingRoomName(values.roomName, this.props.reports, values.policyID)) {
errors.roomName = this.props.translate('newRoomPage.roomAlreadyExistsError');
}
// Certain names are reserved for default rooms and should not be used for policy rooms.
if (ValidationUtils.isReservedRoomName(values.roomName)) {
errors.roomName = this.props.translate('newRoomPage.roomNameReservedError');
}
if (!values.policyID) {
errors.policyID = this.props.translate('newRoomPage.pleaseSelectWorkspace');
}
return errors;
}
render() {
if (!Permissions.canUsePolicyRooms(this.props.betas)) {
Log.info('Not showing create Policy Room page since user is not on policy rooms beta');
Navigation.dismissModal();
return null;
}
// Workspaces are policies with type === 'free'
const workspaceOptions = _.map(
_.filter(this.props.policies, policy => policy && policy.type === CONST.POLICY.TYPE.FREE),
policy => ({label: policy.name, key: policy.id, value: policy.id}),
);
const visibilityOptions = _.map(_.values(CONST.REPORT.VISIBILITY), visibilityOption => ({
label: this.props.translate(`newRoomPage.visibilityOptions.${visibilityOption}`),
value: visibilityOption,
description: this.props.translate(`newRoomPage.${visibilityOption}Description`),
}));
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithCloseButton
title={this.props.translate('newRoomPage.newRoom')}
onCloseButtonPress={() => Navigation.dismissModal()}
/>
<Form
formID={ONYXKEYS.FORMS.NEW_ROOM_FORM}
submitButtonText={this.props.translate('newRoomPage.createRoom')}
style={[styles.mh5, styles.mt5, styles.flexGrow1]}
validate={this.validate}
onSubmit={this.submit}
enabledWhenOffline
>
<View style={styles.mb5}>
<RoomNameInput
inputID="roomName"
autoFocus
/>
</View>
<View style={styles.mb5}>
<Picker
inputID="policyID"
label={this.props.translate('workspace.common.workspace')}
placeholder={{value: '', label: this.props.translate('newRoomPage.selectAWorkspace')}}
items={workspaceOptions}
/>
</View>
<View style={styles.mb2}>
<Picker
inputID="visibility"
label={this.props.translate('newRoomPage.visibility')}
items={visibilityOptions}
onValueChange={this.updateVisibilityDescription}
defaultValue={CONST.REPORT.VISIBILITY.RESTRICTED}
/>
</View>
<Text style={[styles.textLabel, styles.colorMuted]}>
{this.state.visibilityDescription}
</Text>
</Form>
</ScreenWrapper>
);
}
}
WorkspaceNewRoomPage.propTypes = propTypes;
WorkspaceNewRoomPage.defaultProps = defaultProps;
export default compose(
withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
}),
withLocalize,
)(WorkspaceNewRoomPage);