Updated GroupListItem to have toggle membership button, always slim
• Updated: - GroupListItem to have toggle membership button - always slim
This commit is contained in:
parent
1eb33c482d
commit
35fdaf0208
@ -3,9 +3,11 @@ import ImmutablePureComponent from 'react-immutable-pure-component'
|
|||||||
import { Fragment } from 'react'
|
import { Fragment } from 'react'
|
||||||
import { NavLink } from 'react-router-dom'
|
import { NavLink } from 'react-router-dom'
|
||||||
import { defineMessages, injectIntl } from 'react-intl'
|
import { defineMessages, injectIntl } from 'react-intl'
|
||||||
|
import { joinGroup, leaveGroup } from '../actions/groups'
|
||||||
import { CX } from '../constants'
|
import { CX } from '../constants'
|
||||||
import { PLACEHOLDER_MISSING_HEADER_SRC } from '../constants'
|
import { PLACEHOLDER_MISSING_HEADER_SRC } from '../constants'
|
||||||
import { shortNumberFormat } from '../utils/numbers'
|
import { shortNumberFormat } from '../utils/numbers'
|
||||||
|
import Button from './button'
|
||||||
import Image from './image'
|
import Image from './image'
|
||||||
import Text from './text'
|
import Text from './text'
|
||||||
|
|
||||||
@ -15,36 +17,67 @@ const messages = defineMessages({
|
|||||||
|
|
||||||
const mapStateToProps = (state, { id }) => ({
|
const mapStateToProps = (state, { id }) => ({
|
||||||
group: state.getIn(['groups', id]),
|
group: state.getIn(['groups', id]),
|
||||||
|
relationships: state.getIn(['group_relationships', id]),
|
||||||
|
})
|
||||||
|
|
||||||
|
const mapDispatchToProps = (dispatch) => ({
|
||||||
|
onToggleMembership(groupId, relationships) {
|
||||||
|
if (relationships.get('member')) {
|
||||||
|
dispatch(leaveGroup(groupId))
|
||||||
|
} else {
|
||||||
|
dispatch(joinGroup(groupId))
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export default
|
export default
|
||||||
@connect(mapStateToProps)
|
@connect(mapStateToProps, mapDispatchToProps)
|
||||||
@injectIntl
|
@injectIntl
|
||||||
class GroupListItem extends ImmutablePureComponent {
|
class GroupListItem extends ImmutablePureComponent {
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
group: ImmutablePropTypes.map,
|
group: ImmutablePropTypes.map,
|
||||||
slim: PropTypes.bool,
|
isAddable: PropTypes.bool,
|
||||||
isLast: PropTypes.bool,
|
|
||||||
isHidden: PropTypes.bool,
|
isHidden: PropTypes.bool,
|
||||||
|
isLast: PropTypes.bool,
|
||||||
|
onToggleMembership: PropTypes.func.isRequired,
|
||||||
|
relationships: ImmutablePropTypes.map,
|
||||||
}
|
}
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
slim: false,
|
|
||||||
isLast: false,
|
isLast: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = {
|
||||||
|
hovering: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnToggleMembership = () => {
|
||||||
|
const { group, relationships } = this.props
|
||||||
|
this.props.onToggleMembership(group.get('id'), relationships)
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnMouseEnter = () => {
|
||||||
|
this.setState({ hovering: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnMouseLeave = () => {
|
||||||
|
this.setState({ hovering: false })
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
intl,
|
|
||||||
group,
|
group,
|
||||||
slim,
|
intl,
|
||||||
|
isAddable,
|
||||||
isLast,
|
isLast,
|
||||||
isHidden,
|
isHidden,
|
||||||
|
relationships,
|
||||||
} = this.props
|
} = this.props
|
||||||
|
const { hovering } = this.state
|
||||||
|
|
||||||
if (!group) return null
|
if (!group) return null
|
||||||
|
|
||||||
if (isHidden) {
|
if (isHidden) {
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
@ -55,68 +88,75 @@ class GroupListItem extends ImmutablePureComponent {
|
|||||||
|
|
||||||
const containerClasses = CX({
|
const containerClasses = CX({
|
||||||
default: 1,
|
default: 1,
|
||||||
noUnderline: 1,
|
|
||||||
overflowHidden: 1,
|
overflowHidden: 1,
|
||||||
bgSubtle_onHover: 1,
|
bgSubtle_onHover: 1,
|
||||||
borderColorSecondary: 1,
|
borderColorSecondary: 1,
|
||||||
radiusSmall: !slim,
|
borderBottom1PX: !isLast,
|
||||||
mb10: !slim && !isLast,
|
flexRow: 1,
|
||||||
border1PX: !slim,
|
py5: 1,
|
||||||
borderBottom1PX: slim && !isLast,
|
width100PC: 1,
|
||||||
flexRow: slim,
|
|
||||||
py5: slim,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const imageClasses = CX({
|
const containerLinkClasses = CX({
|
||||||
height122PX: !slim,
|
|
||||||
radiusSmall: slim,
|
|
||||||
height72PX: slim,
|
|
||||||
width72PX: slim,
|
|
||||||
ml15: slim,
|
|
||||||
})
|
|
||||||
|
|
||||||
const textContainerClasses = CX({
|
|
||||||
default: 1,
|
default: 1,
|
||||||
px10: 1,
|
flexRow: 1,
|
||||||
mt5: 1,
|
noUnderline: 1,
|
||||||
flexShrink1: slim,
|
width100PC: 1,
|
||||||
mb10: !slim,
|
maxWidth100PC42PX: isAddable,
|
||||||
})
|
})
|
||||||
|
|
||||||
const coverSrc = group.get('cover_image_url') || ''
|
const coverSrc = group.get('cover_image_url') || ''
|
||||||
const coverMissing = coverSrc.indexOf(PLACEHOLDER_MISSING_HEADER_SRC) > -1 || !coverSrc
|
const coverMissing = coverSrc.indexOf(PLACEHOLDER_MISSING_HEADER_SRC) > -1 || !coverSrc
|
||||||
|
const isMember = !!relationships && relationships.get('member')
|
||||||
|
const addButtonIcon = isMember ? hovering ? 'subtract' : 'check' : 'add'
|
||||||
|
const addButtonColor = isMember ? 'tertiary' : 'brand'
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NavLink
|
<div className={containerClasses}>
|
||||||
to={`/groups/${group.get('id')}`}
|
<NavLink
|
||||||
className={containerClasses}
|
to={`/groups/${group.get('id')}`}
|
||||||
>
|
className={containerLinkClasses}
|
||||||
|
>
|
||||||
{
|
|
||||||
(!!coverSrc || slim) && !coverMissing &&
|
|
||||||
<Image
|
|
||||||
src={coverSrc}
|
|
||||||
alt={group.get('title')}
|
|
||||||
className={imageClasses}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
|
|
||||||
<div className={textContainerClasses}>
|
|
||||||
<Text color='brand' weight='bold'>
|
|
||||||
{group.get('title')}
|
|
||||||
</Text>
|
|
||||||
|
|
||||||
{
|
{
|
||||||
slim &&
|
!coverMissing &&
|
||||||
|
<Image
|
||||||
|
src={coverSrc}
|
||||||
|
alt={group.get('title')}
|
||||||
|
className={[_s.radiusSmall, _s.height53PX, _s.width84PX, _s.ml15].join(' ')}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div className={[_s.default, _s.px10, _s.mt5, _s.flexShrink1].join(' ')}>
|
||||||
|
<Text color='brand' weight='bold'>
|
||||||
|
{group.get('title')}
|
||||||
|
</Text>
|
||||||
|
|
||||||
<Text color='secondary' size='small' className={_s.mt5}>
|
<Text color='secondary' size='small' className={_s.mt5}>
|
||||||
{shortNumberFormat(group.get('member_count'))}
|
{shortNumberFormat(group.get('member_count'))}
|
||||||
|
|
||||||
{intl.formatMessage(messages.members)}
|
{intl.formatMessage(messages.members)}
|
||||||
</Text>
|
</Text>
|
||||||
}
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</NavLink>
|
||||||
|
<div className={[_s.default, _s.justifyContentCenter].join(' ')}>
|
||||||
|
{
|
||||||
|
relationships &&
|
||||||
|
<Button
|
||||||
|
isNarrow
|
||||||
|
color='white'
|
||||||
|
backgroundColor={addButtonColor}
|
||||||
|
className={[_s.px5, _s.bgDangerDark_onHover].join(' ')}
|
||||||
|
onClick={this.handleOnToggleMembership}
|
||||||
|
onMouseEnter={this.handleOnMouseEnter}
|
||||||
|
onMouseLeave={this.handleOnMouseLeave}
|
||||||
|
icon={addButtonIcon}
|
||||||
|
iconSize='10px'
|
||||||
|
/>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
</NavLink>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user