mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-16 07:31:59 +03:00
3f99332f4b
This fixes a common React warning we see. Most of these components should be using constructors instead, however componentDidMount is just as good (and doesn't require converting most of these). Conversion to classes will be done in a later stage of React warning fixes. For https://github.com/vector-im/riot-web/issues/12877
80 lines
2.6 KiB
JavaScript
80 lines
2.6 KiB
JavaScript
/*
|
|
Copyright 2017 New Vector 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';
|
|
import PropTypes from 'prop-types';
|
|
import createReactClass from 'create-react-class';
|
|
import * as sdk from '../../../index';
|
|
import GroupStore from '../../../stores/GroupStore';
|
|
import ToggleSwitch from "../elements/ToggleSwitch";
|
|
|
|
export default createReactClass({
|
|
displayName: 'GroupPublicityToggle',
|
|
|
|
propTypes: {
|
|
groupId: PropTypes.string.isRequired,
|
|
},
|
|
|
|
getInitialState() {
|
|
return {
|
|
busy: false,
|
|
ready: false,
|
|
isGroupPublicised: false, // assume false as <ToggleSwitch /> expects a boolean
|
|
};
|
|
},
|
|
|
|
componentDidMount: function() {
|
|
this._initGroupStore(this.props.groupId);
|
|
},
|
|
|
|
_initGroupStore: function(groupId) {
|
|
this._groupStoreToken = GroupStore.registerListener(groupId, () => {
|
|
this.setState({
|
|
isGroupPublicised: Boolean(GroupStore.getGroupPublicity(groupId)),
|
|
ready: GroupStore.isStateReady(groupId, GroupStore.STATE_KEY.Summary),
|
|
});
|
|
});
|
|
},
|
|
|
|
componentWillUnmount() {
|
|
if (this._groupStoreToken) this._groupStoreToken.unregister();
|
|
},
|
|
|
|
_onPublicityToggle: function() {
|
|
this.setState({
|
|
busy: true,
|
|
// Optimistic early update
|
|
isGroupPublicised: !this.state.isGroupPublicised,
|
|
});
|
|
GroupStore.setGroupPublicity(this.props.groupId, !this.state.isGroupPublicised).then(() => {
|
|
this.setState({
|
|
busy: false,
|
|
});
|
|
});
|
|
},
|
|
|
|
render() {
|
|
const GroupTile = sdk.getComponent('groups.GroupTile');
|
|
return <div className="mx_GroupPublicity_toggle">
|
|
<GroupTile groupId={this.props.groupId} showDescription={false}
|
|
avatarHeight={40} draggable={false}
|
|
/>
|
|
<ToggleSwitch checked={this.state.isGroupPublicised}
|
|
disabled={!this.state.ready || this.state.busy}
|
|
onChange={this._onPublicityToggle} />
|
|
</div>;
|
|
},
|
|
});
|