import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import ImmutablePropTypes from 'react-immutable-proptypes' import ImmutablePureComponent from 'react-immutable-pure-component' import { injectIntl, defineMessages } from 'react-intl' import { fetchGroupsByTab } from '../actions/groups' import { openPopover } from '../actions/popover' import { POPOVER_GROUP_LIST_SORT_OPTIONS } from '../constants' import Block from '../components/block' import Button from '../components/button' import ColumnIndicator from '../components/column_indicator' import Heading from '../components/heading' import GroupListItem from '../components/group_list_item' class GroupsCollection extends ImmutablePureComponent { componentWillMount() { this.props.onFetchGroupsByTab(this.props.activeTab) } componentDidUpdate(oldProps) { if (this.props.activeTab && this.props.activeTab !== oldProps.activeTab) { this.props.onFetchGroupsByTab(this.props.activeTab) } } handleOnOpenSortPopover = () => { this.props.onOpenSortPopover(this.props.activeTab, this.sortBtn) } setSortBtn = (n) => { this.sortBtn = n } render() { const { activeTab, groupIds, intl, isLoading, isFetched, } = this.props if (!groupIds || (isFetched && groupIds.size === 0)) { return } else if (isLoading && groupIds.size === 0) { return } const isAddable = ['featured', 'new'].indexOf(activeTab) > -1 return ( {intl.formatMessage(messages[activeTab])} { groupIds.map((groupId, i) => ( )) } ) } } const messages = defineMessages({ empty: { id: 'groups.empty', defaultMessage: 'There are no groups to display' }, featured: { id: 'featured', defaultMessage: 'Featured' }, new: { id: 'new', defaultMessage: 'Just Added' }, member: { id: 'my_groups', defaultMessage: 'My Groups' }, admin: { id: 'admin', defaultMessage: 'Admin' }, }) const mapStateToProps = (state, { activeTab }) => ({ groupIds: state.getIn(['group_lists', activeTab, 'items']), isFetched: state.getIn(['group_lists', activeTab, 'isFetched']), isLoading: state.getIn(['group_lists', activeTab, 'isLoading']), }) const mapDispatchToProps = (dispatch) => ({ onFetchGroupsByTab: (tab) => dispatch(fetchGroupsByTab(tab)), onOpenSortPopover(tab, targetRef) { dispatch(openPopover(POPOVER_GROUP_LIST_SORT_OPTIONS, { targetRef, tab, position: 'bottom', })) } }) GroupsCollection.propTypes = { activeTab: PropTypes.string.isRequired, groupIds: ImmutablePropTypes.list, intl: PropTypes.object.isRequired, isFetched: PropTypes.bool.isRequired, isLoading: PropTypes.bool.isRequired, onFetchGroups: PropTypes.func.isRequired, onOpenSortPopover: PropTypes.func.isRequired, } export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(GroupsCollection))