-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspaceNewRoomPage.js
205 lines (186 loc) · 7.85 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React from 'react';
import {ScrollView, View} from 'react-native';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import withFullPolicy, {fullPolicyDefaultProps, fullPolicyPropTypes} from './withFullPolicy';
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 TextInputWithLabel from '../../components/TextInputWithLabel';
import ExpensiPicker from '../../components/ExpensiPicker';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import Button from '../../components/Button';
import FixedFooter from '../../components/FixedFooter';
import * as Report from '../../libs/actions/Report';
import Permissions from '../../libs/Permissions';
import Log from '../../libs/Log';
const propTypes = {
/** All reports shared with the user */
reports: PropTypes.shape({
reportName: PropTypes.string,
type: PropTypes.string,
policyID: PropTypes.string,
}).isRequired,
/** Are we loading the createPolicyRoom command */
isLoadingCreatePolicyRoom: PropTypes.bool,
...fullPolicyPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
betas: [],
isLoadingCreatePolicyRoom: false,
...fullPolicyDefaultProps,
};
class WorkspaceNewRoomPage extends React.Component {
constructor(props) {
super(props);
this.state = {
roomName: '',
policyID: '',
visibility: CONST.REPORT.VISIBILITY.RESTRICTED,
error: '',
workspaceOptions: [],
};
this.onWorkspaceSelect = this.onWorkspaceSelect.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.checkAndModifyRoomName = this.checkAndModifyRoomName.bind(this);
}
componentDidMount() {
// Workspaces are policies with type === 'free'
const workspaces = _.filter(this.props.policies, policy => policy && policy.type === CONST.POLICY.TYPE.FREE);
this.setState({workspaceOptions: _.map(workspaces, policy => ({label: policy.name, key: policy.id, value: policy.id}))});
}
componentDidUpdate(prevProps) {
if (this.props.policies.length === prevProps.policies.length) {
return;
}
// Workspaces are policies with type === 'free'
const workspaces = _.filter(this.props.policies, policy => policy && policy.type === CONST.POLICY.TYPE.FREE);
// eslint-disable-next-line react/no-did-update-set-state
this.setState({workspaceOptions: _.map(workspaces, policy => ({label: policy.name, key: policy.id, value: policy.id}))});
}
/**
* Called when a workspace is selected. Also calls checkAndModifyRoomName,
* which displays an error if the given roomName exists on the newly selected workspace.
* @param {String} policyID
*/
onWorkspaceSelect(policyID) {
this.setState({policyID});
this.checkAndModifyRoomName(this.state.roomName);
}
/**
* Called when the "Create Room" button is pressed.
*/
onSubmit() {
Report.createPolicyRoom(this.state.policyID, this.state.roomName, this.state.visibility);
}
/**
* Modifies the room name to follow our conventions:
* - Max length 80 characters
* - Cannot not include space or special characters, and we automatically apply an underscore for spaces
* - Must be lowercase
* Also checks to see if this room name already exists, and displays an error message if so.
* @param {String} roomName
*
* @returns {String}
*/
checkAndModifyRoomName(roomName) {
const modifiedRoomNameWithoutHash = roomName.substr(1)
.replace(/ /g, '_')
.replace(/[^a-zA-Z\d_]/g, '')
.substr(0, CONST.REPORT.MAX_ROOM_NAME_LENGTH)
.toLowerCase();
const finalRoomName = `#${modifiedRoomNameWithoutHash}`;
const isExistingRoomName = _.some(
_.values(this.props.reports),
report => report && report.policyID === this.state.policyID && report.reportName === finalRoomName,
);
if (isExistingRoomName) {
this.setState({error: this.props.translate('newRoomPage.roomAlreadyExists')});
} else {
this.setState({error: ''});
}
return finalRoomName;
}
render() {
if (!Permissions.canUseDefaultRooms(this.props.betas)) {
Log.info('Not showing create Policy Room page since user is not on default rooms beta');
Navigation.dismissModal();
return null;
}
const shouldDisableSubmit = Boolean(!this.state.roomName || !this.state.policyID || this.state.error);
const visibilityOptions = _.map(_.values(CONST.REPORT.VISIBILITY), visibilityOption => ({
label: this.props.translate(`newRoomPage.visibilityOptions.${visibilityOption}`),
value: visibilityOption,
}));
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={this.props.translate('newRoomPage.newRoom')}
onCloseButtonPress={() => Navigation.dismissModal()}
/>
<ScrollView style={styles.flex1} contentContainerStyle={styles.p5}>
<TextInputWithLabel
label={this.props.translate('newRoomPage.roomName')}
prefixCharacter="#"
placeholder={this.props.translate('newRoomPage.social')}
containerStyles={[styles.mb5]}
onChangeText={roomName => this.setState({roomName: this.checkAndModifyRoomName(roomName)})}
value={this.state.roomName.substr(1)}
errorText={this.state.error}
/>
<View style={styles.mb5}>
<ExpensiPicker
value={this.state.policyID}
label={this.props.translate('workspace.common.workspace')}
placeholder={{value: '', label: this.props.translate('newRoomPage.selectAWorkspace')}}
items={this.state.workspaceOptions}
onChange={this.onWorkspaceSelect}
/>
</View>
<ExpensiPicker
value={this.state.visibility}
label={this.props.translate('newRoomPage.visibility')}
items={visibilityOptions}
onChange={visibility => this.setState({visibility})}
/>
</ScrollView>
<FixedFooter>
<Button
isLoading={this.props.isLoadingCreatePolicyRoom}
isDisabled={shouldDisableSubmit}
success
onPress={this.onSubmit}
style={[styles.w100]}
text={this.props.translate('newRoomPage.createRoom')}
/>
</FixedFooter>
</ScreenWrapper>
);
}
}
WorkspaceNewRoomPage.propTypes = propTypes;
WorkspaceNewRoomPage.defaultProps = defaultProps;
export default compose(
withFullPolicy,
withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
isLoadingCreatePolicyRoom: {
key: ONYXKEYS.IS_LOADING_CREATE_POLICY_ROOM,
},
}),
withLocalize,
)(WorkspaceNewRoomPage);