<commit_before>// @flow
import {connect} from 'react-redux'
import * as I from 'immutable'
import {compose, withState} from 'recompose'
import RolePicker from '.'
import * as Creators from '../../actions/teams/creators'
import * as Constants from '../../constants/teams'

import type {TypedState} from '../../constants/reducer'

type StateProps = {
  _memberInfo: I.Set<Constants.MemberInfo>,
  you: ?string,
  username: string,
  teamname: string,
}

const mapStateToProps = (state: TypedState, {routeProps}): StateProps => ({
  _memberInfo: state.entities.getIn(['teams', 'teamNameToMembers', routeProps.get('teamname')], I.Set()),
  teamname: routeProps.get('teamname'),
  username: routeProps.get('username'),
  you: state.config.username,
})

// TODO add stuff for edit membership options
type DispatchProps = {
  _onAddMember: (
    teamname: Constants.Teamname,
    username: string,
    role: Constants.TeamRoleType,
    sendNotification: boolean
  ) => void,
  _onEditMember: (teamname: Constants.Teamname, username: string, role: Constants.TeamRoleType) => void,
  onBack: () => void,
}

const mapDispatchToProps = (dispatch: Dispatch, {navigateUp}): DispatchProps => ({
  _onAddMember: (teamname, username, role, sendNotification) =>
    dispatch(Creators.addToTeam(teamname, '', username, role, sendNotification)),
  _onEditMember: (teamname, username, role) => dispatch(Creators.editMembership(teamname, username, role)),
  onBack: () => dispatch(navigateUp()),
})

const mergeProps = (stateProps: StateProps, dispatchProps: DispatchProps, ownProps) => {
  const yourInfo = stateProps._memberInfo.find(member => member.username === stateProps.you)
  const theirInfo = stateProps._memberInfo.find(member => member.username === stateProps.username)
  const onComplete = (role: Constants.TeamRoleType, sendNotification?: boolean) => {
    if (theirInfo) {
      dispatchProps._onEditMember(stateProps.teamname, stateProps.username, role)
    } else {
      dispatchProps._onAddMember(stateProps.teamname, stateProps.username, role, sendNotification || false)
    }
    dispatchProps.onBack()
  }
  const showSendNotification = !!theirInfo
  return {
    ...stateProps,
    ...dispatchProps,
    ...ownProps,
    allowOwner: yourInfo && yourInfo.type === 'owners',
    onComplete,
    showSendNotification,
  }
}

export default compose(
  withState('selectedRole', 'setSelectedRole', null),
  withState('sendNotification', 'setSendNotification', false),
  withState('confirm', 'setConfirm', false),
  connect(mapStateToProps, mapDispatchToProps, mergeProps)
)(RolePicker)<commit_msg>fix showSendNotification check<commit_after>// @flow
import {connect} from 'react-redux'
import * as I from 'immutable'
import {compose, withState} from 'recompose'
import RolePicker from '.'
import * as Creators from '../../actions/teams/creators'
import * as Constants from '../../constants/teams'

import type {TypedState} from '../../constants/reducer'

type StateProps = {
  _memberInfo: I.Set<Constants.MemberInfo>,
  you: ?string,
  username: string,
  teamname: string,
}

const mapStateToProps = (state: TypedState, {routeProps}): StateProps => ({
  _memberInfo: state.entities.getIn(['teams', 'teamNameToMembers', routeProps.get('teamname')], I.Set()),
  teamname: routeProps.get('teamname'),
  username: routeProps.get('username'),
  you: state.config.username,
})

// TODO add stuff for edit membership options
type DispatchProps = {
  _onAddMember: (
    teamname: Constants.Teamname,
    username: string,
    role: Constants.TeamRoleType,
    sendNotification: boolean
  ) => void,
  _onEditMember: (teamname: Constants.Teamname, username: string, role: Constants.TeamRoleType) => void,
  onBack: () => void,
}

const mapDispatchToProps = (dispatch: Dispatch, {navigateUp}): DispatchProps => ({
  _onAddMember: (teamname, username, role, sendNotification) =>
    dispatch(Creators.addToTeam(teamname, '', username, role, sendNotification)),
  _onEditMember: (teamname, username, role) => dispatch(Creators.editMembership(teamname, username, role)),
  onBack: () => dispatch(navigateUp()),
})

const mergeProps = (stateProps: StateProps, dispatchProps: DispatchProps, ownProps) => {
  const yourInfo = stateProps._memberInfo.find(member => member.username === stateProps.you)
  const theirInfo = stateProps._memberInfo.find(member => member.username === stateProps.username)
  const onComplete = (role: Constants.TeamRoleType, sendNotification?: boolean) => {
    if (theirInfo) {
      dispatchProps._onEditMember(stateProps.teamname, stateProps.username, role)
    } else {
      dispatchProps._onAddMember(stateProps.teamname, stateProps.username, role, sendNotification || false)
    }
    dispatchProps.onBack()
  }
  const showSendNotification = !theirInfo
  return {
    ...stateProps,
    ...dispatchProps,
    ...ownProps,
    allowOwner: yourInfo && yourInfo.type === 'owners',
    onComplete,
    showSendNotification,
  }
}

export default compose(
  withState('selectedRole', 'setSelectedRole', null),
  withState('sendNotification', 'setSendNotification', false),
  withState('confirm', 'setConfirm', false),
  connect(mapStateToProps, mapDispatchToProps, mergeProps)
)(RolePicker)
{'fraction_non_alphanumeric': 0.1382160707420223, 'fraction_numerical': 0.0, 'mean_word_length': 5.0011534025374855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '6423080', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1557, 'n_words': 357}