| |
@@ -0,0 +1,173 @@
|
| |
+ import React from 'react';
|
| |
+ import PropTypes from 'prop-types';
|
| |
+ import { connect } from 'react-redux';
|
| |
+ import {
|
| |
+ associateUser,
|
| |
+ dissociateUser
|
| |
+ } from "../core/actions/hub";
|
| |
+ import StateButton from "./StateButton";
|
| |
+
|
| |
+
|
| |
+
|
| |
+ class HubMembership extends React.Component {
|
| |
+
|
| |
+ constructor(props) {
|
| |
+ super(props);
|
| |
+ this.onSubscribe = this.onSubscribe.bind(this);
|
| |
+ this.onUnsubscribe = this.onUnsubscribe.bind(this);
|
| |
+ this.onJoin = this.onJoin.bind(this);
|
| |
+ this.onLeave = this.onLeave.bind(this);
|
| |
+ this.onGiveUpAdmin = this.onGiveUpAdmin.bind(this);
|
| |
+ this.userHasRole = this.userHasRole.bind(this);
|
| |
+ }
|
| |
+
|
| |
+ onSubscribe() {
|
| |
+ this.props.dispatch(associateUser("subscriber"));
|
| |
+ }
|
| |
+
|
| |
+ onUnsubscribe() {
|
| |
+ this.props.dispatch(dissociateUser("subscriber"));
|
| |
+ }
|
| |
+
|
| |
+ onJoin() {
|
| |
+ this.props.dispatch(associateUser("member"));
|
| |
+ }
|
| |
+
|
| |
+ onLeave() {
|
| |
+ this.props.dispatch(dissociateUser("member"));
|
| |
+ }
|
| |
+
|
| |
+ onGiveUpAdmin() {
|
| |
+ this.props.dispatch(dissociateUser("owner"));
|
| |
+ }
|
| |
+
|
| |
+ userHasRole(role) {
|
| |
+ if (!this.props.currentUser.logged_in) {
|
| |
+ return false;
|
| |
+ }
|
| |
+ const users = this.props.hub.users[role].map((user) => (user.username));
|
| |
+ return (users.indexOf(this.props.currentUser.nickname) !== -1);
|
| |
+ }
|
| |
+
|
| |
+ render() {
|
| |
+ if (!this.props.hub.name) {
|
| |
+ return null;
|
| |
+ }
|
| |
+ if (this.props.hub.user_hub && this.props.currentUser.perms.config_hub) {
|
| |
+ return null; // The user's own hub.
|
| |
+ }
|
| |
+ let commonProps = {disabled: false, title: ""}
|
| |
+ if (!this.props.currentUser.logged_in) {
|
| |
+ commonProps.disabled = true;
|
| |
+ commonProps.title = "You must be logged in.";
|
| |
+ } else if (this.props.hub.isLoading) {
|
| |
+ commonProps.disabled = true;
|
| |
+ commonProps.title = "loading...";
|
| |
+ }
|
| |
+ let secondButton = null;
|
| |
+ if (!this.props.hub.user_hub) {
|
| |
+ if (this.userHasRole("owner")) {
|
| |
+ secondButton = (
|
| |
+ <HubGiveUpAdminButton
|
| |
+ onGiveUpAdmin={this.onGiveUpAdmin}
|
| |
+ {...commonProps}
|
| |
+ />
|
| |
+ );
|
| |
+ } else {
|
| |
+ secondButton = (
|
| |
+ <HubJoinButton
|
| |
+ isMember={this.userHasRole("member")}
|
| |
+ onJoin={this.onJoin}
|
| |
+ onLeave={this.onLeave}
|
| |
+ {...commonProps}
|
| |
+ />
|
| |
+ );
|
| |
+ }
|
| |
+ }
|
| |
+ return (
|
| |
+ <div className="HubMembership">
|
| |
+ <HubSubscribeButton
|
| |
+ isSubscriber={this.userHasRole("subscriber")}
|
| |
+ onSubscribe={this.onSubscribe}
|
| |
+ onUnsubscribe={this.onUnsubscribe}
|
| |
+ {...commonProps}
|
| |
+ />
|
| |
+ {secondButton}
|
| |
+ </div>
|
| |
+ );
|
| |
+ }
|
| |
+ }
|
| |
+ HubMembership.propTypes = {
|
| |
+ hub: PropTypes.object.isRequired,
|
| |
+ currentUser: PropTypes.object,
|
| |
+ }
|
| |
+
|
| |
+
|
| |
+
|
| |
+ const mapStateToProps = (state) => {
|
| |
+ return {
|
| |
+ hub: state.entities.hub,
|
| |
+ currentUser: state.currentUser,
|
| |
+ }
|
| |
+ };
|
| |
+
|
| |
+ export default connect(mapStateToProps)(HubMembership);
|
| |
+
|
| |
+
|
| |
+ class HubSubscribeButton extends React.Component {
|
| |
+ render() {
|
| |
+ return (
|
| |
+ <StateButton
|
| |
+ isOn={this.props.isSubscriber}
|
| |
+ turnOnText="Subscribe"
|
| |
+ turnOffText="Unsubscribe"
|
| |
+ turnedOnText="Subscribed"
|
| |
+ turnedOnIcon="check"
|
| |
+ onTurnOn={this.props.onSubscribe}
|
| |
+ onTurnOff={this.props.onUnsubscribe}
|
| |
+ className="btn-sm mr-2"
|
| |
+ disabled={this.props.disabled}
|
| |
+ title={this.props.title}
|
| |
+ />
|
| |
+ );
|
| |
+ }
|
| |
+ }
|
| |
+
|
| |
+
|
| |
+ class HubJoinButton extends React.Component {
|
| |
+ render() {
|
| |
+ return (
|
| |
+ <StateButton
|
| |
+ isOn={this.props.isMember}
|
| |
+ turnOnText="Join this hub"
|
| |
+ turnOffText="Leave this hub"
|
| |
+ turnedOnText="Member"
|
| |
+ turnedOnIcon="user"
|
| |
+ turnedOffIcon="user"
|
| |
+ onTurnOn={this.props.onJoin}
|
| |
+ onTurnOff={this.props.onLeave}
|
| |
+ className="btn-sm"
|
| |
+ disabled={this.props.disabled}
|
| |
+ title={this.props.title}
|
| |
+ />
|
| |
+ );
|
| |
+ }
|
| |
+ }
|
| |
+
|
| |
+
|
| |
+ class HubGiveUpAdminButton extends React.Component {
|
| |
+ render() {
|
| |
+ return (
|
| |
+ <StateButton
|
| |
+ isOn={true}
|
| |
+ turnOffText="Give up admin"
|
| |
+ turnedOnText="Admin"
|
| |
+ turnedOnIcon="key"
|
| |
+ onTurnOff={this.props.onGiveUpAdmin}
|
| |
+ className="btn-sm"
|
| |
+ disabled={this.props.disabled}
|
| |
+ title={this.props.title}
|
| |
+ />
|
| |
+ );
|
| |
+ }
|
| |
+ }
|
| |
For a second I thought this was Python and I was like "whoah I had no idea you could import this way!"