import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
import debounce from 'lodash.debounce'
import isObject from 'lodash.isobject'
import {
fetchRemovedAccounts,
expandRemovedAccounts,
removeRemovedAccount,
fetchGroupRemovedAccountsAdminSearch,
} from '../actions/groups'
import { FormattedMessage } from 'react-intl'
import Account from '../components/account'
import Block from '../components/block'
import Input from '../components/input'
import BlockHeading from '../components/block_heading'
import ColumnIndicator from '../components/column_indicator'
import ScrollableList from '../components/scrollable_list'
class GroupRemovedAccounts extends ImmutablePureComponent {
state = {
query: '',
}
componentWillMount() {
const { groupId } = this.props
this.props.onFetchRemovedAccounts(groupId)
}
componentWillReceiveProps(nextProps) {
if (nextProps.groupId !== this.props.groupId) {
this.props.onFetchRemovedAccounts(nextProps.groupId)
}
}
handleLoadMore = debounce(() => {
this.props.onExpandRemovedAccounts(this.props.groupId)
}, 300, { leading: true })
handleOnChange = (query) => {
this.setState({ query })
this.props.onChange(this.props.groupId, query)
}
render() {
const {
listAccountIds,
searchAcountIds,
hasMore,
relationships,
group,
} = this.props
const { query } = this.state
if (!group || !relationships) return
const isAdminOrMod = relationships ? (relationships.get('admin') || relationships.get('moderator')) : false
if (!isAdminOrMod) return
if (!group) return
const accountIds = !!query ? searchAcountIds : listAccountIds
return (
}
>
{
accountIds && accountIds.map((id) => (
this.props.onRemoveRemovedAccount(group.get('id'), id)}
actionTitle='Allow to join'
/>
))
}
)
}
}
const mapStateToProps = (state, { params }) => {
const groupId = isObject(params) ? params['id'] : -1
const group = groupId === -1 ? null : state.getIn(['groups', groupId])
return {
group,
groupId,
listAccountIds: state.getIn(['user_lists', 'group_removed_accounts', groupId, 'items']),
searchAcountIds: state.getIn(['group_lists', 'removed_search_accounts']),
hasMore: !!state.getIn(['user_lists', 'group_removed_accounts', groupId, 'next']),
relationships: state.getIn(['group_relationships', groupId]),
}
}
const mapDispatchToProps = (dispatch) => ({
onChange(groupId, query) {
dispatch(fetchGroupRemovedAccountsAdminSearch(groupId, query))
},
onFetchRemovedAccounts(groupId) {
dispatch(fetchRemovedAccounts(groupId))
},
onExpandRemovedAccounts(groupId) {
dispatch(expandRemovedAccounts(groupId))
},
onRemoveRemovedAccount(groupId, accountId) {
dispatch(removeRemovedAccount(groupId, accountId))
},
})
GroupRemovedAccounts.propTypes = {
groupId: PropTypes.string.isRequired,
accountIds: ImmutablePropTypes.list,
hasMore: PropTypes.bool,
onFetchRemovedAccounts: PropTypes.func.isRequired,
onExpandRemovedAccounts: PropTypes.func.isRequired,
onRemoveRemovedAccount: PropTypes.func.isRequired,
}
export default connect(mapStateToProps, mapDispatchToProps)(GroupRemovedAccounts)