2023-03-20 14:41:12 -07:00
import React from 'react' ;
2022-12-18 09:03:41 -08:00
import { FormattedMessage , defineMessages , useIntl } from 'react-intl' ;
2023-03-20 14:41:12 -07:00
import { authorizeGroupMembershipRequest , rejectGroupMembershipRequest } from 'soapbox/actions/groups' ;
2022-12-18 09:03:41 -08:00
import Account from 'soapbox/components/account' ;
import ScrollableList from 'soapbox/components/scrollable-list' ;
import { Button , Column , HStack , Spinner } from 'soapbox/components/ui' ;
2023-03-20 14:41:12 -07:00
import { useAppDispatch , useGroup } from 'soapbox/hooks' ;
2023-03-20 13:54:06 -07:00
import { useGroupMembershipRequests } from 'soapbox/hooks/api/groups/useGroupMembershipRequests' ;
2023-01-27 14:04:42 -08:00
import toast from 'soapbox/toast' ;
2022-12-18 09:03:41 -08:00
import ColumnForbidden from '../ui/components/column-forbidden' ;
2023-03-20 14:41:12 -07:00
import type { Account as AccountEntity } from 'soapbox/schemas' ;
2022-12-18 09:03:41 -08:00
type RouteParams = { id : string } ;
const messages = defineMessages ( {
heading : { id : 'column.group_pending_requests' , defaultMessage : 'Pending requests' } ,
authorize : { id : 'group.group_mod_authorize' , defaultMessage : 'Accept' } ,
authorized : { id : 'group.group_mod_authorize.success' , defaultMessage : 'Accepted @{name} to group' } ,
reject : { id : 'group.group_mod_reject' , defaultMessage : 'Reject' } ,
rejected : { id : 'group.group_mod_reject.success' , defaultMessage : 'Rejected @{name} from group' } ,
} ) ;
interface IMembershipRequest {
2023-03-20 14:41:12 -07:00
account : AccountEntity
2022-12-18 09:03:41 -08:00
groupId : string
}
2023-03-20 14:41:12 -07:00
const MembershipRequest : React.FC < IMembershipRequest > = ( { account , groupId } ) = > {
2022-12-18 09:03:41 -08:00
const intl = useIntl ( ) ;
const dispatch = useAppDispatch ( ) ;
if ( ! account ) return null ;
const handleAuthorize = ( ) = >
2023-03-20 14:41:12 -07:00
dispatch ( authorizeGroupMembershipRequest ( groupId , account . id ) ) . then ( ( ) = > {
2023-01-27 14:04:42 -08:00
toast . success ( intl . formatMessage ( messages . authorized , { name : account.acct } ) ) ;
2022-12-18 09:03:41 -08:00
} ) ;
const handleReject = ( ) = >
2023-03-20 14:41:12 -07:00
dispatch ( rejectGroupMembershipRequest ( groupId , account . id ) ) . then ( ( ) = > {
2023-01-27 14:04:42 -08:00
toast . success ( intl . formatMessage ( messages . rejected , { name : account.acct } ) ) ;
2022-12-18 09:03:41 -08:00
} ) ;
return (
< HStack space = { 1 } alignItems = 'center' justifyContent = 'between' className = 'p-2.5' >
< div className = 'w-full' >
< Account account = { account } withRelationship = { false } / >
< / div >
< HStack space = { 2 } >
< Button
theme = 'secondary'
size = 'sm'
text = { intl . formatMessage ( messages . authorize ) }
onClick = { handleAuthorize }
/ >
< Button
theme = 'danger'
size = 'sm'
text = { intl . formatMessage ( messages . reject ) }
onClick = { handleReject }
/ >
< / HStack >
< / HStack >
) ;
} ;
interface IGroupMembershipRequests {
params : RouteParams
}
const GroupMembershipRequests : React.FC < IGroupMembershipRequests > = ( { params } ) = > {
const intl = useIntl ( ) ;
2023-03-15 17:26:37 -07:00
const id = params ? . id ;
2022-12-18 09:03:41 -08:00
2023-03-15 17:26:37 -07:00
const { group } = useGroup ( id ) ;
2023-03-20 14:41:12 -07:00
const { entities : accounts , isLoading } = useGroupMembershipRequests ( id ) ;
2022-12-18 09:03:41 -08:00
2023-03-20 14:41:12 -07:00
if ( ! group || ! group . relationship || isLoading ) {
2022-12-18 09:03:41 -08:00
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 emptyMessage = < FormattedMessage id = 'empty_column.group_membership_requests' defaultMessage = 'There are no pending membership requests for this group.' / > ;
return (
< Column label = { intl . formatMessage ( messages . heading ) } backHref = { ` /groups/ ${ id } /manage ` } >
< ScrollableList
scrollKey = 'group_membership_requests'
emptyMessage = { emptyMessage }
>
2023-03-20 14:41:12 -07:00
{ accounts . map ( ( account ) = >
< MembershipRequest key = { account . id } account = { account } groupId = { id } / > ,
2022-12-18 09:03:41 -08:00
) }
< / ScrollableList >
< / Column >
) ;
} ;
export default GroupMembershipRequests ;