Convert RelatedGroupSettings to a class component

pull/21833/head
Travis Ralston 2019-02-21 16:45:57 -07:00
parent 10daa35263
commit 96eeab23af
2 changed files with 36 additions and 39 deletions

View File

@ -25,39 +25,33 @@ import ErrorDialog from "../dialogs/ErrorDialog";
const GROUP_ID_REGEX = /\+\S+:\S+/; const GROUP_ID_REGEX = /\+\S+:\S+/;
module.exports = React.createClass({ export default class RelatedGroupSettings extends React.Component {
displayName: 'RelatedGroupSettings', static propTypes = {
propTypes: {
roomId: PropTypes.string.isRequired, roomId: PropTypes.string.isRequired,
canSetRelatedGroups: PropTypes.bool.isRequired, canSetRelatedGroups: PropTypes.bool.isRequired,
relatedGroupsEvent: PropTypes.instanceOf(MatrixEvent), relatedGroupsEvent: PropTypes.instanceOf(MatrixEvent),
}, };
contextTypes: { static contextTypes = {
matrixClient: PropTypes.instanceOf(MatrixClient), matrixClient: PropTypes.instanceOf(MatrixClient),
}, };
getDefaultProps: function() { static defaultProps = {
return { canSetRelatedGroups: false,
canSetRelatedGroups: false, };
};
},
getInitialState: function() { constructor(props) {
return { super(props);
newGroupsList: this.getInitialGroupList(),
this.state = {
newGroupId: "", newGroupId: "",
newGroupsList: props.relatedGroupsEvent ? (props.relatedGroupsEvent.getContent().groups || []) : [],
}; };
}, }
getInitialGroupList: function() { updateGroups(newGroupsList) {
return this.props.relatedGroupsEvent ? (this.props.relatedGroupsEvent.getContent().groups || []) : [];
},
updateGroups: function() {
this.context.matrixClient.sendStateEvent(this.props.roomId, 'm.room.related_groups', { this.context.matrixClient.sendStateEvent(this.props.roomId, 'm.room.related_groups', {
groups: this.state.newGroupsList, groups: newGroupsList,
}, '').catch((err) => { }, '').catch((err) => {
console.error(err); console.error(err);
Modal.createTrackedDialog('Error updating flair', '', ErrorDialog, { Modal.createTrackedDialog('Error updating flair', '', ErrorDialog, {
@ -68,9 +62,9 @@ module.exports = React.createClass({
), ),
}); });
}) })
}, }
validateGroupId: function(groupId) { validateGroupId(groupId) {
if (!GROUP_ID_REGEX.test(groupId)) { if (!GROUP_ID_REGEX.test(groupId)) {
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
Modal.createTrackedDialog('Invalid related community ID', '', ErrorDialog, { Modal.createTrackedDialog('Invalid related community ID', '', ErrorDialog, {
@ -80,31 +74,32 @@ module.exports = React.createClass({
return false; return false;
} }
return true; return true;
}, }
onNewGroupChanged: function(newGroupId) { onNewGroupChanged = (newGroupId) => {
this.setState({ newGroupId }); this.setState({ newGroupId });
}, };
onGroupAdded: function(groupId) { onGroupAdded = (groupId) => {
if (groupId.length === 0 || !this.validateGroupId(groupId)) { if (groupId.length === 0 || !this.validateGroupId(groupId)) {
return; return;
} }
const newGroupsList = [...this.state.newGroupsList, groupId];
this.setState({ this.setState({
newGroupsList: this.state.newGroupsList.concat([groupId]), newGroupsList: newGroupsList,
newGroupId: '', newGroupId: '',
}); });
this.updateGroups(); this.updateGroups(newGroupsList);
}, };
onGroupDeleted: function(index) { onGroupDeleted = (index) => {
const newGroupsList = this.state.newGroupsList.slice(); const group = this.state.newGroupsList[index];
newGroupsList.splice(index, 1); const newGroupsList = this.state.newGroupsList.filter((g) => g !== group);
this.setState({ newGroupsList }); this.setState({ newGroupsList });
this.updateGroups(); this.updateGroups(newGroupsList);
}, };
render: function() { render() {
const localDomain = this.context.matrixClient.getDomain(); const localDomain = this.context.matrixClient.getDomain();
const EditableItemList = sdk.getComponent('elements.EditableItemList'); const EditableItemList = sdk.getComponent('elements.EditableItemList');
return <div> return <div>
@ -123,5 +118,5 @@ module.exports = React.createClass({
)} )}
/> />
</div>; </div>;
}, }
}); };

View File

@ -818,6 +818,8 @@
"Local addresses for this room:": "Local addresses for this room:", "Local addresses for this room:": "Local addresses for this room:",
"This room has no local addresses": "This room has no local addresses", "This room has no local addresses": "This room has no local addresses",
"New address (e.g. #foo:%(localDomain)s)": "New address (e.g. #foo:%(localDomain)s)", "New address (e.g. #foo:%(localDomain)s)": "New address (e.g. #foo:%(localDomain)s)",
"Error updating flair": "Error updating flair",
"There was an error updating the flair for this room. The server may not allow it or a temporary error occurred.": "There was an error updating the flair for this room. The server may not allow it or a temporary error occurred.",
"Invalid community ID": "Invalid community ID", "Invalid community ID": "Invalid community ID",
"'%(groupId)s' is not a valid community ID": "'%(groupId)s' is not a valid community ID", "'%(groupId)s' is not a valid community ID": "'%(groupId)s' is not a valid community ID",
"Showing flair for these communities:": "Showing flair for these communities:", "Showing flair for these communities:": "Showing flair for these communities:",