Delegate all room alias validation to the RoomAliasField validator
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
65954b1e1b
commit
b6098ddb47
1 changed files with 43 additions and 36 deletions
|
@ -17,7 +17,7 @@ limitations under the License.
|
||||||
|
|
||||||
import EditableItemList from "../elements/EditableItemList";
|
import EditableItemList from "../elements/EditableItemList";
|
||||||
|
|
||||||
const React = require('react');
|
import React, {createRef} from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
const MatrixClientPeg = require('../../../MatrixClientPeg');
|
const MatrixClientPeg = require('../../../MatrixClientPeg');
|
||||||
const sdk = require("../../../index");
|
const sdk = require("../../../index");
|
||||||
|
@ -28,22 +28,41 @@ import AccessibleButton from "../elements/AccessibleButton";
|
||||||
const Modal = require("../../../Modal");
|
const Modal = require("../../../Modal");
|
||||||
|
|
||||||
class EditableAliasesList extends EditableItemList {
|
class EditableAliasesList extends EditableItemList {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
|
this._aliasField = createRef();
|
||||||
|
}
|
||||||
|
|
||||||
|
_onAliasAdded = async () => {
|
||||||
|
await this._aliasField.current.validate({ allowEmpty: false });
|
||||||
|
|
||||||
|
if (this._aliasField.current.isValid) {
|
||||||
|
if (this.props.onItemAdded) this.props.onItemAdded(this.props.newItem);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._aliasField.current.focus();
|
||||||
|
this._aliasField.current.validate({ allowEmpty: false, focused: true });
|
||||||
|
};
|
||||||
|
|
||||||
_renderNewItemField() {
|
_renderNewItemField() {
|
||||||
const RoomAliasField = sdk.getComponent('views.elements.RoomAliasField');
|
const RoomAliasField = sdk.getComponent('views.elements.RoomAliasField');
|
||||||
const onChange = (alias) => this._onNewItemChanged({target: {value: alias}});
|
const onChange = (alias) => this._onNewItemChanged({target: {value: alias}});
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
onSubmit={this._onItemAdded}
|
onSubmit={this._onAliasAdded}
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
noValidate={true}
|
noValidate={true}
|
||||||
className="mx_EditableItemList_newItem"
|
className="mx_EditableItemList_newItem"
|
||||||
>
|
>
|
||||||
<RoomAliasField
|
<RoomAliasField
|
||||||
id={`mx_EditableItemList_new_${this.props.id}`}
|
id={`mx_EditableItemList_new_${this.props.id}`}
|
||||||
|
ref={this._aliasField}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
value={this.props.newItem || ""}
|
value={this.props.newItem || ""}
|
||||||
domain={this.props.domain} />
|
domain={this.props.domain} />
|
||||||
<AccessibleButton onClick={this._onItemAdded} kind="primary">
|
<AccessibleButton onClick={this._onAliasAdded} kind="primary">
|
||||||
{ _t("Add") }
|
{ _t("Add") }
|
||||||
</AccessibleButton>
|
</AccessibleButton>
|
||||||
</form>
|
</form>
|
||||||
|
@ -99,11 +118,6 @@ export default class AliasSettings extends React.Component {
|
||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
isAliasValid(alias) {
|
|
||||||
// XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668
|
|
||||||
return (alias.match(/^#([^/:,]+?):(.+)$/) && encodeURI(alias) === alias);
|
|
||||||
}
|
|
||||||
|
|
||||||
changeCanonicalAlias(alias) {
|
changeCanonicalAlias(alias) {
|
||||||
if (!this.props.canSetCanonicalAlias) return;
|
if (!this.props.canSetCanonicalAlias) return;
|
||||||
|
|
||||||
|
@ -139,38 +153,31 @@ export default class AliasSettings extends React.Component {
|
||||||
|
|
||||||
const localDomain = MatrixClientPeg.get().getDomain();
|
const localDomain = MatrixClientPeg.get().getDomain();
|
||||||
if (!alias.includes(':')) alias += ':' + localDomain;
|
if (!alias.includes(':')) alias += ':' + localDomain;
|
||||||
if (this.isAliasValid(alias) && alias.endsWith(localDomain)) {
|
|
||||||
MatrixClientPeg.get().createAlias(alias, this.props.roomId).then(() => {
|
|
||||||
const localAliases = this.state.domainToAliases[localDomain] || [];
|
|
||||||
const domainAliases = Object.assign({}, this.state.domainToAliases);
|
|
||||||
domainAliases[localDomain] = [...localAliases, alias];
|
|
||||||
|
|
||||||
this.setState({
|
MatrixClientPeg.get().createAlias(alias, this.props.roomId).then(() => {
|
||||||
domainToAliases: domainAliases,
|
const localAliases = this.state.domainToAliases[localDomain] || [];
|
||||||
// Reset the add field
|
const domainAliases = Object.assign({}, this.state.domainToAliases);
|
||||||
newAlias: "",
|
domainAliases[localDomain] = [...localAliases, alias];
|
||||||
});
|
|
||||||
|
|
||||||
if (!this.state.canonicalAlias) {
|
this.setState({
|
||||||
this.changeCanonicalAlias(alias);
|
domainToAliases: domainAliases,
|
||||||
}
|
// Reset the add field
|
||||||
}).catch((err) => {
|
newAlias: "",
|
||||||
console.error(err);
|
|
||||||
Modal.createTrackedDialog('Error creating alias', '', ErrorDialog, {
|
|
||||||
title: _t("Error creating alias"),
|
|
||||||
description: _t(
|
|
||||||
"There was an error creating that alias. It may not be allowed by the server " +
|
|
||||||
"or a temporary failure occurred.",
|
|
||||||
),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
if (!this.state.canonicalAlias) {
|
||||||
Modal.createTrackedDialog('Invalid alias format', '', ErrorDialog, {
|
this.changeCanonicalAlias(alias);
|
||||||
title: _t('Invalid alias format'),
|
}
|
||||||
description: _t('\'%(alias)s\' is not a valid format for an alias', { alias: alias }),
|
}).catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
Modal.createTrackedDialog('Error creating alias', '', ErrorDialog, {
|
||||||
|
title: _t("Error creating alias"),
|
||||||
|
description: _t(
|
||||||
|
"There was an error creating that alias. It may not be allowed by the server " +
|
||||||
|
"or a temporary failure occurred.",
|
||||||
|
),
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
onLocalAliasDeleted = (index) => {
|
onLocalAliasDeleted = (index) => {
|
||||||
|
|
Loading…
Reference in a new issue