2020-05-07 06:55:24 +01:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
2020-04-08 02:06:59 +01:00
|
|
|
import debounce from 'lodash.debounce'
|
2020-06-11 04:27:03 +01:00
|
|
|
import { FormattedMessage } from 'react-intl'
|
2020-02-29 15:42:47 +00:00
|
|
|
import {
|
|
|
|
fetchMembers,
|
|
|
|
expandMembers,
|
2020-05-07 06:55:24 +01:00
|
|
|
} from '../actions/groups'
|
2020-06-11 04:27:03 +01:00
|
|
|
import { openPopover } from '../actions/popover'
|
2020-05-07 06:55:24 +01:00
|
|
|
import Account from '../components/account'
|
|
|
|
import ScrollableList from '../components/scrollable_list'
|
2020-02-29 15:42:47 +00:00
|
|
|
|
2020-05-07 06:55:24 +01:00
|
|
|
const mapStateToProps = (state, { groupId }) => ({
|
|
|
|
group: state.getIn(['groups', groupId]),
|
|
|
|
relationships: state.getIn(['group_relationships', groupId]),
|
|
|
|
accountIds: state.getIn(['user_lists', 'groups', groupId, 'items']),
|
|
|
|
hasMore: !!state.getIn(['user_lists', 'groups', groupId, 'next']),
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onFetchMembers(groupId) {
|
|
|
|
dispatch(fetchMembers(groupId))
|
|
|
|
},
|
|
|
|
onExpandMembers(groupId) {
|
|
|
|
dispatch(expandMembers(groupId))
|
|
|
|
},
|
2020-06-11 04:27:03 +01:00
|
|
|
onOpenGroupMemberOptions(targetRef, accountId, groupId) {
|
|
|
|
dispatch(openPopover('GROUP_MEMBER_OPTIONS', {
|
|
|
|
targetRef,
|
|
|
|
accountId,
|
|
|
|
groupId,
|
|
|
|
position: 'top',
|
|
|
|
}))
|
|
|
|
},
|
2020-05-07 06:55:24 +01:00
|
|
|
})
|
2020-02-29 15:42:47 +00:00
|
|
|
|
|
|
|
export default
|
2020-05-07 06:55:24 +01:00
|
|
|
@connect(mapStateToProps, mapDispatchToProps)
|
2020-02-29 15:42:47 +00:00
|
|
|
class GroupMembers extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
2020-05-07 06:55:24 +01:00
|
|
|
groupId: PropTypes.string.isRequired,
|
2020-02-29 15:42:47 +00:00
|
|
|
accountIds: ImmutablePropTypes.list,
|
|
|
|
hasMore: PropTypes.bool,
|
2020-05-07 06:55:24 +01:00
|
|
|
onExpandMembers: PropTypes.func.isRequired,
|
|
|
|
onFetchMembers: PropTypes.func.isRequired,
|
2020-06-11 04:27:03 +01:00
|
|
|
onOpenGroupMemberOptions: PropTypes.func.isRequired,
|
2020-05-07 06:55:24 +01:00
|
|
|
}
|
2020-02-29 15:42:47 +00:00
|
|
|
|
|
|
|
componentWillMount() {
|
2020-05-07 06:55:24 +01:00
|
|
|
const { groupId } = this.props
|
2020-02-29 15:42:47 +00:00
|
|
|
|
2020-05-07 06:55:24 +01:00
|
|
|
this.props.onFetchMembers(groupId)
|
2020-02-29 15:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2020-05-07 06:55:24 +01:00
|
|
|
if (nextProps.groupId !== this.props.groupId) {
|
|
|
|
this.props.onFetchMembers(nextProps.groupId)
|
2020-02-29 15:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 04:27:03 +01:00
|
|
|
handleOpenGroupMemberOptions = (e, accountId) => {
|
|
|
|
this.props.onOpenGroupMemberOptions(e.currentTarget, accountId, this.props.groupId)
|
|
|
|
}
|
|
|
|
|
2020-02-29 15:42:47 +00:00
|
|
|
handleLoadMore = debounce(() => {
|
2020-05-07 06:55:24 +01:00
|
|
|
this.props.onExpandMembers(this.props.groupId)
|
|
|
|
}, 300, { leading: true })
|
2020-02-29 15:42:47 +00:00
|
|
|
|
|
|
|
render() {
|
2020-05-07 06:55:24 +01:00
|
|
|
const {
|
|
|
|
accountIds,
|
|
|
|
hasMore,
|
|
|
|
group,
|
|
|
|
relationships,
|
|
|
|
} = this.props
|
2020-02-29 15:42:47 +00:00
|
|
|
|
2020-06-11 04:27:03 +01:00
|
|
|
const isAdmin = relationships.get('admin')
|
|
|
|
|
2020-02-29 15:42:47 +00:00
|
|
|
return (
|
|
|
|
<ScrollableList
|
2020-05-07 06:55:24 +01:00
|
|
|
scrollKey='group-members'
|
2020-02-29 15:42:47 +00:00
|
|
|
hasMore={hasMore}
|
2020-05-07 06:55:24 +01:00
|
|
|
showLoading={(!group || !accountIds || !relationships)}
|
2020-02-29 15:42:47 +00:00
|
|
|
onLoadMore={this.handleLoadMore}
|
|
|
|
emptyMessage={<FormattedMessage id='group.members.empty' defaultMessage='This group does not has any members.' />}
|
|
|
|
>
|
2020-05-07 06:55:24 +01:00
|
|
|
{
|
2020-06-11 04:27:03 +01:00
|
|
|
accountIds && accountIds.map((id) => (
|
|
|
|
<Account
|
|
|
|
compact
|
|
|
|
key={id}
|
|
|
|
id={id}
|
|
|
|
actionIcon={!isAdmin ? undefined : 'ellipsis'}
|
|
|
|
onActionClick={(data, event) => {
|
|
|
|
return !isAdmin ? false : this.handleOpenGroupMemberOptions(event, id)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
))
|
2020-05-07 06:55:24 +01:00
|
|
|
}
|
2020-02-29 15:42:47 +00:00
|
|
|
</ScrollableList>
|
2020-05-07 06:55:24 +01:00
|
|
|
)
|
2020-02-29 15:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|