2020-12-29 20:17:03 -08:00
import React from 'react' ;
2020-12-29 22:42:55 -08:00
import { injectIntl , defineMessages , FormattedMessage } from 'react-intl' ;
2020-12-29 20:17:03 -08:00
import { connect } from 'react-redux' ;
import ImmutablePureComponent from 'react-immutable-pure-component' ;
import {
SimpleForm ,
FieldsGroup ,
RadioGroup ,
RadioItem ,
} from 'soapbox/features/forms' ;
2020-12-29 21:25:07 -08:00
import { updateConfig } from 'soapbox/actions/admin' ;
2020-12-29 22:42:55 -08:00
import snackbar from 'soapbox/actions/snackbar' ;
const messages = defineMessages ( {
saved : { id : 'admin.dashboard.settings_saved' , defaultMessage : 'Settings saved!' } ,
} ) ;
2020-12-29 20:17:03 -08:00
const mapStateToProps = ( state , props ) => ( {
2020-12-29 21:25:07 -08:00
mode : modeFromInstance ( state . get ( 'instance' ) ) ,
2020-12-29 20:17:03 -08:00
} ) ;
2020-12-29 21:25:07 -08:00
const generateConfig = mode => {
const configMap = {
open : [ { tuple : [ ':registrations_open' , true ] } , { tuple : [ ':account_approval_required' , false ] } ] ,
approval : [ { tuple : [ ':registrations_open' , true ] } , { tuple : [ ':account_approval_required' , true ] } ] ,
closed : [ { tuple : [ ':registrations_open' , false ] } ] ,
} ;
return [ {
group : ':pleroma' ,
key : ':instance' ,
value : configMap [ mode ] ,
} ] ;
} ;
const modeFromInstance = instance => {
if ( instance . get ( 'approval_required' ) && instance . get ( 'registrations' ) ) return 'approval' ;
return instance . get ( 'registrations' ) ? 'open' : 'closed' ;
} ;
2020-12-29 20:17:03 -08:00
export default @ connect ( mapStateToProps )
2020-12-29 22:42:55 -08:00
@ injectIntl
2020-12-29 20:17:03 -08:00
class RegistrationModePicker extends ImmutablePureComponent {
2020-12-29 21:25:07 -08:00
onChange = e => {
2020-12-29 22:42:55 -08:00
const { dispatch , intl } = this . props ;
2020-12-29 21:25:07 -08:00
const config = generateConfig ( e . target . value ) ;
2020-12-29 22:42:55 -08:00
dispatch ( updateConfig ( config ) ) . then ( ( ) => {
dispatch ( snackbar . success ( intl . formatMessage ( messages . saved ) ) ) ;
} ) . catch ( ( ) => { } ) ;
2020-12-29 21:25:07 -08:00
}
2020-12-29 20:17:03 -08:00
render ( ) {
2020-12-29 21:25:07 -08:00
const { mode } = this . props ;
2020-12-29 20:17:03 -08:00
return (
< SimpleForm >
< FieldsGroup >
< RadioGroup
label = { < FormattedMessage id = 'admin.dashboard.registration_mode_label' defaultMessage = 'Registrations' / > }
onChange = { this . onChange }
>
< RadioItem
label = { < FormattedMessage id = 'admin.dashboard.registration_mode.open_label' defaultMessage = 'Open' / > }
hint = { < FormattedMessage id = 'admin.dashboard.registration_mode.open_hint' defaultMessage = 'Anyone can join.' / > }
2020-12-29 21:25:07 -08:00
checked = { mode === 'open' }
2020-12-29 20:17:03 -08:00
value = 'open'
/ >
< RadioItem
label = { < FormattedMessage id = 'admin.dashboard.registration_mode.approval_label' defaultMessage = 'Approval Required' / > }
hint = { < FormattedMessage id = 'admin.dashboard.registration_mode.approval_hint' defaultMessage = 'Users can sign up, but their account only gets activated when an admin approves it.' / > }
2020-12-29 21:25:07 -08:00
checked = { mode === 'approval' }
2020-12-29 20:17:03 -08:00
value = 'approval'
/ >
< RadioItem
label = { < FormattedMessage id = 'admin.dashboard.registration_mode.closed_label' defaultMessage = 'Closed' / > }
hint = { < FormattedMessage id = 'admin.dashboard.registration_mode.closed_hint' defaultMessage = 'Nobody can sign up. You can still invite people.' / > }
2020-12-29 21:25:07 -08:00
checked = { mode === 'closed' }
2020-12-29 20:17:03 -08:00
value = 'closed'
/ >
< / R a d i o G r o u p >
< / F i e l d s G r o u p >
< / S i m p l e F o r m >
) ;
2021-08-03 12:22:51 -07:00
}
2020-12-29 20:17:03 -08:00
}