<commit_before>import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Tab, Tabs } from 'react-bootstrap';
import { graphql, compose } from 'react-apollo';
import ConsortiumAbout from './consortium-about';
import ConsortiumPipeline from './consortium-pipeline';
import ConsortiumRuns from './consortium-runs';
import { updateUserPerms } from '../../state/ducks/auth';
import { saveAssociatedConsortia } from '../../state/ducks/collections';
import {
  consortiaMembershipProp,
  getAllAndSubProp,
  getSelectAndSubProp,
  saveDocumentProp,
  userRolesProp,
} from '../../state/graphql/props';
import {
  ADD_USER_ROLE_MUTATION,
  CONSORTIUM_CHANGED_SUBSCRIPTION,
  FETCH_ALL_USERS_QUERY,
  FETCH_CONSORTIUM_QUERY,
  JOIN_CONSORTIUM_MUTATION,
  LEAVE_CONSORTIUM_MUTATION,
  REMOVE_USER_ROLE_MUTATION,
  SAVE_CONSORTIUM_MUTATION,
  USER_CHANGED_SUBSCRIPTION,
} from '../../state/graphql/functions';
import { notifySuccess } from '../../state/ducks/notifyAndLog';

const styles = {
  tab: {
    marginTop: 10,
  },
};

class ConsortiumTabs extends Component {
  constructor(props) {
    super(props);

    const consortium = {
      name: '',
      description: '',
      members: [],
      owners: [],
      activePipelineId: '',
      activeComputationInputs: [],
      tags: [],
    };

    this.state = {
      consortium,
      unsubscribeConsortia: null,
      unsubscribeUsers: null,
      key: 1
    };;

    this.getConsortiumRuns = this.getConsortiumRuns.bind(this);
    this.addMemberToConsortium = this.addMemberToConsortium.bind(this);
    this.removeMemberFromConsortium = this.removeMemberFromConsortium.bind(this);
    this.saveConsortium = this.saveConsortium.bind(this);
    this.updateConsortium = this.updateConsortium.bind(this);
    this.handleSelect = this.handleSelect.bind(this);
  }

  handleSelect(key) {
    this.setState({ key });
  }

  componentWillReceiveProps(nextProps) {

    let newKey = parseInt(this.props.params.tabId);
    if(this.props.params.tabId && newKey !== this.state.key){
      this.handleSelect(this.props.params.tabId);
      this.setState({key: newKey});
    }

    if (this.state.consortium.id && !this.state.unsubscribeConsortia) {
      this.setState({
        unsubscribeConsortia: this.props.subscribeToConsortia(this.state.consortium.id),
      });
    }

    if (nextProps.users && !this.state.unsubscribeUsers) {
      this.setState({ unsubscribeUsers: this.props.subscribeToUsers(null) });
    }

    if (nextProps.activeConsortium) {
      const { activeConsortium: { __typename, ...other } } = nextProps;
      const consortiumUsers = [];
      if (this.props.router.routes[3].path !== 'new') {
        nextProps.activeConsortium.owners.forEach(user => consortiumUsers
          .push({ id: user, owner: true, member: true }));
        nextProps.activeConsortium.members
          .filter(user => consortiumUsers.findIndex(consUser => consUser.id === user) === -1)
          .forEach(user => consortiumUsers.push({ id: user, member: true }));
        this.setState({ consortium: { ...other }, consortiumUsers });
      }
    }
  }

  componentWillUnmount() {
    if (this.state.unsubscribeConsortia) {
      this.state.unsubscribeConsortia();
    }

    this.state.unsubscribeUsers();
  }

  addMemberToConsortium(userId) {
    this.props.joinConsortium(this.state.consortium.id, userId);
  }

  getConsortiumRuns() {
    return (
      this.props.runs.filter(run => run.consortiumId === this.state.consortium.id)
    );
  }

  removeMemberFromConsortium(user) {
    return () => {
      this.props.leaveConsortium(this.state.consortium.id, user.id);
      //These are not needed since the consortia permissions are handled in the resolver
      //this.props.removeUserRole(user.id, 'consortia', this.state.consortium.id, 'member');
      //this.props.removeUserRole(user.id, 'consortia', this.state.consortium.id, 'owner');
    };
  }

  saveConsortium(e) {
    e.preventDefault();

    /* This is creating a duplicate consortia owner. Why is this here?
    //
    if (this.state.consortium.owners.indexOf(this.props.auth.user.id) === -1) {
      this.setState(prevState => ({
        owners: prevState.consortium.owners.push(this.props.auth.user.id),
      }));
    }
    //
    */

    this.props.saveConsortium(this.state.consortium)
    .then(({ data: { saveConsortium: { __typename, ...other } } }) => {
      this.props.addUserRole(this.props.auth.user.id, 'consortia', other.id, 'owner');
      let unsubscribeConsortia = this.state.unsubscribeConsortia;

      this.props.saveAssociatedConsortia({ ...other });

      if (!unsubscribeConsortia) {
        unsubscribeConsortia = this.props.subscribeToConsortia(other.id);
      }

      // Update consortiumUsers to display the owner user in users list
      let consortiumUsers = [];
      consortiumUsers.push({ id: this.props.auth.user.id, owner: true, member: true });

      this.setState({
        consortium: { ...other },
        unsubscribeConsortia,
        consortiumUsers
      });

      this.props.notifySuccess({
        message: 'Consortium Saved',
        autoDismiss: 5,
        action: {
          label: 'View Consortia List',
          callback: () => {
            this.props.router.push('/dashboard/consortia/');
          },
        },
      });

    })<commit_msg>update consortiumUsers after a consortium is saved<commit_after>import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Tab, Tabs } from 'react-bootstrap';
import { graphql, compose } from 'react-apollo';
import ConsortiumAbout from './consortium-about';
import ConsortiumPipeline from './consortium-pipeline';
import ConsortiumRuns from './consortium-runs';
import { updateUserPerms } from '../../state/ducks/auth';
import { saveAssociatedConsortia } from '../../state/ducks/collections';
import {
  consortiaMembershipProp,
  getAllAndSubProp,
  getSelectAndSubProp,
  saveDocumentProp,
  userRolesProp,
} from '../../state/graphql/props';
import {
  ADD_USER_ROLE_MUTATION,
  CONSORTIUM_CHANGED_SUBSCRIPTION,
  FETCH_ALL_USERS_QUERY,
  FETCH_CONSORTIUM_QUERY,
  JOIN_CONSORTIUM_MUTATION,
  LEAVE_CONSORTIUM_MUTATION,
  REMOVE_USER_ROLE_MUTATION,
  SAVE_CONSORTIUM_MUTATION,
  USER_CHANGED_SUBSCRIPTION,
} from '../../state/graphql/functions';
import { notifySuccess } from '../../state/ducks/notifyAndLog';

const styles = {
  tab: {
    marginTop: 10,
  },
};

class ConsortiumTabs extends Component {
  constructor(props) {
    super(props);

    const consortium = {
      name: '',
      description: '',
      members: [],
      owners: [],
      activePipelineId: '',
      activeComputationInputs: [],
      tags: [],
    };

    this.state = {
      consortium,
      unsubscribeConsortia: null,
      unsubscribeUsers: null,
      key: 1
    };;

    this.getConsortiumRuns = this.getConsortiumRuns.bind(this);
    this.addMemberToConsortium = this.addMemberToConsortium.bind(this);
    this.removeMemberFromConsortium = this.removeMemberFromConsortium.bind(this);
    this.saveConsortium = this.saveConsortium.bind(this);
    this.updateConsortium = this.updateConsortium.bind(this);
    this.handleSelect = this.handleSelect.bind(this);
  }

  handleSelect(key) {
    this.setState({ key });
  }

  componentWillReceiveProps(nextProps) {

    let newKey = parseInt(this.props.params.tabId);
    if(this.props.params.tabId && newKey !== this.state.key){
      this.handleSelect(this.props.params.tabId);
      this.setState({key: newKey});
    }

    if (this.state.consortium.id && !this.state.unsubscribeConsortia) {
      this.setState({
        unsubscribeConsortia: this.props.subscribeToConsortia(this.state.consortium.id),
      });
    }

    if (nextProps.users && !this.state.unsubscribeUsers) {
      this.setState({ unsubscribeUsers: this.props.subscribeToUsers(null) });
    }

    if (nextProps.activeConsortium) {
      const { activeConsortium: { __typename, ...other } } = nextProps;
      const consortiumUsers = [];
      if (this.props.routes[3].path !== 'new' || (this.props.routes[3].path === 'new' && this.state.consortium.id) ) {
        nextProps.activeConsortium.owners.forEach(user => consortiumUsers
          .push({ id: user, owner: true, member: true }));
        nextProps.activeConsortium.members
          .filter(user => consortiumUsers.findIndex(consUser => consUser.id === user) === -1)
          .forEach(user => consortiumUsers.push({ id: user, member: true }));
        this.setState({ consortium: { ...other }, consortiumUsers });
      }
    }
  }

  componentWillUnmount() {
    if (this.state.unsubscribeConsortia) {
      this.state.unsubscribeConsortia();
    }

    this.state.unsubscribeUsers();
  }

  addMemberToConsortium(userId) {
    this.props.joinConsortium(this.state.consortium.id, userId);
  }

  getConsortiumRuns() {
    return (
      this.props.runs.filter(run => run.consortiumId === this.state.consortium.id)
    );
  }

  removeMemberFromConsortium(user) {
    return () => {
      this.props.leaveConsortium(this.state.consortium.id, user.id);
      //These are not needed since the consortia permissions are handled in the resolver
      //this.props.removeUserRole(user.id, 'consortia', this.state.consortium.id, 'member');
      //this.props.removeUserRole(user.id, 'consortia', this.state.consortium.id, 'owner');
    };
  }

  saveConsortium(e) {
    e.preventDefault();

    /* This is creating a duplicate consortia owner. Why is this here?
    //
    if (this.state.consortium.owners.indexOf(this.props.auth.user.id) === -1) {
      this.setState(prevState => ({
        owners: prevState.consortium.owners.push(this.props.auth.user.id),
      }));
    }
    //
    */

    this.props.saveConsortium(this.state.consortium)
    .then(({ data: { saveConsortium: { __typename, ...other } } }) => {
      this.props.addUserRole(this.props.auth.user.id, 'consortia', other.id, 'owner');
      let unsubscribeConsortia = this.state.unsubscribeConsortia;

      this.props.saveAssociatedConsortia({ ...other });

      if (!unsubscribeConsortia) {
        unsubscribeConsortia = this.props.subscribeToConsortia(other.id);
      }


      this.setState({
        consortium: { ...other },
        unsubscribeConsortia
      });

      this.props.notifySuccess({
        message: 'Consortium Saved',
        autoDismiss: 5,
        action: {
          label: 'View Consortia List',
          callback: () => {
            this.props.router.push('/dashboard/consortia/');
          },
        },
      });

    })
{'fraction_non_alphanumeric': 0.13970104352731033, 'fraction_numerical': 0.0014101720409890007, 'mean_word_length': 3.0789877300613497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3635139', 'n_tokens_mistral': 3502, 'n_tokens_neox': 3347, 'n_words': 553}