2020-07-20 16:18:54 -07:00
import React from 'react' ;
import { connect } from 'react-redux' ;
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
import ImmutablePureComponent from 'react-immutable-pure-component' ;
import PropTypes from 'prop-types' ;
import ImmutablePropTypes from 'react-immutable-proptypes' ;
import Column from '../ui/components/column' ;
import {
SimpleForm ,
FieldsGroup ,
2020-07-21 06:30:51 -07:00
TextInput ,
Checkbox ,
FileChooser ,
2020-08-23 21:19:56 -07:00
SimpleTextarea ,
2020-08-02 13:41:26 -07:00
ColorWithPicker ,
2020-08-02 12:07:13 -07:00
FileChooserLogo ,
2020-07-20 16:18:54 -07:00
} from 'soapbox/features/forms' ;
2020-08-23 21:19:56 -07:00
import { Map as ImmutableMap , List as ImmutableList , fromJS } from 'immutable' ;
2020-08-23 10:48:45 -07:00
import { updateAdminConfig } from 'soapbox/actions/admin' ;
2020-08-23 13:04:32 -07:00
import Icon from 'soapbox/components/icon' ;
2020-08-23 18:29:29 -07:00
import { defaultConfig } from 'soapbox/actions/soapbox' ;
2020-08-23 19:35:33 -07:00
import { uploadMedia } from 'soapbox/actions/media' ;
2020-07-20 16:18:54 -07:00
const messages = defineMessages ( {
2020-08-23 13:21:19 -07:00
heading : { id : 'column.soapbox_config' , defaultMessage : 'Soapbox config' } ,
copyrightFooterLabel : { id : 'soapbox_config.copyright_footer.meta_fields.label_placeholder' , defaultMessage : 'Copyright footer' } ,
promoItemIcon : { id : 'soapbox_config.promo_panel.meta_fields.icon_placeholder' , defaultMessage : 'Icon' } ,
promoItemLabel : { id : 'soapbox_config.promo_panel.meta_fields.label_placeholder' , defaultMessage : 'Label' } ,
promoItemURL : { id : 'soapbox_config.promo_panel.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
homeFooterItemLabel : { id : 'soapbox_config.home_footer.meta_fields.label_placeholder' , defaultMessage : 'Label' } ,
homeFooterItemURL : { id : 'soapbox_config.home_footer.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
customCssLabel : { id : 'soapbox_config.custom_css.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
2020-08-23 21:19:56 -07:00
rawJSONLabel : { id : 'soapbox_config.raw_json_label' , defaultMessage : 'Raw JSON data' } ,
rawJSONHint : { id : 'soapbox_config.raw_json_hint' , defaultMessage : 'Advanced: Edit the settings data directly. You need to paste it in for now.' } ,
2020-07-20 16:18:54 -07:00
} ) ;
2020-08-23 18:29:29 -07:00
const templates = {
promoPanelItem : ImmutableMap ( { icon : '' , text : '' , url : '' } ) ,
footerItem : ImmutableMap ( { title : '' , url : '' } ) ,
} ;
2020-08-23 13:56:18 -07:00
const mapStateToProps = state => ( {
2020-08-23 18:29:29 -07:00
soapbox : state . get ( 'soapbox' ) ,
2020-08-23 13:56:18 -07:00
} ) ;
2020-07-21 06:30:51 -07:00
2020-07-20 16:18:54 -07:00
export default @ connect ( mapStateToProps )
@ injectIntl
2020-08-23 13:16:04 -07:00
class SoapboxConfig extends ImmutablePureComponent {
2020-07-20 16:18:54 -07:00
static propTypes = {
2020-08-23 18:29:29 -07:00
soapbox : ImmutablePropTypes . map . isRequired ,
2020-07-20 16:18:54 -07:00
dispatch : PropTypes . func . isRequired ,
intl : PropTypes . object . isRequired ,
} ;
2020-07-21 06:30:51 -07:00
state = {
isLoading : false ,
2020-08-23 18:29:29 -07:00
soapbox : this . props . soapbox ,
2020-07-21 06:30:51 -07:00
}
2020-08-23 18:29:29 -07:00
setConfig = ( path , value ) => {
const { soapbox } = this . state ;
const config = soapbox . setIn ( path , value ) ;
this . setState ( { soapbox : config } ) ;
} ;
2020-07-23 07:14:28 -07:00
2020-08-23 21:19:56 -07:00
putConfig = config => {
this . setState ( { soapbox : config } ) ;
} ;
2020-07-21 06:30:51 -07:00
getParams = ( ) => {
2020-08-23 18:29:29 -07:00
const { soapbox } = this . state ;
return {
2020-07-29 17:09:44 -07:00
configs : [ {
group : ':pleroma' ,
key : ':frontend_configurations' ,
value : [ {
2020-08-23 18:29:29 -07:00
tuple : [ ':soapbox_fe' , soapbox . toJSON ( ) ] ,
2020-07-29 17:09:44 -07:00
} ] ,
} ] ,
} ;
2020-07-21 06:30:51 -07:00
}
handleSubmit = ( event ) => {
2020-07-20 16:18:54 -07:00
const { dispatch } = this . props ;
2020-08-23 10:48:45 -07:00
dispatch ( updateAdminConfig ( this . getParams ( ) ) ) . then ( ( ) => {
2020-07-21 06:30:51 -07:00
this . setState ( { isLoading : false } ) ;
} ) . catch ( ( error ) => {
this . setState ( { isLoading : false } ) ;
} ) ;
this . setState ( { isLoading : true } ) ;
event . preventDefault ( ) ;
}
2020-08-23 18:29:29 -07:00
handleChange = ( path , getValue ) => {
return e => {
this . setConfig ( path , getValue ( e ) ) ;
2020-07-23 07:14:28 -07:00
} ;
2020-08-23 18:29:29 -07:00
} ;
2020-07-23 07:14:28 -07:00
2020-08-23 19:35:33 -07:00
handleFileChange = path => {
return e => {
const data = new FormData ( ) ;
data . append ( 'file' , e . target . files [ 0 ] ) ;
this . props . dispatch ( uploadMedia ( data ) ) . then ( ( { data } ) => {
this . handleChange ( path , e => data . url ) ( e ) ;
} ) . catch ( ( ) => { } ) ;
} ;
} ;
2020-08-23 18:29:29 -07:00
handleAddItem = ( path , template ) => {
return e => {
this . setConfig (
path ,
this . getSoapboxConfig ( ) . getIn ( path , ImmutableList ( ) ) . push ( template ) ,
) ;
2020-07-24 17:30:33 -07:00
} ;
2020-08-23 18:29:29 -07:00
} ;
2020-07-21 06:30:51 -07:00
2020-08-23 20:41:22 -07:00
handleDeleteItem = path => {
return e => {
const soapbox = this . state . soapbox . deleteIn ( path ) ;
this . setState ( { soapbox } ) ;
} ;
} ;
2020-08-23 18:29:29 -07:00
handleItemChange = ( path , key , field , template ) => {
return this . handleChange (
path , ( e ) =>
template
. merge ( field )
. set ( key , e . target . value )
) ;
} ;
2020-07-20 16:18:54 -07:00
2020-08-23 18:29:29 -07:00
handlePromoItemChange = ( index , key , field ) => {
return this . handleItemChange (
[ 'promoPanel' , 'items' , index ] , key , field , templates . promoPanelItem
) ;
} ;
2020-08-11 04:30:54 -07:00
2020-08-23 18:29:29 -07:00
handleHomeFooterItemChange = ( index , key , field ) => {
return this . handleItemChange (
[ 'navlinks' , 'homeFooter' , index ] , key , field , templates . footerItem
) ;
} ;
2020-07-25 21:31:39 -07:00
2020-08-23 21:19:56 -07:00
handleEditJSON = e => {
try {
const data = fromJS ( JSON . parse ( e . target . value ) ) ;
this . putConfig ( data ) ;
} catch {
// do nothing
}
}
2020-08-23 18:29:29 -07:00
getSoapboxConfig = ( ) => {
return defaultConfig . mergeDeep ( this . state . soapbox ) ;
2020-07-25 21:31:39 -07:00
}
2020-08-23 18:29:29 -07:00
componentDidUpdate ( prevProps , prevState ) {
if ( prevProps . soapbox !== this . props . soapbox ) {
2020-08-23 21:19:56 -07:00
this . putConfig ( this . props . soapbox ) ;
2020-08-23 18:29:29 -07:00
}
2020-07-25 21:31:39 -07:00
}
2020-07-20 16:18:54 -07:00
render ( ) {
2020-08-12 18:52:32 -07:00
const { intl } = this . props ;
2020-08-23 18:29:29 -07:00
const soapbox = this . getSoapboxConfig ( ) ;
2020-07-20 16:18:54 -07:00
return (
2020-08-23 19:38:51 -07:00
< Column icon = 'cog' heading = { intl . formatMessage ( messages . heading ) } backBtnSlim >
2020-07-21 06:30:51 -07:00
< SimpleForm onSubmit = { this . handleSubmit } >
< fieldset disabled = { this . state . isLoading } >
< FieldsGroup >
2020-08-23 19:35:33 -07:00
< div className = 'fields-row file-picker' >
2020-07-21 06:30:51 -07:00
< div className = 'fields-row__column fields-row__column-6' >
2020-08-23 18:29:29 -07:00
< img src = { soapbox . get ( 'logo' ) } / >
2020-07-21 06:30:51 -07:00
< / d i v >
< div className = 'fields-row__column fields-group fields-row__column-6' >
2020-08-02 12:07:13 -07:00
< FileChooserLogo
2020-08-23 13:21:19 -07:00
label = { < FormattedMessage id = 'soapbox_config.fields.logo_label' defaultMessage = 'Logo' / > }
2020-07-23 17:45:34 -07:00
name = 'logo'
2020-08-23 20:44:13 -07:00
hint = { < FormattedMessage id = 'soapbox_config.hints.logo' defaultMessage = 'SVG. At most 2 MB. Will be displayed to 50px height, maintaining aspect ratio' / > }
2020-08-23 19:35:33 -07:00
onChange = { this . handleFileChange ( [ 'logo' ] ) }
2020-07-21 06:30:51 -07:00
/ >
2020-07-24 17:30:33 -07:00
< / d i v >
< / d i v >
2020-08-23 19:35:33 -07:00
< div className = 'fields-row file-picker' >
2020-07-24 17:30:33 -07:00
< div className = 'fields-row__column fields-row__column-6' >
2020-08-23 18:29:29 -07:00
< img src = { soapbox . get ( 'banner' ) } / >
2020-07-24 17:30:33 -07:00
< / d i v >
< div className = 'fields-row__column fields-group fields-row__column-6' >
2020-07-21 06:30:51 -07:00
< FileChooser
2020-08-23 13:21:19 -07:00
label = { < FormattedMessage id = 'soapbox_config.fields.banner_label' defaultMessage = 'Banner' / > }
2020-07-23 17:45:34 -07:00
name = 'banner'
2020-08-23 20:44:13 -07:00
hint = { < FormattedMessage id = 'soapbox_config.hints.banner' defaultMessage = 'PNG, GIF or JPG. At most 2 MB. Will be displayed to 400x400px' / > }
2020-08-23 19:35:33 -07:00
onChange = { this . handleFileChange ( [ 'banner' ] ) }
2020-07-21 06:30:51 -07:00
/ >
< / d i v >
< / d i v >
2020-07-24 17:30:33 -07:00
< / F i e l d s G r o u p >
< FieldsGroup >
2020-08-02 13:41:26 -07:00
< div className = 'fields-row__column fields-group' >
< ColorWithPicker
buttonId = 'brand_color'
2020-08-23 13:21:19 -07:00
label = { < FormattedMessage id = 'soapbox_config.fields.brand_color_label' defaultMessage = 'Brand color' / > }
2020-08-23 18:29:29 -07:00
value = { soapbox . get ( 'brandColor' ) }
onChange = { this . handleChange ( [ 'brandColor' ] , ( e ) => e . hex ) }
2020-08-02 13:41:26 -07:00
/ >
2020-07-24 17:30:33 -07:00
< / d i v >
< / F i e l d s G r o u p >
< FieldsGroup >
2020-07-21 06:30:51 -07:00
< Checkbox
2020-08-23 13:21:19 -07:00
label = { < FormattedMessage id = 'soapbox_config.fields.patron_enabled_label' defaultMessage = 'Patron module' / > }
hint = { < FormattedMessage id = 'soapbox_config.hints.patron_enabled' defaultMessage = 'Enables display of Patron module. Requires installation of Patron module.' / > }
2020-08-09 09:03:24 -07:00
name = 'patron'
2020-08-23 18:29:29 -07:00
checked = { soapbox . getIn ( [ 'extensions' , 'patron' , 'enabled' ] ) }
onChange = { this . handleChange (
[ 'extensions' , 'patron' , 'enabled' ] , ( e ) => e . checked ,
) }
2020-07-20 16:18:54 -07:00
/ >
2020-07-21 06:30:51 -07:00
< / F i e l d s G r o u p >
2020-08-02 17:44:04 -07:00
< FieldsGroup >
< TextInput
name = 'copyright'
label = { intl . formatMessage ( messages . copyrightFooterLabel ) }
placeholder = { intl . formatMessage ( messages . copyrightFooterLabel ) }
2020-08-23 18:29:29 -07:00
value = { soapbox . get ( 'copyright' ) }
onChange = { this . handleChange ( [ 'copyright' ] , ( e ) => e . target . value ) }
2020-08-02 17:44:04 -07:00
/ >
< / F i e l d s G r o u p >
2020-08-12 15:24:14 -07:00
< FieldsGroup >
2020-07-21 06:30:51 -07:00
< div className = 'fields-row__column fields-group' >
< div className = 'input with_block_label' >
2020-08-23 13:21:19 -07:00
< label > < FormattedMessage id = 'soapbox_config.fields.promo_panel_fields_label' defaultMessage = 'Promo panel items' / > < / l a b e l >
2020-07-21 06:30:51 -07:00
< span className = 'hint' >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.hints.promo_panel_fields' defaultMessage = 'You can have custom defined links displayed on the left panel of the timelines page.' / >
2020-07-25 16:27:39 -07:00
< / s p a n >
< span className = 'hint' >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.hints.promo_panel_icons' defaultMessage = '{ link }' values = { { link : < a target = '_blank' href = 'https://forkaweso.me/Fork-Awesome/icons/' > Soapbox Icons List < /a> }} / >
2020-07-21 06:30:51 -07:00
< / s p a n >
{
2020-08-23 18:29:29 -07:00
soapbox . getIn ( [ 'promoPanel' , 'items' ] ) . map ( ( field , i ) => (
2020-07-21 06:30:51 -07:00
< div className = 'row' key = { i } >
< TextInput
2020-07-24 17:30:33 -07:00
label = { intl . formatMessage ( messages . promoItemIcon ) }
placeholder = { intl . formatMessage ( messages . promoItemIcon ) }
value = { field . get ( 'icon' ) }
2020-08-23 18:29:29 -07:00
onChange = { this . handlePromoItemChange ( i , 'icon' , field ) }
2020-07-21 06:30:51 -07:00
/ >
< TextInput
2020-07-24 17:30:33 -07:00
label = { intl . formatMessage ( messages . promoItemLabel ) }
placeholder = { intl . formatMessage ( messages . promoItemLabel ) }
value = { field . get ( 'text' ) }
2020-08-23 18:29:29 -07:00
onChange = { this . handlePromoItemChange ( i , 'text' , field ) }
2020-07-24 17:30:33 -07:00
/ >
< TextInput
label = { intl . formatMessage ( messages . promoItemURL ) }
placeholder = { intl . formatMessage ( messages . promoItemURL ) }
value = { field . get ( 'url' ) }
2020-08-23 18:29:29 -07:00
onChange = { this . handlePromoItemChange ( i , 'url' , field ) }
2020-07-21 06:30:51 -07:00
/ >
2020-08-23 20:41:22 -07:00
< a onClick = { this . handleDeleteItem ( [ 'promoPanel' , 'items' , i ] ) } > Delete < / a >
2020-07-21 06:30:51 -07:00
< / d i v >
) )
}
2020-07-24 17:30:33 -07:00
< div className = 'actions' >
2020-08-23 18:29:29 -07:00
< div name = 'button' type = 'button' role = 'presentation' className = 'btn button button-secondary' onClick = { this . handleAddItem ( [ 'promoPanel' , 'items' ] , templates . promoPanelItem ) } >
2020-08-23 13:04:32 -07:00
< Icon id = 'plus-circle' / >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.fields.promo_panel.add' defaultMessage = 'Add new Promo panel item' / >
2020-08-10 17:29:38 -07:00
< / d i v >
2020-07-24 17:30:33 -07:00
< / d i v >
2020-07-21 06:30:51 -07:00
< / d i v >
2020-07-23 07:14:28 -07:00
< div className = 'input with_block_label' >
2020-08-23 13:21:19 -07:00
< label > < FormattedMessage id = 'soapbox_config.fields.home_footer_fields_label' defaultMessage = 'Home footer items' / > < / l a b e l >
2020-07-23 07:14:28 -07:00
< span className = 'hint' >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.hints.home_footer_fields' defaultMessage = 'You can have custom defined links displayed on the footer of your static pages' / >
2020-07-23 07:14:28 -07:00
< / s p a n >
{
2020-08-23 18:29:29 -07:00
soapbox . getIn ( [ 'navlinks' , 'homeFooter' ] ) . map ( ( field , i ) => (
2020-07-23 07:14:28 -07:00
< div className = 'row' key = { i } >
< TextInput
2020-07-24 17:30:33 -07:00
label = { intl . formatMessage ( messages . homeFooterItemLabel ) }
placeholder = { intl . formatMessage ( messages . homeFooterItemLabel ) }
value = { field . get ( 'title' ) }
2020-08-23 18:29:29 -07:00
onChange = { this . handleHomeFooterItemChange ( i , 'title' , field ) }
2020-07-23 07:14:28 -07:00
/ >
< TextInput
2020-07-24 17:30:33 -07:00
label = { intl . formatMessage ( messages . homeFooterItemURL ) }
placeholder = { intl . formatMessage ( messages . homeFooterItemURL ) }
value = { field . get ( 'url' ) }
2020-08-23 18:29:29 -07:00
onChange = { this . handleHomeFooterItemChange ( i , 'url' , field ) }
2020-07-24 17:30:33 -07:00
/ >
2020-08-23 20:41:22 -07:00
< a onClick = { this . handleDeleteItem ( [ 'navlinks' , 'homeFooter' , i ] ) } > Delete < / a >
2020-07-24 17:30:33 -07:00
< / d i v >
) )
}
< div className = 'actions' >
2020-08-23 18:29:29 -07:00
< div name = 'button' type = 'button' role = 'presentation' className = 'btn button button-secondary' onClick = { this . handleAddItem ( [ 'navlinks' , 'homeFooter' ] , templates . footerItem ) } >
2020-08-23 13:04:32 -07:00
< Icon id = 'plus-circle' / >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.fields.home_footer.add' defaultMessage = 'Add new Home Footer Item' / >
2020-08-10 17:29:38 -07:00
< / d i v >
2020-07-24 17:30:33 -07:00
< / d i v >
< / d i v >
< / d i v >
2020-07-25 13:25:33 -07:00
< div className = 'input with_block_label' >
2020-08-23 13:21:19 -07:00
< label > < FormattedMessage id = 'soapbox_config.fields.custom_css_fields_label' defaultMessage = 'Custom CSS' / > < / l a b e l >
2020-07-25 07:55:01 -07:00
< span className = 'hint' >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.hints.custom_css_fields' defaultMessage = 'Insert a URL to a CSS file like `https://mysite.com/instance/custom.css`, or simply `/instance/custom.css`' / >
2020-07-25 07:55:01 -07:00
< / s p a n >
{
2020-08-23 18:29:29 -07:00
soapbox . get ( 'customCss' ) . map ( ( field , i ) => (
2020-07-25 07:55:01 -07:00
< div className = 'row' key = { i } >
< TextInput
label = { intl . formatMessage ( messages . customCssLabel ) }
placeholder = { intl . formatMessage ( messages . customCssLabel ) }
2020-07-27 17:16:02 -07:00
value = { field }
2020-08-23 18:29:29 -07:00
onChange = { this . handleChange ( [ 'customCss' , i ] , ( e ) => e . target . value ) }
2020-07-25 07:55:01 -07:00
/ >
2020-08-23 20:41:22 -07:00
< a onClick = { this . handleDeleteItem ( [ 'customCss' , i ] ) } > Delete < / a >
2020-07-25 07:55:01 -07:00
< / d i v >
) )
}
< div className = 'actions' >
2020-08-23 18:29:29 -07:00
< div name = 'button' type = 'button' role = 'presentation' className = 'btn button button-secondary' onClick = { this . handleAddItem ( [ 'customCss' ] , '' ) } >
2020-08-23 13:04:32 -07:00
< Icon id = 'plus-circle' / >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.fields.custom_css.add' defaultMessage = 'Add another custom CSS URL' / >
2020-08-10 17:29:38 -07:00
< / d i v >
2020-07-23 07:14:28 -07:00
< / d i v >
2020-07-21 06:30:51 -07:00
< / d i v >
2020-08-12 15:24:14 -07:00
< / F i e l d s G r o u p >
2020-08-23 21:19:56 -07:00
< FieldsGroup >
< div className = 'code-editor' >
< SimpleTextarea
label = { intl . formatMessage ( messages . rawJSONLabel ) }
hint = { intl . formatMessage ( messages . rawJSONHint ) }
value = { JSON . stringify ( this . state . soapbox , null , 2 ) }
onChange = { this . handleEditJSON }
rows = { 12 }
/ >
< / d i v >
< / F i e l d s G r o u p >
2020-07-21 06:30:51 -07:00
< / f i e l d s e t >
< div className = 'actions' >
< button name = 'button' type = 'submit' className = 'btn button button-primary' >
2020-08-23 13:21:19 -07:00
< FormattedMessage id = 'soapbox_config.save' defaultMessage = 'Save' / >
2020-07-21 06:30:51 -07:00
< / b u t t o n >
< / d i v >
2020-07-20 16:18:54 -07:00
< / S i m p l e F o r m >
< / C o l u m n >
) ;
}
}