2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
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-11-25 21:22:37 +00:00
|
|
|
import { fetchGroupsByTab } 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-07-28 21:11:51 +01:00
|
|
|
import GroupListItemPlaceholder from '../placeholder/group_list_item_placeholder'
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-08-13 00:39:50 +01:00
|
|
|
class GroupsPanel extends ImmutablePureComponent {
|
2020-04-29 23:32:49 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
state = {
|
|
|
|
fetched: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-25 21:22:37 +00:00
|
|
|
this.props.onFetchGroupsByTab(this.props.groupType)
|
2020-04-28 06:33:58 +01:00
|
|
|
}
|
2020-02-08 06:12:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 20:28:46 +01:00
|
|
|
componentDidMount() {
|
|
|
|
if (!this.props.isLazy) {
|
2020-11-25 21:22:37 +00:00
|
|
|
this.props.onFetchGroupsByTab(this.props.groupType)
|
2020-06-05 20:28:46 +01:00
|
|
|
this.setState({ fetched: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
render() {
|
2020-08-06 06:04:11 +01:00
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
groupIds,
|
|
|
|
groupType,
|
|
|
|
} = 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-08-06 06:04:11 +01:00
|
|
|
const maxCount = 12
|
2020-02-22 23:26:23 +00:00
|
|
|
|
2020-06-05 20:28:46 +01:00
|
|
|
if (count === 0 && fetched) return null
|
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
return (
|
|
|
|
<PanelLayout
|
2020-08-06 06:04:11 +01:00
|
|
|
title={intl.formatMessage(groupType === 'member' ? messages.memberTitle : messages.featuredTitle)}
|
2020-12-26 20:28:09 +00:00
|
|
|
headerButtonTitle={intl.formatMessage(messages.show_all)}
|
2020-08-06 06:04:11 +01:00
|
|
|
headerButtonTo={groupType === 'member' ? '/groups/browse/member' : '/groups/browse/featured'}
|
2020-02-22 23:26:23 +00:00
|
|
|
footerButtonTitle={count > maxCount ? intl.formatMessage(messages.show_all) : undefined}
|
2020-08-06 06:04:11 +01:00
|
|
|
footerButtonTo={count > maxCount ? '/groups' : undefined}
|
2020-07-14 06:24:28 +01:00
|
|
|
noPadding
|
2020-02-08 06:12:01 +00:00
|
|
|
>
|
2020-06-05 20:28:46 +01:00
|
|
|
<ScrollableList
|
|
|
|
scrollKey='groups_panel'
|
|
|
|
showLoading={!fetched}
|
2020-07-28 21:11:51 +01:00
|
|
|
placeholderComponent={GroupListItemPlaceholder}
|
|
|
|
placeholderCount={6}
|
2020-06-05 20:28:46 +01:00
|
|
|
>
|
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}
|
|
|
|
isLast={groupIds.count() - 1 === i}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ScrollableList>
|
2020-02-08 06:12:01 +00:00
|
|
|
</PanelLayout>
|
|
|
|
)
|
|
|
|
}
|
2020-04-29 23:32:49 +01:00
|
|
|
|
2020-08-19 01:22:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
memberTitle: { id: 'groups.sidebar-panel.member_title', defaultMessage: 'Groups you\'re in' },
|
|
|
|
featuredTitle: { id: 'groups.sidebar-panel.featured_title', defaultMessage: 'Featured Groups' },
|
|
|
|
show_all: { id: 'groups.sidebar-panel.show_all', defaultMessage: 'Show all' },
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapStateToProps = (state, { groupType }) => ({
|
|
|
|
groupIds: state.getIn(['group_lists', groupType, 'items']),
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
2020-11-25 21:22:37 +00:00
|
|
|
onFetchGroupsByTab: (type) => dispatch(fetchGroupsByTab(type))
|
2020-08-19 01:22:15 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
GroupsPanel.propTypes = {
|
|
|
|
groupIds: ImmutablePropTypes.list,
|
|
|
|
isLazy: PropTypes.bool,
|
2020-12-09 20:02:43 +00:00
|
|
|
onFetchGroupsByTab: PropTypes.func.isRequired,
|
2020-08-19 01:22:15 +01:00
|
|
|
shouldLoad: PropTypes.bool,
|
|
|
|
groupType: PropTypes.string,
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupsPanel.defaultProps = {
|
|
|
|
groupType: 'member'
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(GroupsPanel))
|