2020-02-08 06:12:01 +00:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl'
|
2020-04-28 06:33:58 +01:00
|
|
|
import { fetchGroups } from '../../actions/groups'
|
2020-02-08 06:12:01 +00:00
|
|
|
import PanelLayout from './panel_layout'
|
2020-02-19 23:57:07 +00:00
|
|
|
import GroupListItem from '../group_list_item'
|
2020-04-28 06:33:58 +01:00
|
|
|
import ScrollableList from '../scrollable_list'
|
2020-02-08 06:12:01 +00:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
title: { id: 'groups.sidebar-panel.title', defaultMessage: 'Groups you\'re in' },
|
|
|
|
show_all: { id: 'groups.sidebar-panel.show_all', defaultMessage: 'Show all' },
|
|
|
|
all: { id: 'groups.sidebar-panel.all', defaultMessage: 'All' },
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => ({
|
2020-06-04 23:48:24 +01:00
|
|
|
groupIds: state.getIn(['group_lists', 'member', 'items']),
|
2020-02-08 06:12:01 +00:00
|
|
|
})
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
2020-04-29 23:32:49 +01:00
|
|
|
onFetchGroups: (type) => dispatch(fetchGroups(type))
|
2020-04-28 06:33:58 +01:00
|
|
|
})
|
|
|
|
|
2020-02-25 16:04:44 +00:00
|
|
|
export default
|
2020-04-28 06:33:58 +01:00
|
|
|
@connect(mapStateToProps, mapDispatchToProps)
|
2020-02-08 06:12:01 +00:00
|
|
|
@injectIntl
|
|
|
|
class GroupSidebarPanel extends ImmutablePureComponent {
|
2020-04-29 23:32:49 +01:00
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
static propTypes = {
|
|
|
|
groupIds: ImmutablePropTypes.list,
|
2020-04-28 06:33:58 +01:00
|
|
|
isLazy: PropTypes.bool,
|
|
|
|
isSlim: PropTypes.bool,
|
|
|
|
onFetchGroups: PropTypes.func.isRequired,
|
2020-06-05 20:28:46 +01:00
|
|
|
shouldLoad: PropTypes.bool,
|
2020-04-28 06:33:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
fetched: false,
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
updateOnProps = [
|
|
|
|
'groupIds',
|
|
|
|
'isLazy',
|
|
|
|
'isSlim',
|
2020-06-05 20:28:46 +01:00
|
|
|
'shouldLoad',
|
2020-04-29 23:32:49 +01:00
|
|
|
]
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
static getDerivedStateFromProps(nextProps, prevState) {
|
2020-06-05 20:28:46 +01:00
|
|
|
if (nextProps.shouldLoad && !prevState.fetched) {
|
|
|
|
return { fetched: true }
|
2020-04-28 06:33:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState, snapshot) {
|
2020-06-05 20:28:46 +01:00
|
|
|
if (!prevState.fetched && this.state.fetched) {
|
2020-04-28 06:33:58 +01:00
|
|
|
this.props.onFetchGroups('member')
|
|
|
|
}
|
2020-02-08 06:12:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 20:28:46 +01:00
|
|
|
componentDidMount() {
|
|
|
|
if (!this.props.isLazy) {
|
|
|
|
this.props.onFetchGroups('member')
|
|
|
|
this.setState({ fetched: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
render() {
|
2020-02-22 23:26:23 +00:00
|
|
|
const { intl, groupIds, slim } = this.props
|
2020-06-05 20:28:46 +01:00
|
|
|
const { fetched } = this.state
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-06-05 20:28:46 +01:00
|
|
|
const count = !!groupIds ? groupIds.count() : 0
|
2020-02-22 23:26:23 +00:00
|
|
|
const maxCount = slim ? 12 : 6
|
|
|
|
|
2020-06-05 20:28:46 +01:00
|
|
|
if (count === 0 && fetched) return null
|
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
return (
|
|
|
|
<PanelLayout
|
|
|
|
title={intl.formatMessage(messages.title)}
|
2020-02-21 00:57:29 +00:00
|
|
|
headerButtonTitle={intl.formatMessage(messages.all)}
|
|
|
|
headerButtonTo='/groups/browse/member'
|
2020-02-22 23:26:23 +00:00
|
|
|
footerButtonTitle={count > maxCount ? intl.formatMessage(messages.show_all) : undefined}
|
|
|
|
footerButtonTo={count > maxCount ? '/groups/browse/member' : undefined}
|
|
|
|
noPadding={slim}
|
2020-02-08 06:12:01 +00:00
|
|
|
>
|
2020-06-05 20:28:46 +01:00
|
|
|
<ScrollableList
|
|
|
|
scrollKey='groups_panel'
|
|
|
|
showLoading={!fetched}
|
|
|
|
>
|
2020-04-29 23:32:49 +01:00
|
|
|
{
|
2020-06-05 20:28:46 +01:00
|
|
|
groupIds && groupIds.slice(0, maxCount).map((groupId, i) => (
|
2020-04-29 23:32:49 +01:00
|
|
|
<GroupListItem
|
|
|
|
key={`group-panel-item-${groupId}`}
|
|
|
|
id={groupId}
|
|
|
|
slim={slim}
|
|
|
|
isLast={groupIds.count() - 1 === i}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ScrollableList>
|
2020-02-08 06:12:01 +00:00
|
|
|
</PanelLayout>
|
|
|
|
)
|
|
|
|
}
|
2020-04-29 23:32:49 +01:00
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
}
|