2022-01-07 13:58:01 -08:00
import PropTypes from 'prop-types' ;
2022-01-10 14:17:52 -08:00
import React from 'react' ;
2022-01-07 13:58:01 -08:00
import ImmutablePropTypes from 'react-immutable-proptypes' ;
import ImmutablePureComponent from 'react-immutable-pure-component' ;
import { injectIntl , FormattedMessage , defineMessages } from 'react-intl' ;
2022-01-10 14:17:52 -08:00
import { connect } from 'react-redux' ;
2022-01-10 14:25:06 -08:00
2022-01-07 13:58:01 -08:00
import { showAlertForError } from 'soapbox/actions/alerts' ;
import { patchMe } from 'soapbox/actions/me' ;
import { FE _NAME , SETTINGS _UPDATE } from 'soapbox/actions/settings' ;
2022-01-10 14:17:52 -08:00
import Column from 'soapbox/features/ui/components/column' ;
2022-01-07 13:58:01 -08:00
2022-03-21 11:09:01 -07:00
import { Button , Form , FormActions , FormGroup , Textarea } from '../../components/ui' ;
2022-01-07 13:58:01 -08:00
const isJSONValid = text => {
try {
JSON . parse ( text ) ;
return true ;
} catch {
return false ;
}
} ;
const messages = defineMessages ( {
heading : { id : 'column.settings_store' , defaultMessage : 'Settings store' } ,
hint : { id : 'developers.settings_store.hint' , defaultMessage : 'It is possible to directly edit your user settings here. BE CAREFUL! Editing this section can break your account, and you will only be able to recover through the API.' } ,
} ) ;
const mapStateToProps = state => {
return {
settingsStore : state . get ( 'settings' ) ,
} ;
} ;
export default @ connect ( mapStateToProps )
@ injectIntl
class SettingsStore extends ImmutablePureComponent {
static propTypes = {
intl : PropTypes . object . isRequired ,
dispatch : PropTypes . func . isRequired ,
settingsStore : ImmutablePropTypes . map . isRequired ,
}
state = {
rawJSON : JSON . stringify ( this . props . settingsStore , null , 2 ) ,
jsonValid : true ,
isLoading : false ,
}
componentDidUpdate ( prevProps ) {
const { settingsStore } = this . props ;
if ( settingsStore !== prevProps . settingsStore ) {
this . setState ( {
rawJSON : JSON . stringify ( settingsStore , null , 2 ) ,
jsonValid : true ,
} ) ;
}
}
handleEditJSON = ( { target } ) => {
const rawJSON = target . value ;
this . setState ( { rawJSON , jsonValid : isJSONValid ( rawJSON ) } ) ;
}
handleSubmit = e => {
const { dispatch } = this . props ;
const { rawJSON } = this . state ;
const settings = JSON . parse ( rawJSON ) ;
this . setState ( { isLoading : true } ) ;
dispatch ( patchMe ( {
pleroma _settings _store : {
[ FE _NAME ] : settings ,
} ,
} ) ) . then ( response => {
dispatch ( { type : SETTINGS _UPDATE , settings } ) ;
this . setState ( { isLoading : false } ) ;
} ) . catch ( error => {
dispatch ( showAlertForError ( error ) ) ;
this . setState ( { isLoading : false } ) ;
} ) ;
}
render ( ) {
const { intl } = this . props ;
const { rawJSON , jsonValid , isLoading } = this . state ;
return (
2022-03-21 11:09:01 -07:00
< Column label = { intl . formatMessage ( messages . heading ) } backHref = '/developers' >
< Form onSubmit = { this . handleSubmit } disabled = { ! jsonValid || isLoading } >
< FormGroup
hintText = { intl . formatMessage ( messages . hint ) }
errors = { jsonValid ? [ ] : [ 'is invalid' ] }
>
< Textarea
2022-01-07 13:58:01 -08:00
value = { rawJSON }
onChange = { this . handleEditJSON }
disabled = { isLoading }
rows = { 12 }
2022-03-21 11:09:01 -07:00
isCodeEditor
2022-01-07 13:58:01 -08:00
/ >
2022-03-21 11:09:01 -07:00
< / F o r m G r o u p >
< FormActions >
< Button theme = 'primary' type = 'submit' disabled = { ! jsonValid || isLoading } >
2022-01-07 13:58:01 -08:00
< FormattedMessage id = 'soapbox_config.save' defaultMessage = 'Save' / >
2022-03-21 11:09:01 -07:00
< / B u t t o n >
< / F o r m A c t i o n s >
< / F o r m >
2022-01-07 13:58:01 -08:00
< / C o l u m n >
) ;
}
}