gab-social/app/javascript/gabsocial/features/group_members.js

146 lines
4.0 KiB
JavaScript
Raw Normal View History

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'
import isObject from 'lodash.isobject'
import { FormattedMessage } from 'react-intl'
import { me } from '../initial_state'
2020-02-29 15:42:47 +00:00
import {
fetchMembers,
expandMembers,
2020-05-07 06:55:24 +01:00
} from '../actions/groups'
import { openPopover } from '../actions/popover'
2020-05-07 06:55:24 +01:00
import Account from '../components/account'
import ColumnIndicator from '../components/column_indicator'
import Block from '../components/block'
import BlockHeading from '../components/block_heading'
import Input from '../components/input'
2020-05-07 06:55:24 +01:00
import ScrollableList from '../components/scrollable_list'
2020-02-29 15:42:47 +00:00
const mapStateToProps = (state, { params }) => {
const groupId = isObject(params) ? params['id'] : -1
const group = groupId === -1 ? null : state.getIn(['groups', groupId])
return {
group,
groupId,
relationships: state.getIn(['group_relationships', groupId]),
accountIds: state.getIn(['user_lists', 'groups', groupId, 'items']),
hasMore: !!state.getIn(['user_lists', 'groups', groupId, 'next']),
}
}
2020-05-07 06:55:24 +01:00
const mapDispatchToProps = (dispatch) => ({
onFetchMembers(groupId) {
dispatch(fetchMembers(groupId))
},
onExpandMembers(groupId) {
dispatch(expandMembers(groupId))
},
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 = {
group: ImmutablePropTypes.map,
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,
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
}
}
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
if (!group || !relationships) return <ColumnIndicator type='loading' />
const isAdmin = relationships ? relationships.get('admin') : false
if (!isAdmin) return <ColumnIndicator type='missing' />
2020-02-29 15:42:47 +00:00
return (
<Block>
<BlockHeading title='Group Members' />
2020-05-07 06:55:24 +01:00
{
/* : todo :
<div className={[_s.default, _s.justifyContentCenter, _s.px15, _s.my5, _s.borderBottom1PX, _s.borderColorSecondary, _s.pt5, _s.pb15].join(' ')}>
<Input
id='group-member-search'
placeholder='Search group members'
prependIcon='search'
// value={value}
onKeyUp={this.handleKeyUp}
onChange={this.handleOnChange}
onFocus={this.handleOnFocus}
onBlur={this.handleOnBlur}
autoComplete='off'
/>
</div>
*/
2020-05-07 06:55:24 +01:00
}
<div className={[_s.default].join(' ')}>
<ScrollableList
scrollKey='group-members'
hasMore={hasMore}
showLoading={(!group || !accountIds || !relationships)}
onLoadMore={this.handleLoadMore}
emptyMessage={<FormattedMessage id='group.members.empty' defaultMessage='This group does not has any members.' />}
>
{
accountIds && accountIds.map((id) => (
<Account
compact
key={id}
id={id}
actionIcon={(!isAdmin || id === me) ? undefined : 'ellipsis'}
onActionClick={(data, event) => {
return !isAdmin ? false : this.handleOpenGroupMemberOptions(event, id)
}}
/>
))
}
</ScrollableList>
</div>
</Block>
2020-05-07 06:55:24 +01:00
)
2020-02-29 15:42:47 +00:00
}
}