import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { joinGroup, leaveGroup } from 'soapbox/actions/groups'; import { openModal } from 'soapbox/actions/modals'; import { Button } from 'soapbox/components/ui'; import { useAppDispatch } from 'soapbox/hooks'; import { Group } from 'soapbox/types/entities'; interface IGroupActionButton { group: Group } const messages = defineMessages({ confirmationHeading: { id: 'confirmations.leave_group.heading', defaultMessage: 'Leave group' }, confirmationMessage: { id: 'confirmations.leave_group.message', defaultMessage: 'You are about to leave the group. Do you want to continue?' }, confirmationConfirm: { id: 'confirmations.leave_group.confirm', defaultMessage: 'Leave' }, }); const GroupActionButton = ({ group }: IGroupActionButton) => { const dispatch = useAppDispatch(); const intl = useIntl(); const isNonMember = !group.relationship || !group.relationship.member; const isRequested = group.relationship?.requested; const isAdmin = group.relationship?.role === 'admin'; const onJoinGroup = () => dispatch(joinGroup(group.id)); const onLeaveGroup = () => dispatch(openModal('CONFIRM', { heading: intl.formatMessage(messages.confirmationHeading), message: intl.formatMessage(messages.confirmationMessage), confirm: intl.formatMessage(messages.confirmationConfirm), onConfirm: () => dispatch(leaveGroup(group.id)), })); if (isNonMember) { return ( ); } if (isRequested) { return ( ); } if (isAdmin) { return ( ); } return ( ); }; export default GroupActionButton;