<commit_before>import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { hilarious } from 'app/render/utils/hilarious-loading-messages';
import ConsortiumSingle from './consortium-single';
import { fetchComputations } from 'app/render/state/ducks/computations';
import { unlistenToConsortia, listenToConsortia } from 'app/render/state/ducks/bg-services';
import { saveConsortium, setConsortium } from 'app/render/state/ducks/consortium';
import { fetch as fetchRemoteResults, setRemoteResults } from 'app/render/state/ducks/remote-results';  // eslint-disable-line
import without from 'lodash/without';
import noop from 'lodash/noop';
import app from 'ampersand-app';
import { addConsortiumComputationListener } from 'app/render/state/ducks/bg-services';  // eslint-disable-line

class ConsortiumSingleController extends Component {

  componentWillMount() {
    const { dispatch, params: { _id: consortiumId }, consortia } = this.props;
    this.addUser = this.addUser.bind(this);
    this.removeUser = this.removeUser.bind(this);
    const consortium = consortia.find(tium => tium._id === consortiumId);
    if (!consortium) { throw new ReferenceError(`consortium ${consortiumId} not found`); }
    dispatch(setConsortium(consortium));
    dispatch(fetchRemoteResults(consortium._id));
    dispatch(fetchComputations()); // @TODO never set. let background service keep up to date
    this.updateComputation = this.updateComputation.bind(this);
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch(setConsortium(null));
    dispatch(setRemoteResults(null));
  }

  addUser(username) {
    const { dispatch, consortium } = this.props;
    consortium.users.push(username);
    dispatch(saveConsortium(consortium))
    .then((tium) => {
      // TODO: Figure out a better way to initiate this background service
      listenToConsortia(tium)
      .then(() => {
        addConsortiumComputationListener(tium);
      });
      app.logger.info(`now listening to events on consortium ${tium.label}`);
    });
  }

  updateComputation(evt) {
    const { dispatch, consortium } = this.props;
    const computationId = evt.target.value;
    consortium.activeComputationId = computationId;

    dispatch(saveConsortium(consortium));
  }

  removeUser(username) {
    const { dispatch, consortium } = this.props;
    consortium.users = without(consortium.users, username);
    dispatch(saveConsortium(consortium))
    .then(() => {
      // TODO: Figure out a better way to initiate this background service
      unlistenToConsortia(consortium._id);

      app.notify('success', `${username} removed`);
    })
    .catch((err) => app.notify('error', err.message));
  }

  render() {
    const { auth, computations, consortium, loading, remoteResults } = this.props;
    if (!consortium || !remoteResults || !computations) { return (<span>{hilarious.random()}</span>); }<commit_msg>Remove bad promise chaining.<commit_after>import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { hilarious } from 'app/render/utils/hilarious-loading-messages';
import ConsortiumSingle from './consortium-single';
import { fetchComputations } from 'app/render/state/ducks/computations';
import { unlistenToConsortia, listenToConsortia } from 'app/render/state/ducks/bg-services';
import { saveConsortium, setConsortium } from 'app/render/state/ducks/consortium';
import { fetch as fetchRemoteResults, setRemoteResults } from 'app/render/state/ducks/remote-results';  // eslint-disable-line
import without from 'lodash/without';
import noop from 'lodash/noop';
import app from 'ampersand-app';
import { addConsortiumComputationListener } from 'app/render/state/ducks/bg-services';  // eslint-disable-line

class ConsortiumSingleController extends Component {

  componentWillMount() {
    const { dispatch, params: { _id: consortiumId }, consortia } = this.props;
    this.addUser = this.addUser.bind(this);
    this.removeUser = this.removeUser.bind(this);
    const consortium = consortia.find(tium => tium._id === consortiumId);
    if (!consortium) { throw new ReferenceError(`consortium ${consortiumId} not found`); }
    dispatch(setConsortium(consortium));
    dispatch(fetchRemoteResults(consortium._id));
    dispatch(fetchComputations()); // @TODO never set. let background service keep up to date
    this.updateComputation = this.updateComputation.bind(this);
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch(setConsortium(null));
    dispatch(setRemoteResults(null));
  }

  addUser(username) {
    const { dispatch, consortium } = this.props;
    consortium.users.push(username);
    dispatch(saveConsortium(consortium))
    .then((tium) => {
      // TODO: Figure out a better way to initiate this background service
      listenToConsortia(tium)
      addConsortiumComputationListener(tium);

      app.logger.info(`now listening to events on consortium ${tium.label}`);
    });
  }

  updateComputation(evt) {
    const { dispatch, consortium } = this.props;
    const computationId = evt.target.value;
    consortium.activeComputationId = computationId;

    dispatch(saveConsortium(consortium));
  }

  removeUser(username) {
    const { dispatch, consortium } = this.props;
    consortium.users = without(consortium.users, username);
    dispatch(saveConsortium(consortium))
    .then(() => {
      // TODO: Figure out a better way to initiate this background service
      unlistenToConsortia(consortium._id);

      app.notify('success', `${username} removed`);
    })
    .catch((err) => app.notify('error', err.message));
  }

  render() {
    const { auth, computations, consortium, loading, remoteResults } = this.props;
    if (!consortium || !remoteResults || !computations) { return (<span>{hilarious.random()}</span>); }
{'fraction_non_alphanumeric': 0.12896081771720613, 'fraction_numerical': 0.0, 'mean_word_length': 4.313122171945701, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377832', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1678, 'n_words': 380}