2020-12-29 13:55:04 -08:00
import React from 'react' ;
import { defineMessages , injectIntl } from 'react-intl' ;
import { connect } from 'react-redux' ;
import ImmutablePureComponent from 'react-immutable-pure-component' ;
import PropTypes from 'prop-types' ;
import ImmutablePropTypes from 'react-immutable-proptypes' ;
import Column from '../ui/components/column' ;
2020-12-29 16:22:31 -08:00
import IconButton from 'soapbox/components/icon_button' ;
import ScrollableList from 'soapbox/components/scrollable_list' ;
import { fetchUsers , deleteUsers , approveUsers } from 'soapbox/actions/admin' ;
2020-12-31 08:56:43 -08:00
import snackbar from 'soapbox/actions/snackbar' ;
2020-12-29 13:55:04 -08:00
const messages = defineMessages ( {
heading : { id : 'column.admin.awaiting_approval' , defaultMessage : 'Awaiting Approval' } ,
2020-12-29 16:38:58 -08:00
emptyMessage : { id : 'admin.awaiting_approval.empty_message' , defaultMessage : 'There is nobody waiting for approval. When a new user signs up, you can review them here.' } ,
2020-12-31 08:56:43 -08:00
approved : { id : 'admin.awaiting_approval.approved_message' , defaultMessage : '{acct} was approved!' } ,
rejected : { id : 'admin.awaiting_approval.rejected_message' , defaultMessage : '{acct} was rejected.' } ,
2020-12-29 13:55:04 -08:00
} ) ;
const mapStateToProps = state => {
2020-12-29 16:22:31 -08:00
const nicknames = state . getIn ( [ 'admin' , 'awaitingApproval' ] ) ;
2020-12-29 13:55:04 -08:00
return {
2020-12-29 16:22:31 -08:00
users : nicknames . toList ( ) . map ( nickname => state . getIn ( [ 'admin' , 'users' , nickname ] ) ) ,
2020-12-29 13:55:04 -08:00
} ;
} ;
export default @ connect ( mapStateToProps )
@ injectIntl
class AwaitingApproval extends ImmutablePureComponent {
static propTypes = {
intl : PropTypes . object . isRequired ,
users : ImmutablePropTypes . list . isRequired ,
} ;
2020-12-29 16:22:31 -08:00
state = {
isLoading : true ,
}
2020-12-29 13:55:04 -08:00
componentDidMount ( ) {
2020-12-29 16:22:31 -08:00
const { dispatch } = this . props ;
const params = { page : 1 , filters : 'local,need_approval' } ;
dispatch ( fetchUsers ( params ) )
. then ( ( ) => this . setState ( { isLoading : false } ) )
. catch ( ( ) => { } ) ;
}
handleApprove = nickname => {
2020-12-31 08:56:43 -08:00
const { dispatch , intl } = this . props ;
2020-12-29 16:22:31 -08:00
return e => {
2020-12-31 08:56:43 -08:00
dispatch ( approveUsers ( [ nickname ] ) ) . then ( ( ) => {
const message = intl . formatMessage ( messages . approved , { acct : ` @ ${ nickname } ` } ) ;
dispatch ( snackbar . success ( message ) ) ;
} ) . catch ( ( ) => { } ) ;
2020-12-29 16:22:31 -08:00
} ;
}
handleReject = nickname => {
2020-12-31 08:56:43 -08:00
const { dispatch , intl } = this . props ;
2020-12-29 16:22:31 -08:00
return e => {
2020-12-31 08:56:43 -08:00
dispatch ( deleteUsers ( [ nickname ] ) ) . then ( ( ) => {
const message = intl . formatMessage ( messages . rejected , { acct : ` @ ${ nickname } ` } ) ;
dispatch ( snackbar . info ( message ) ) ;
} ) . catch ( ( ) => { } ) ;
2020-12-29 16:22:31 -08:00
} ;
2020-12-29 13:55:04 -08:00
}
render ( ) {
const { intl , users } = this . props ;
2020-12-29 16:22:31 -08:00
const { isLoading } = this . state ;
2020-12-31 12:37:35 -08:00
const showLoading = isLoading && users . count ( ) === 0 ;
2020-12-29 13:55:04 -08:00
return (
< Column icon = 'user' heading = { intl . formatMessage ( messages . heading ) } backBtnSlim >
2020-12-31 12:37:35 -08:00
< ScrollableList isLoading = { isLoading } showLoading = { showLoading } scrollKey = 'awaiting-approval' emptyMessage = { intl . formatMessage ( messages . emptyMessage ) } >
2020-12-29 16:22:31 -08:00
{ users . map ( ( user , i ) => (
< div className = 'unapproved-account' key = { user . get ( 'id' ) } >
< div className = 'unapproved-account__bio' >
< div className = 'unapproved-account__nickname' > @ { user . get ( 'nickname' ) } < / d i v >
2020-12-31 15:20:46 -08:00
< blockquote className = 'md' > { user . get ( 'registration_reason' ) } < / b l o c k q u o t e >
2020-12-29 16:22:31 -08:00
< / d i v >
< div className = 'unapproved-account__actions' >
2020-12-31 08:56:43 -08:00
< IconButton icon = 'check' size = { 22 } onClick = { this . handleApprove ( user . get ( 'nickname' ) ) } / >
< IconButton icon = 'close' size = { 22 } onClick = { this . handleReject ( user . get ( 'nickname' ) ) } / >
2020-12-29 16:22:31 -08:00
< / d i v >
< / d i v >
) ) }
< / S c r o l l a b l e L i s t >
2020-12-29 13:55:04 -08:00
< / C o l u m n >
) ;
}
}