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 { defineMessages, injectIntl } from 'react-intl' import { openPopover, closePopover } from '../actions/popover' import { openModal } from '../actions/modal' import { joinGroup, leaveGroup } from '../actions/groups' import { addShortcut, removeShortcut, } from '../actions/shortcuts' import { PLACEHOLDER_MISSING_HEADER_SRC, BREAKPOINT_EXTRA_SMALL, } from '../constants' import { me } from '../initial_state' import Responsive from '../features/ui/util/responsive_component' import Button from './button' import Block from './block' import Heading from './heading' import Image from './image' import Icon from './icon' import TabBar from './tab_bar' import Pills from './pills' import Text from './text' class GroupHeader extends ImmutablePureComponent { handleOnToggleMembership = () => { const { group, relationships } = this.props this.props.onToggleMembership(group, relationships); } handleOnOpenGroupOptions = () => { const { relationships } = this.props const isAdmin = !!relationships ? relationships.get('admin') : false const isModerator = !!relationships ? relationships.get('moderator') : false this.props.onOpenGroupOptions(this.infoBtn, this.props.group, { isAdmin, isModerator, }) } handleToggleShortcut = () => { const { group, isShortcut } = this.props const groupId = !!group ? group.get('id') : null if (!groupId) return if (isShortcut) { this.props.onRemoveShortcut(groupId) } else { this.props.onAddShortcut(groupId) } } setInfoBtn = (c) => { this.infoBtn = c; } render() { const { children, group, intl, isXS, isShortcut, relationships, } = this.props const coverSrc = !!group ? group.get('cover_image_url') : '' const coverSrcMissing = coverSrc.indexOf(PLACEHOLDER_MISSING_HEADER_SRC) > -1 || !coverSrc const title = !!group ? group.get('title') : undefined const slug = !!group ? !!group.get('slug') ? `g/${group.get('slug')}` : undefined : undefined const isPrivate = !!group ? group.get('is_private') : false let isAdminOrMod = false let actionButtonTitle let actionButtonOptions = {} if (relationships) { isAdminOrMod = relationships.get('admin') || relationships.get('moderator') const isMember = relationships.get('member') actionButtonTitle = intl.formatMessage(isMember ? messages.member : messages.join) actionButtonOptions = { isOutline: !isMember, backgroundColor: isMember ? 'brand' : 'none', color: isMember ? 'white' : 'brand', } } const tabs = !group ? [] : [ { to: `/groups/${group.get('id')}`, title: 'Timeline', }, // { // to: `/groups/${group.get('id')}/media`, // title: 'Media', // }, ] if (isAdminOrMod && group) { tabs.push({ to: `/groups/${group.get('id')}/members`, title: 'Members', }) } if (isAdminOrMod && group && isPrivate) { tabs.push({ to: `/groups/${group.get('id')}/requests`, title: 'Requests', }) } if (isXS && group) { tabs.push({ to: `/groups/${group.get('id')}/about`, title: 'About', }) } // : todo : // {group.get('archived') && } return (
{ coverSrc && !coverSrcMissing && {title} }
{children}
{ !!me &&
{ !!actionButtonTitle && }
}
{ /** desktop */}
{ coverSrc && !coverSrcMissing && {title} }
{title} { !!slug && {slug} }
{ !!me &&
} { !!actionButtonTitle && }
) } } const messages = defineMessages({ join: { id: 'groups.join', defaultMessage: 'Join group' }, member: { id: 'groups.member', defaultMessage: 'Member' }, removed_accounts: { id: 'groups.removed_accounts', defaultMessage: 'Removed Accounts' }, group_archived: { id: 'group.detail.archived_group', defaultMessage: 'Archived group' }, group_admin: { id: 'groups.detail.role_admin', defaultMessage: 'You\'re an admin' } }) const mapStateToProps = (state, { group }) => { const groupId = group ? group.get('id') : null const shortcuts = state.getIn(['shortcuts', 'items']) const isShortcut = !!shortcuts.find((s) => { return s.get('shortcut_id') == groupId && s.get('shortcut_type') === 'group' }) return { isShortcut } } const mapDispatchToProps = (dispatch, { intl }) => ({ onToggleMembership(group, relationships) { if (relationships.get('member')) { dispatch(leaveGroup(group.get('id'))) } else { dispatch(joinGroup(group.get('id'))) } }, onOpenGroupOptions(targetRef, group, options) { dispatch(openPopover('GROUP_OPTIONS', { targetRef, group, ...options, position: 'left', })) }, onAddShortcut(groupId) { dispatch(addShortcut('group', groupId)) }, onRemoveShortcut(groupId) { dispatch(removeShortcut(null, 'group', groupId)) }, }) GroupHeader.propTypes = { group: ImmutablePropTypes.map, children: PropTypes.any, intl: PropTypes.object.isRequired, isShortcut: PropTypes.bool.isRequired, isXS: PropTypes.bool, onAddShortcut: PropTypes.func.isRequired, onToggleMembership: PropTypes.func.isRequired, onRemoveShortcut: PropTypes.func.isRequired, onOpenGroupOptions: PropTypes.func.isRequired, relationships: ImmutablePropTypes.map, } export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(GroupHeader))