// @flow
import * as Creators from '../../actions/teams/creators'
import * as SearchCreators from '../../actions/search/creators'
import * as SearchConstants from '../../constants/search'
import AddPeople from '.'
import {HeaderHoc} from '../../common-adapters'
import {navigateAppend} from '../../actions/route-tree'
import {
  connect,
  compose,
  withHandlers,
  withPropsOnChange,
  withState,
  type TypedState,
} from '../../util/container'

const mapStateToProps = (state: TypedState, {routeProps}) => ({
  isEmpty: SearchConstants.getUserInputItemIds(state, {searchKey: 'addToTeamSearch'}).length === 0,
  name: routeProps.get('teamname'),
})

const mapDispatchToProps = (dispatch: Dispatch, {navigateUp, routeProps}) => ({
  onAddPeople: (role: string) => {
    dispatch(Creators.addPeopleToTeam(routeProps.get('teamname'), role))
    dispatch(navigateUp())
    dispatch(Creators.getTeams())
    dispatch(SearchCreators.clearSearchResults('addToTeamSearch'))
    dispatch(SearchCreators.setUserInputItems('addToTeamSearch', []))
  },
  onClose: () => dispatch(navigateUp()),
  onOpenRolePicker: (role: string, onComplete: string => void) => {
    dispatch(
      navigateAppend([
        {
          props: {
            allowOwner: false,
            onComplete,
            selectedRole: role,
          },
          selected: 'controlledRolePicker',
        },
      ])
    )
  },
})

export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  compose(
    withState('role', 'onRoleChange', 'writer'),
    withPropsOnChange(['onExitSearch'], props => ({
      onCancel: () => props.onClose(),
      title: 'Add people',
    })),
    withHandlers({
      onAddPeople: ({onAddPeople, role}) => () => role && onAddPeople(role),
    }),
    HeaderHoc
  )
)(AddPeople)

@@ -1053,16 +1053,22 @@
 e: () =%3E
+ %7B%0A   
  dispatc
@@ -1084,16 +1084,158 @@
 eUp()),%0A
+    dispatch(SearchCreators.clearSearchResults('addToTeamSearch'))%0A    dispatch(SearchCreators.setUserInputItems('addToTeamSearch', %5B%5D))%0A  %7D,%0A
   onOpen

Clear add to team selections when closing the dialog