2017-06-29 16:03:05 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2017-06-29 16:51:38 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-29 16:03:05 +00:00
|
|
|
import sdk from '../../../index';
|
|
|
|
import dis from '../../../dispatcher';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
|
|
|
|
export default React.createClass({
|
|
|
|
displayName: 'CreateGroupDialog',
|
|
|
|
propTypes: {
|
2017-06-29 16:51:38 +00:00
|
|
|
onFinished: PropTypes.func.isRequired,
|
2017-06-29 16:03:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
groupName: '',
|
|
|
|
groupId: '',
|
|
|
|
groupError: null,
|
|
|
|
creating: false,
|
|
|
|
createError: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
_onGroupNameChange: function(e) {
|
|
|
|
this.setState({
|
|
|
|
groupName: e.target.value,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_onGroupIdChange: function(e) {
|
|
|
|
this.setState({
|
|
|
|
groupId: e.target.value,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_onGroupIdBlur: function(e) {
|
|
|
|
this._checkGroupId();
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkGroupId: function(e) {
|
|
|
|
let error = null;
|
2017-10-31 11:48:56 +00:00
|
|
|
if (!/^[a-z0-9=_\-\.\/]*$/.test(this.state.groupId)) {
|
2017-10-31 10:25:06 +00:00
|
|
|
error = _t("Community IDs may only contain characters a-z, 0-9, or '=_-./'");
|
2017-06-29 16:03:05 +00:00
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
groupIdError: error,
|
|
|
|
});
|
|
|
|
return error;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onFormSubmit: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (this._checkGroupId()) return;
|
|
|
|
|
|
|
|
const profile = {};
|
|
|
|
if (this.state.groupName !== '') {
|
|
|
|
profile.name = this.state.groupName;
|
|
|
|
}
|
|
|
|
this.setState({creating: true});
|
|
|
|
MatrixClientPeg.get().createGroup({
|
2017-10-19 15:47:43 +00:00
|
|
|
localpart: this.state.groupId,
|
2017-06-29 16:03:05 +00:00
|
|
|
profile: profile,
|
|
|
|
}).then((result) => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_group',
|
|
|
|
group_id: result.group_id,
|
2017-11-06 18:02:50 +00:00
|
|
|
group_is_new: true,
|
2017-06-29 16:03:05 +00:00
|
|
|
});
|
|
|
|
this.props.onFinished(true);
|
|
|
|
}).catch((e) => {
|
|
|
|
this.setState({createError: e});
|
|
|
|
}).finally(() => {
|
|
|
|
this.setState({creating: false});
|
|
|
|
}).done();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onCancel: function() {
|
|
|
|
this.props.onFinished(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
2017-07-07 17:34:40 +00:00
|
|
|
const Spinner = sdk.getComponent('elements.Spinner');
|
2017-06-29 16:03:05 +00:00
|
|
|
|
|
|
|
if (this.state.creating) {
|
2017-07-07 17:34:40 +00:00
|
|
|
return <Spinner />;
|
2017-06-29 16:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let createErrorNode;
|
|
|
|
if (this.state.createError) {
|
|
|
|
// XXX: We should catch errcodes and give sensible i18ned messages for them,
|
|
|
|
// rather than displaying what the server gives us, but synapse doesn't give
|
|
|
|
// any yet.
|
|
|
|
createErrorNode = <div className="error">
|
2017-10-23 13:57:40 +00:00
|
|
|
<div>{ _t('Something went wrong whilst creating your community') }</div>
|
2017-09-28 10:21:06 +00:00
|
|
|
<div>{ this.state.createError.message }</div>
|
2017-06-29 16:03:05 +00:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BaseDialog className="mx_CreateGroupDialog" onFinished={this.props.onFinished}
|
2017-10-19 14:01:16 +00:00
|
|
|
title={_t('Create Community')}
|
2017-06-29 16:03:05 +00:00
|
|
|
>
|
|
|
|
<form onSubmit={this._onFormSubmit}>
|
|
|
|
<div className="mx_Dialog_content">
|
2017-07-07 17:34:40 +00:00
|
|
|
<div className="mx_CreateGroupDialog_inputRow">
|
|
|
|
<div className="mx_CreateGroupDialog_label">
|
2017-10-19 14:01:16 +00:00
|
|
|
<label htmlFor="groupname">{ _t('Community Name') }</label>
|
2017-07-07 17:34:40 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<input id="groupname" className="mx_CreateGroupDialog_input"
|
|
|
|
autoFocus={true} size="64"
|
|
|
|
placeholder={_t('Example')}
|
|
|
|
onChange={this._onGroupNameChange}
|
|
|
|
value={this.state.groupName}
|
|
|
|
/>
|
|
|
|
</div>
|
2017-06-29 16:03:05 +00:00
|
|
|
</div>
|
2017-07-07 17:34:40 +00:00
|
|
|
<div className="mx_CreateGroupDialog_inputRow">
|
|
|
|
<div className="mx_CreateGroupDialog_label">
|
2017-10-19 14:01:16 +00:00
|
|
|
<label htmlFor="groupid">{ _t('Community ID') }</label>
|
2017-07-07 17:34:40 +00:00
|
|
|
</div>
|
2017-11-03 15:38:12 +00:00
|
|
|
<div className="mx_CreateGroupDialog_input_group">
|
2017-11-03 16:02:41 +00:00
|
|
|
<span className="mx_CreateGroupDialog_prefix">+</span>
|
|
|
|
<input id="groupid"
|
|
|
|
className="mx_CreateGroupDialog_input mx_CreateGroupDialog_input_hasPrefixAndSuffix"
|
2017-10-19 15:47:43 +00:00
|
|
|
size="32"
|
|
|
|
placeholder={_t('example')}
|
2017-07-07 17:34:40 +00:00
|
|
|
onChange={this._onGroupIdChange}
|
|
|
|
onBlur={this._onGroupIdBlur}
|
|
|
|
value={this.state.groupId}
|
|
|
|
/>
|
2017-11-03 16:02:41 +00:00
|
|
|
<span className="mx_CreateGroupDialog_suffix">
|
2017-11-03 15:38:12 +00:00
|
|
|
:{ MatrixClientPeg.get().getDomain() }
|
|
|
|
</span>
|
2017-07-07 17:34:40 +00:00
|
|
|
</div>
|
2017-06-29 16:03:05 +00:00
|
|
|
</div>
|
|
|
|
<div className="error">
|
2017-09-28 10:21:06 +00:00
|
|
|
{ this.state.groupIdError }
|
2017-06-29 16:03:05 +00:00
|
|
|
</div>
|
2017-09-28 10:21:06 +00:00
|
|
|
{ createErrorNode }
|
2017-06-29 16:03:05 +00:00
|
|
|
</div>
|
|
|
|
<div className="mx_Dialog_buttons">
|
|
|
|
<button onClick={this._onCancel}>
|
|
|
|
{ _t("Cancel") }
|
|
|
|
</button>
|
|
|
|
<input type="submit" value={_t('Create')} className="mx_Dialog_primary" />
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</BaseDialog>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|