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-22 23:26:23 +00:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
2020-07-14 06:41:20 +01:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl'
|
2020-11-25 21:22:37 +00:00
|
|
|
import { fetchGroupsByTab } from '../actions/groups'
|
2020-07-25 03:41:05 +01:00
|
|
|
import { openPopover } from '../actions/popover'
|
|
|
|
import { POPOVER_GROUP_LIST_SORT_OPTIONS } from '../constants'
|
2020-07-14 06:41:20 +01:00
|
|
|
import Block from '../components/block'
|
|
|
|
import Button from '../components/button'
|
2020-04-02 04:17:21 +01:00
|
|
|
import ColumnIndicator from '../components/column_indicator'
|
2020-07-14 06:41:20 +01:00
|
|
|
import Heading from '../components/heading'
|
|
|
|
import GroupListItem from '../components/group_list_item'
|
|
|
|
|
2020-02-22 23:26:23 +00:00
|
|
|
class GroupsCollection extends ImmutablePureComponent {
|
2020-04-08 02:06:59 +01:00
|
|
|
|
2020-02-22 23:26:23 +00:00
|
|
|
componentWillMount() {
|
2020-11-25 21:22:37 +00:00
|
|
|
this.props.onFetchGroupsByTab(this.props.activeTab)
|
2020-02-22 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(oldProps) {
|
|
|
|
if (this.props.activeTab && this.props.activeTab !== oldProps.activeTab) {
|
2020-11-25 21:22:37 +00:00
|
|
|
this.props.onFetchGroupsByTab(this.props.activeTab)
|
2020-02-22 23:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 03:41:05 +01:00
|
|
|
handleOnOpenSortPopover = () => {
|
|
|
|
this.props.onOpenSortPopover(this.props.activeTab, this.sortBtn)
|
2020-07-14 06:41:20 +01:00
|
|
|
}
|
|
|
|
|
2020-07-25 03:41:05 +01:00
|
|
|
setSortBtn = (n) => {
|
|
|
|
this.sortBtn = n
|
2020-07-14 06:41:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 23:26:23 +00:00
|
|
|
render() {
|
2020-06-04 23:48:24 +01:00
|
|
|
const {
|
2020-07-14 06:41:20 +01:00
|
|
|
activeTab,
|
2020-06-04 23:48:24 +01:00
|
|
|
groupIds,
|
2020-07-14 06:41:20 +01:00
|
|
|
intl,
|
2020-06-04 23:48:24 +01:00
|
|
|
isLoading,
|
|
|
|
isFetched,
|
|
|
|
} = this.props
|
2020-02-22 23:26:23 +00:00
|
|
|
|
2020-09-14 23:12:45 +01:00
|
|
|
if (!groupIds || (isFetched && groupIds.size === 0)) {
|
2020-07-14 06:41:20 +01:00
|
|
|
return <ColumnIndicator type='error' message={intl.formatMessage(messages.empty)} />
|
2020-09-14 23:12:45 +01:00
|
|
|
} else if (isLoading && groupIds.size === 0) {
|
|
|
|
return <ColumnIndicator type='loading' />
|
2020-04-02 04:17:21 +01:00
|
|
|
}
|
2020-03-27 15:29:52 +00:00
|
|
|
|
2020-07-14 06:41:20 +01:00
|
|
|
const isAddable = ['featured', 'new'].indexOf(activeTab) > -1
|
2020-04-08 02:06:59 +01:00
|
|
|
|
2020-02-22 23:26:23 +00:00
|
|
|
return (
|
2020-07-14 06:41:20 +01:00
|
|
|
<Block>
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.flexRow, _s.px15, _s.pt10, _s.jcCenter].join(' ')}>
|
|
|
|
<div className={[_s.d, _s.flexGrow1, _s.maxW80PC, _s.jcCenter, _s.overflowHidden].join(' ')}>
|
2020-07-14 06:41:20 +01:00
|
|
|
<Heading size='h2'>
|
|
|
|
{intl.formatMessage(messages[activeTab])}
|
|
|
|
</Heading>
|
2020-05-07 05:03:34 +01:00
|
|
|
</div>
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.flexRow, _s.mlAuto].join(' ')}>
|
2020-07-25 03:41:05 +01:00
|
|
|
<Button
|
|
|
|
icon='sort'
|
|
|
|
className={_s.px10}
|
|
|
|
color='primary'
|
|
|
|
backgroundColor='none'
|
|
|
|
iconSize='14px'
|
|
|
|
onClick={this.handleOnOpenSortPopover}
|
|
|
|
buttonRef={this.setSortBtn}
|
2020-07-14 06:41:20 +01:00
|
|
|
/>
|
2020-05-07 05:03:34 +01:00
|
|
|
</div>
|
2020-07-25 03:41:05 +01:00
|
|
|
</div>
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.py10, _s.w100PC].join(' ')}>
|
2020-07-14 06:41:20 +01:00
|
|
|
{
|
|
|
|
groupIds.map((groupId, i) => (
|
|
|
|
<GroupListItem
|
|
|
|
isAddable={isAddable}
|
|
|
|
key={`group-collection-item-${i}`}
|
|
|
|
id={groupId}
|
|
|
|
isLast={groupIds.count() - 1 === i}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</Block>
|
2020-02-22 23:26:23 +00:00
|
|
|
)
|
|
|
|
}
|
2020-04-08 02:06:59 +01:00
|
|
|
|
2020-08-19 01:22:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) => ({
|
2020-11-25 21:22:37 +00:00
|
|
|
onFetchGroupsByTab: (tab) => dispatch(fetchGroupsByTab(tab)),
|
2020-08-19 01:22:15 +01:00
|
|
|
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))
|