2023-03-15 15:59:37 -07:00
import React from 'react' ;
2022-12-18 09:03:41 -08:00
import { defineMessages , useIntl } from 'react-intl' ;
import { useHistory } from 'react-router-dom' ;
2023-03-20 12:40:21 -07:00
import { editGroup } from 'soapbox/actions/groups' ;
2022-12-18 09:03:41 -08:00
import { openModal } from 'soapbox/actions/modals' ;
import List , { ListItem } from 'soapbox/components/list' ;
2023-03-20 19:52:44 -07:00
import { CardBody , CardHeader , CardTitle , Column , Spinner , Text } from 'soapbox/components/ui' ;
2023-03-20 12:40:21 -07:00
import { useAppDispatch , useGroup , useGroupsPath } from 'soapbox/hooks' ;
import { useDeleteGroup } from 'soapbox/hooks/api' ;
import { GroupRoles } from 'soapbox/schemas/group-member' ;
import toast from 'soapbox/toast' ;
2022-12-18 09:03:41 -08:00
import ColumnForbidden from '../ui/components/column-forbidden' ;
type RouteParams = { id : string } ;
const messages = defineMessages ( {
heading : { id : 'column.manage_group' , defaultMessage : 'Manage group' } ,
editGroup : { id : 'manage_group.edit_group' , defaultMessage : 'Edit group' } ,
pendingRequests : { id : 'manage_group.pending_requests' , defaultMessage : 'Pending requests' } ,
2023-03-20 19:56:15 -07:00
blockedMembers : { id : 'manage_group.blocked_members' , defaultMessage : 'Banned members' } ,
2022-12-18 09:03:41 -08:00
deleteGroup : { id : 'manage_group.delete_group' , defaultMessage : 'Delete group' } ,
deleteConfirm : { id : 'confirmations.delete_group.confirm' , defaultMessage : 'Delete' } ,
deleteHeading : { id : 'confirmations.delete_group.heading' , defaultMessage : 'Delete group' } ,
deleteMessage : { id : 'confirmations.delete_group.message' , defaultMessage : 'Are you sure you want to delete this group? This is a permanent action that cannot be undone.' } ,
2023-03-20 19:52:44 -07:00
members : { id : 'group.tabs.members' , defaultMessage : 'Members' } ,
other : { id : 'settings.other' , defaultMessage : 'Other options' } ,
2023-03-20 12:40:21 -07:00
deleteSuccess : { id : 'group.delete.success' , defaultMessage : 'Group successfully deleted' } ,
2022-12-18 09:03:41 -08:00
} ) ;
interface IManageGroup {
params : RouteParams
}
const ManageGroup : React.FC < IManageGroup > = ( { params } ) = > {
2023-03-15 15:59:37 -07:00
const { id } = params ;
2022-12-18 09:03:41 -08:00
const intl = useIntl ( ) ;
2023-03-15 15:59:37 -07:00
const history = useHistory ( ) ;
2022-12-18 09:03:41 -08:00
const dispatch = useAppDispatch ( ) ;
2023-03-20 12:40:21 -07:00
const groupsPath = useGroupsPath ( ) ;
2022-12-18 09:03:41 -08:00
2023-03-15 15:59:37 -07:00
const { group } = useGroup ( id ) ;
2022-12-18 09:03:41 -08:00
2023-03-20 12:40:21 -07:00
const deleteGroup = useDeleteGroup ( ) ;
const isOwner = group ? . relationship ? . role === GroupRoles . OWNER ;
2022-12-18 09:03:41 -08:00
if ( ! group || ! group . relationship ) {
return (
< Column label = { intl . formatMessage ( messages . heading ) } >
< Spinner / >
< / Column >
) ;
}
2023-03-15 15:19:42 -07:00
if ( ! group . relationship . role || ! [ 'owner' , 'admin' , 'moderator' ] . includes ( group . relationship . role ) ) {
2022-12-18 09:03:41 -08:00
return ( < ColumnForbidden / > ) ;
}
const onEditGroup = ( ) = >
dispatch ( editGroup ( group ) ) ;
const onDeleteGroup = ( ) = >
dispatch ( openModal ( 'CONFIRM' , {
icon : require ( '@tabler/icons/trash.svg' ) ,
heading : intl.formatMessage ( messages . deleteHeading ) ,
message : intl.formatMessage ( messages . deleteMessage ) ,
confirm : intl.formatMessage ( messages . deleteConfirm ) ,
2023-03-20 12:40:21 -07:00
onConfirm : ( ) = > {
deleteGroup . mutate ( group . id , {
onSuccess() {
toast . success ( intl . formatMessage ( messages . deleteSuccess ) ) ;
history . push ( groupsPath ) ;
} ,
} ) ;
} ,
2022-12-18 09:03:41 -08:00
} ) ) ;
const navigateToPending = ( ) = > history . push ( ` /groups/ ${ id } /manage/requests ` ) ;
const navigateToBlocks = ( ) = > history . push ( ` /groups/ ${ id } /manage/blocks ` ) ;
return (
< Column label = { intl . formatMessage ( messages . heading ) } backHref = { ` /groups/ ${ id } ` } >
< CardBody className = 'space-y-4' >
2023-03-20 12:40:21 -07:00
{ isOwner && (
2023-03-20 19:52:44 -07:00
< >
< CardHeader >
< CardTitle title = { intl . formatMessage ( messages . editGroup ) } / >
< / CardHeader >
< List >
< ListItem label = { intl . formatMessage ( messages . editGroup ) } onClick = { onEditGroup } >
< span dangerouslySetInnerHTML = { { __html : group.display_name_html } } / >
< / ListItem >
< / List >
< / >
2022-12-18 09:03:41 -08:00
) }
2023-03-20 19:52:44 -07:00
< CardHeader >
< CardTitle title = { intl . formatMessage ( messages . members ) } / >
< / CardHeader >
2022-12-18 09:03:41 -08:00
< List >
< ListItem label = { intl . formatMessage ( messages . pendingRequests ) } onClick = { navigateToPending } / >
< ListItem label = { intl . formatMessage ( messages . blockedMembers ) } onClick = { navigateToBlocks } / >
< / List >
2023-03-20 19:52:44 -07:00
2023-03-20 12:40:21 -07:00
{ isOwner && (
2023-03-20 19:52:44 -07:00
< >
< CardHeader >
< CardTitle title = { intl . formatMessage ( messages . other ) } / >
< / CardHeader >
< List >
< ListItem label = { < Text theme = 'danger' > { intl . formatMessage ( messages . deleteGroup ) } < / Text > } onClick = { onDeleteGroup } / >
< / List >
< / >
2022-12-18 09:03:41 -08:00
) }
< / CardBody >
< / Column >
) ;
} ;
export default ManageGroup ;