Make the canonical alias direct manipulation

This commit is contained in:
Travis Ralston 2019-02-21 16:11:09 -07:00
parent 278e48cfc9
commit 750c9202cc
2 changed files with 40 additions and 15 deletions

View file

@ -55,6 +55,7 @@ export default class AliasSettings extends React.Component {
domainToAliases: {}, // { domain.com => [#alias1:domain.com, #alias2:domain.com] } domainToAliases: {}, // { domain.com => [#alias1:domain.com, #alias2:domain.com] }
remoteDomains: [], // [ domain.com, foobar.com ] remoteDomains: [], // [ domain.com, foobar.com ]
canonicalAlias: null, // #canonical:domain.com canonicalAlias: null, // #canonical:domain.com
updatingCanonicalAlias: false,
}; };
const localDomain = MatrixClientPeg.get().getDomain(); const localDomain = MatrixClientPeg.get().getDomain();
@ -140,6 +141,32 @@ export default class AliasSettings extends React.Component {
return ObjectUtils.getKeyValueArrayDiffs(oldAliases, this.state.domainToAliases); return ObjectUtils.getKeyValueArrayDiffs(oldAliases, this.state.domainToAliases);
} }
changeCanonicalAlias(alias) {
if (!this.props.canSetCanonicalAlias) return;
this.setState({
canonicalAlias: alias,
updatingCanonicalAlias: true,
});
const eventContent = {};
if (alias) eventContent["alias"] = alias;
MatrixClientPeg.get().sendStateEvent(this.props.roomId, "m.room.canonical_alias",
eventContent, "").catch((err) => {
console.error(err);
Modal.createTrackedDialog('Error updating main address', '', ErrorDialog, {
title: _t("Error updating main address"),
description: _t(
"There was an error updating the room's main address. It may not be allowed by the server " +
"or a temporary failure occurred."
),
});
}).finally(() => {
this.setState({updatingCanonicalAlias: false});
});
}
onNewAliasChanged = (value) => { onNewAliasChanged = (value) => {
this.setState({newAlias: value}); this.setState({newAlias: value});
}; };
@ -155,18 +182,15 @@ export default class AliasSettings extends React.Component {
const domainAliases = Object.assign({}, this.state.domainToAliases); const domainAliases = Object.assign({}, this.state.domainToAliases);
domainAliases[localDomain] = [...localAliases, alias]; domainAliases[localDomain] = [...localAliases, alias];
const newState = { this.setState({
domainToAliases: domainAliases, domainToAliases: domainAliases,
// Reset the add field // Reset the add field
newAlias: "", newAlias: "",
}; });
// TODO: How should we do direct manipulation for this? if (!this.state.canonicalAlias) {
if (!this.props.canonicalAlias) { this.changeCanonicalAlias(alias);
newState.canonicalAlias = alias;
} }
this.setState(newState);
}).catch((err) => { }).catch((err) => {
console.error(err); console.error(err);
Modal.createTrackedDialog('Error creating alias', '', ErrorDialog, { Modal.createTrackedDialog('Error creating alias', '', ErrorDialog, {
@ -198,11 +222,11 @@ export default class AliasSettings extends React.Component {
const domainAliases = Object.assign({}, this.state.domainToAliases); const domainAliases = Object.assign({}, this.state.domainToAliases);
domainAliases[localDomain] = localAliases; domainAliases[localDomain] = localAliases;
const newState = {domainToAliases: domainAliases}; this.setState({domainToAliases: domainAliases});
if (this.props.canonicalAlias === alias) {
newState.canonicalAlias = null; if (this.state.canonicalAlias === alias) {
this.changeCanonicalAlias(null);
} }
this.setState(newState);
}).catch((err) => { }).catch((err) => {
console.error(err); console.error(err);
Modal.createTrackedDialog('Error removing alias', '', ErrorDialog, { Modal.createTrackedDialog('Error removing alias', '', ErrorDialog, {
@ -216,9 +240,7 @@ export default class AliasSettings extends React.Component {
}; };
onCanonicalAliasChange = (event) => { onCanonicalAliasChange = (event) => {
this.setState({ this.changeCanonicalAlias(event.target.value);
canonicalAlias: event.target.value,
});
}; };
render() { render() {
@ -231,7 +253,8 @@ export default class AliasSettings extends React.Component {
const canonicalValue = this.state.canonicalAlias || ""; const canonicalValue = this.state.canonicalAlias || "";
canonicalAliasSection = ( canonicalAliasSection = (
<Field onChange={this.onCanonicalAliasChange} value={canonicalValue} <Field onChange={this.onCanonicalAliasChange} value={canonicalValue}
element='select' id='canonicalAlias' label={_t('Main address')}> disabled={this.state.updatingCanonicalAlias} element='select'
id='canonicalAlias' label={_t('Main address')}>
<option value="" key="unset">{ _t('not specified') }</option> <option value="" key="unset">{ _t('not specified') }</option>
{ {
Object.keys(this.state.domainToAliases).map((domain, i) => { Object.keys(this.state.domainToAliases).map((domain, i) => {

View file

@ -804,6 +804,8 @@
"Hide Stickers": "Hide Stickers", "Hide Stickers": "Hide Stickers",
"Show Stickers": "Show Stickers", "Show Stickers": "Show Stickers",
"Jump to first unread message.": "Jump to first unread message.", "Jump to first unread message.": "Jump to first unread message.",
"Error updating main address": "Error updating main address",
"There was an error updating the room's main address. It may not be allowed by the server or a temporary failure occurred.": "There was an error updating the room's main address. It may not be allowed by the server or a temporary failure occurred.",
"Error creating alias": "Error creating alias", "Error creating alias": "Error creating alias",
"There was an error creating that alias. It may not be allowed by the server or a temporary failure occurred.": "There was an error creating that alias. It may not be allowed by the server or a temporary failure occurred.", "There was an error creating that alias. It may not be allowed by the server or a temporary failure occurred.": "There was an error creating that alias. It may not be allowed by the server or a temporary failure occurred.",
"Invalid alias format": "Invalid alias format", "Invalid alias format": "Invalid alias format",