2017-11-21 14:50:41 +03:00
|
|
|
/*
|
|
|
|
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 sdk from '../../../index';
|
|
|
|
import GroupStore from '../../../stores/GroupStore';
|
2019-01-26 01:35:32 +03:00
|
|
|
import ToggleSwitch from "../elements/ToggleSwitch";
|
2017-11-21 14:50:41 +03:00
|
|
|
|
|
|
|
export default React.createClass({
|
|
|
|
displayName: 'GroupPublicityToggle',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
groupId: PropTypes.string.isRequired,
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
busy: false,
|
|
|
|
ready: false,
|
|
|
|
isGroupPublicised: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
this._initGroupStore(this.props.groupId);
|
|
|
|
},
|
|
|
|
|
|
|
|
_initGroupStore: function(groupId) {
|
2018-05-01 13:50:14 +03:00
|
|
|
this._groupStoreToken = GroupStore.registerListener(groupId, () => {
|
2017-11-21 14:50:41 +03:00
|
|
|
this.setState({
|
2018-05-01 13:18:45 +03:00
|
|
|
isGroupPublicised: GroupStore.getGroupPublicity(groupId),
|
|
|
|
ready: GroupStore.isStateReady(groupId, GroupStore.STATE_KEY.Summary),
|
2017-11-21 14:50:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-01 13:50:14 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this._groupStoreToken) this._groupStoreToken.unregister();
|
|
|
|
},
|
|
|
|
|
2019-01-26 01:35:32 +03:00
|
|
|
_onPublicityToggle: function() {
|
2017-11-21 14:50:41 +03:00
|
|
|
this.setState({
|
|
|
|
busy: true,
|
|
|
|
// Optimistic early update
|
|
|
|
isGroupPublicised: !this.state.isGroupPublicised,
|
|
|
|
});
|
2018-05-01 13:18:45 +03:00
|
|
|
GroupStore.setGroupPublicity(this.props.groupId, !this.state.isGroupPublicised).then(() => {
|
2017-11-21 14:50:41 +03:00
|
|
|
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} />
|
2019-01-26 01:35:32 +03:00
|
|
|
<ToggleSwitch checked={this.state.isGroupPublicised}
|
|
|
|
disabled={!this.state.ready || this.state.busy}
|
|
|
|
onChange={this._onPublicityToggle} />
|
2017-11-21 14:50:41 +03:00
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
});
|