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-07-20 16:18:54 -07:00
} from 'soapbox/features/forms' ;
2020-07-22 17:31:41 -07:00
// import BrandingPreview from './components/branding_preview';
import StillImage from 'soapbox/components/still_image' ;
2020-07-21 06:30:51 -07:00
import {
Map as ImmutableMap ,
2020-07-25 07:55:01 -07:00
List as ImmutableList ,
2020-07-21 06:30:51 -07:00
} from 'immutable' ;
import { patchMe } from 'soapbox/actions/me' ;
2020-07-22 19:59:52 -07:00
//import { unescape } from 'lodash';
2020-07-22 17:31:41 -07:00
import { generateThemeCss } from 'soapbox/utils/theme' ;
2020-07-20 16:18:54 -07:00
const messages = defineMessages ( {
2020-07-21 16:38:10 -07:00
heading : { id : 'column.soapbox_settings' , defaultMessage : 'Soapbox settings' } ,
2020-07-23 17:45:34 -07:00
promoItemIcon : { id : 'soapbox_settings.promo_panel.meta_fields.icon_placeholder' , defaultMessage : 'Icon' } ,
promoItemLabel : { id : 'soapbox_settings.promo_panel.meta_fields.label_placeholder' , defaultMessage : 'Label' } ,
promoItemURL : { id : 'soapbox_settings.promo_panel.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
homeFooterItemLabel : { id : 'soapbox_settings.home_footer.meta_fields.label_placeholder' , defaultMessage : 'Label' } ,
homeFooterItemURL : { id : 'soapbox_settings.home_footer.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
2020-07-24 17:30:33 -07:00
customCssLabel : { id : 'soapbox_settings.custom_css.meta_fields.label_placeholder' , defaultMessage : 'CSS' } ,
2020-07-20 16:18:54 -07:00
} ) ;
2020-07-21 06:30:51 -07:00
const mapStateToProps = state => {
const soapbox = state . get ( 'soapbox' ) ;
return {
2020-07-22 17:31:41 -07:00
themeCss : generateThemeCss ( soapbox . get ( 'brandColor' ) ) ,
2020-07-25 07:55:01 -07:00
brandColor : soapbox . get ( 'brandColor' ) ,
2020-07-24 17:30:33 -07:00
customCssItems : soapbox . getIn ( [ 'customCSS' , 'items' ] ) ,
2020-07-22 17:31:41 -07:00
logo : soapbox . get ( 'logo' ) ,
2020-07-24 17:30:33 -07:00
banner : soapbox . get ( 'banner' ) ,
2020-07-23 17:45:34 -07:00
promoItems : soapbox . getIn ( [ 'promoPanel' , 'items' ] ) ,
2020-07-23 07:14:28 -07:00
patronEnabled : soapbox . getIn ( [ 'extensions' , 'patron' ] ) ,
autoPlayGif : soapbox . getIn ( [ 'defaultSettings' , 'autoPlayGif' ] ) ,
2020-07-22 17:31:41 -07:00
copyright : soapbox . get ( 'copyright' ) ,
2020-07-23 17:45:34 -07:00
homeFooterItems : soapbox . getIn ( [ 'navlinks' , 'homeFooter' ] ) ,
2020-07-21 06:30:51 -07:00
} ;
} ;
2020-07-20 16:18:54 -07:00
export default @ connect ( mapStateToProps )
2020-07-22 17:31:41 -07:00
// export default @connect()
2020-07-20 16:18:54 -07:00
@ injectIntl
2020-07-21 06:30:51 -07:00
class ConfigSoapbox extends ImmutablePureComponent {
2020-07-20 16:18:54 -07:00
static propTypes = {
dispatch : PropTypes . func . isRequired ,
intl : PropTypes . object . isRequired ,
2020-07-22 17:31:41 -07:00
themeCss : PropTypes . string ,
2020-07-25 07:55:01 -07:00
brandColor : PropTypes . string ,
2020-07-24 17:30:33 -07:00
customCssItems : ImmutablePropTypes . list ,
2020-07-22 17:31:41 -07:00
logo : PropTypes . string ,
2020-07-24 17:30:33 -07:00
banner : PropTypes . string ,
2020-07-23 17:45:34 -07:00
promoItems : ImmutablePropTypes . list ,
2020-07-23 07:14:28 -07:00
patronEnabled : PropTypes . bool ,
autoPlayGif : PropTypes . bool ,
2020-07-22 17:31:41 -07:00
copyright : PropTypes . string ,
2020-07-23 17:45:34 -07:00
homeFooterItems : ImmutablePropTypes . list ,
2020-07-20 16:18:54 -07:00
} ;
2020-07-21 06:30:51 -07:00
state = {
isLoading : false ,
}
2020-07-24 17:30:33 -07:00
constructor ( props ) {
super ( props ) ;
this . state = {
logo : props . logo ,
banner : props . banner ,
promoItems : props . promoItems ,
homeFooterItems : props . homeFooterItems ,
customCssItems : props . customCssItems ,
} ;
2020-07-25 07:55:01 -07:00
if ( ! this . state . logo ) {
this . state . logo = '' ;
}
if ( ! this . state . banner ) {
this . state . banner = '' ;
}
if ( ! this . state . promoItems ) {
this . state . promoItems = ImmutableList ( [
ImmutableMap ( {
icon : '' ,
text : '' ,
url : '' ,
} ) ,
] ) ;
} ;
if ( ! this . state . homeFooterItems ) {
this . state . homeFooterItems = ImmutableList ( [
ImmutableMap ( {
title : '' ,
url : '' ,
} ) ,
] ) ;
} ;
if ( ! this . state . customCssItems ) {
this . state . customCssItems = ImmutableList ( [
ImmutableMap ( {
css : '' ,
} ) ,
] ) ;
} ;
2020-07-25 13:25:33 -07:00
this . handlecustomCSSChange = this . handleCustomCSSChange . bind ( this ) ;
2020-07-24 17:30:33 -07:00
}
2020-07-21 06:30:51 -07:00
2020-07-23 17:45:34 -07:00
getPromoItemsParams = ( ) => {
2020-07-21 06:30:51 -07:00
let params = ImmutableMap ( ) ;
2020-07-23 17:45:34 -07:00
this . state . promoItems . forEach ( ( f , i ) =>
2020-07-21 06:30:51 -07:00
params = params
2020-07-24 17:30:33 -07:00
. set ( ` promo_panel_attributes[ ${ i } ][icon] ` , f . get ( 'icon' ) )
. set ( ` promo_panel_attributes[ ${ i } ][text] ` , f . get ( 'text' ) )
. set ( ` promo_panel_attributes[ ${ i } ][url] ` , f . get ( 'url' ) )
2020-07-21 06:30:51 -07:00
) ;
return params ;
}
2020-07-23 07:14:28 -07:00
getHomeFooterParams = ( ) => {
let params = ImmutableMap ( ) ;
2020-07-23 17:45:34 -07:00
this . state . homeFooterItems . forEach ( ( f , i ) =>
2020-07-23 07:14:28 -07:00
params = params
2020-07-24 17:30:33 -07:00
. set ( ` home_footer_attributes[ ${ i } ][title] ` , f . get ( 'title' ) )
. set ( ` home_footer_attributes[ ${ i } ][url] ` , f . get ( 'url' ) )
) ;
return params ;
}
getCustomCssParams = ( ) => {
let params = ImmutableMap ( ) ;
this . state . customCssItems . forEach ( ( f , i ) =>
params = params
. set ( ` custom_css_attributes[ ${ i } ][css] ` , f . get ( 'css' ) )
2020-07-23 07:14:28 -07:00
) ;
return params ;
}
2020-07-21 06:30:51 -07:00
getParams = ( ) => {
const { state } = this ;
return Object . assign ( {
2020-07-21 16:38:10 -07:00
themeCss : state . themeCss ,
logoFile : state . logoFile ,
patronEnabled : state . patronEnabled ,
displayMode : state . displayMode ,
copyright : state . copyright ,
2020-07-23 07:14:28 -07:00
} ,
this . getHomeFooterParams ( ) . toJS ( ) ,
2020-07-24 17:30:33 -07:00
this . getPromoItemsParams ( ) . toJS ( ) ) ,
this . getCustomCSSParams ( ) . toJS ( ) ;
2020-07-21 06:30:51 -07:00
}
2020-07-20 16:18:54 -07:00
2020-07-21 06:30:51 -07:00
getFormdata = ( ) => {
const data = this . getParams ( ) ;
let formData = new FormData ( ) ;
for ( let key in data ) {
2020-07-24 17:30:33 -07:00
const shouldAppend = Boolean ( data [ key ] || key . startsWith ( 'promo_panel_attributes' ) || key . startsWith ( 'home_footer_attributes' ) || key . startsWith ( 'custom_css_attributes' ) ) ;
2020-07-21 06:30:51 -07:00
if ( shouldAppend ) formData . append ( key , data [ key ] || '' ) ;
}
return formData ;
}
handleSubmit = ( event ) => {
2020-07-20 16:18:54 -07:00
const { dispatch } = this . props ;
2020-07-21 06:30:51 -07:00
dispatch ( patchMe ( this . getFormdata ( ) ) ) . then ( ( ) => {
this . setState ( { isLoading : false } ) ;
} ) . catch ( ( error ) => {
this . setState ( { isLoading : false } ) ;
} ) ;
this . setState ( { isLoading : true } ) ;
event . preventDefault ( ) ;
}
handleCheckboxChange = e => {
this . setState ( { [ e . target . name ] : e . target . checked } ) ;
}
2020-07-25 07:55:01 -07:00
handleBrandColorChange = e => {
this . setState ( { brandColor : e . target . value } ) ;
}
2020-07-21 06:30:51 -07:00
handleTextChange = e => {
this . setState ( { [ e . target . name ] : e . target . value } ) ;
}
2020-07-23 17:45:34 -07:00
handlePromoItemsChange = ( i , key ) => {
2020-07-21 06:30:51 -07:00
return ( e ) => {
this . setState ( {
2020-07-23 17:45:34 -07:00
promoItems : this . state . promoItems . setIn ( [ i , key ] , e . target . value ) ,
2020-07-21 06:30:51 -07:00
} ) ;
} ;
}
2020-07-23 17:45:34 -07:00
handleHomeFooterItemsChange = ( i , key ) => {
2020-07-23 07:14:28 -07:00
return ( e ) => {
this . setState ( {
2020-07-23 17:45:34 -07:00
homeFooterItems : this . state . homeFooterItems . setIn ( [ i , key ] , e . target . value ) ,
2020-07-23 07:14:28 -07:00
} ) ;
} ;
}
2020-07-24 17:30:33 -07:00
handleCustomCSSChange = ( i , key ) => {
return ( e ) => {
this . setState ( {
customCssItems : this . state . customCssItems . setIn ( [ i , key ] , e . target . value ) ,
} ) ;
} ;
}
2020-07-21 06:30:51 -07:00
handleFileChange = e => {
const { name } = e . target ;
const [ file ] = e . target . files || [ ] ;
const url = file ? URL . createObjectURL ( file ) : this . state [ name ] ;
this . setState ( {
[ name ] : url ,
[ ` ${ name } _file ` ] : file ,
} ) ;
2020-07-20 16:18:54 -07:00
}
render ( ) {
2020-07-21 06:30:51 -07:00
const { intl } = this . props ;
2020-07-20 16:18:54 -07:00
return (
2020-07-24 17:30:33 -07:00
< Column icon = 'shield' heading = { intl . formatMessage ( messages . heading ) } backBtnSlim >
2020-07-21 06:30:51 -07:00
< SimpleForm onSubmit = { this . handleSubmit } >
< fieldset disabled = { this . state . isLoading } >
< FieldsGroup >
< div className = 'fields-row' >
< div className = 'fields-row__column fields-row__column-6' >
2020-07-23 21:04:29 -07:00
{ this . state . logo ? ( < StillImage src = { this . state . logo } / > ) : ( < StillImage src = { this . props . logo } / > ) }
2020-07-21 06:30:51 -07:00
< / d i v >
< div className = 'fields-row__column fields-group fields-row__column-6' >
< FileChooser
2020-07-23 17:45:34 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.logo_label' defaultMessage = 'Logo' / > }
name = 'logo'
hint = { < FormattedMessage id = 'soapbox_settings.hints.logo' defaultMessage = 'SVG. At most 2 MB. Will be downscaled to 50px height, maintaining aspect ratio' / > }
2020-07-21 06:30:51 -07:00
onChange = { this . handleFileChange }
/ >
2020-07-24 17:30:33 -07:00
< / d i v >
< / d i v >
< div className = 'fields-row' >
< div className = 'fields-row__column fields-row__column-6' >
{ this . state . banner ? ( < StillImage src = { this . state . banner } / > ) : ( < StillImage src = { this . props . banner } / > ) }
< / d i v >
< div className = 'fields-row__column fields-group fields-row__column-6' >
2020-07-21 06:30:51 -07:00
< FileChooser
2020-07-23 17:45:34 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.banner_label' defaultMessage = 'Banner' / > }
name = 'banner'
hint = { < FormattedMessage id = 'soapbox_settings.hints.banner' defaultMessage = 'PNG, GIF or JPG. At most 2 MB. Will be downscaled to 400x400px' / > }
2020-07-21 06:30:51 -07:00
onChange = { this . handleFileChange }
/ >
< / 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 >
< div >
2020-07-25 07:55:01 -07:00
< label htmlFor = 'brand_color' > < FormattedMessage id = 'soapbox_settings.fields.brand_color_label' defaultMessage = 'Brand color' / > < /label><br / > < br / >
2020-07-25 13:25:33 -07:00
< input type = 'color' id = 'brand_color' name = 'brand_color' value = { this . state . brandColor || '#0482d8' } onChange = { this . handleBrandColorChange } / > < br / > < br / >
2020-07-25 07:55:01 -07:00
< label > { this . state . brandColor } < / l a b e l >
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-07-24 17:30:33 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.patron_enabled_label' defaultMessage = 'Patron module' / > }
2020-07-23 17:45:34 -07:00
hint = { < FormattedMessage id = 'soapbox_settings.hints.patron_enabled' defaultMessage = 'Enables display of Patron module. Requires installation of Patron module.' / > }
name = 'patron_enabled'
2020-07-23 21:04:29 -07:00
checked = { this . state . patronEnabled ? this . state . patronEnabled : this . props . patronEnabled }
2020-07-21 06:30:51 -07:00
onChange = { this . handleCheckboxChange }
2020-07-20 16:18:54 -07:00
/ >
2020-07-21 06:30:51 -07:00
< Checkbox
2020-07-23 17:45:34 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.auto_play_gif_label' defaultMessage = 'Auto-play GIFs' / > }
hint = { < FormattedMessage id = 'soapbox_settings.hints.auto_play_gif' defaultMessage = 'Enable auto-playing of GIF files in timeline' / > }
name = 'auto_play_gif'
2020-07-23 21:04:29 -07:00
checked = { this . state . autoPlayGif ? this . state . autoPlayGif : this . props . autoPlayGif }
2020-07-21 06:30:51 -07:00
onChange = { this . handleCheckboxChange }
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 >
< FieldsGroup >
< div className = 'fields-row__column fields-group' >
< div className = 'input with_block_label' >
2020-07-24 17:30:33 -07:00
< label > < FormattedMessage id = 'soapbox_settings.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-07-24 17:30:33 -07:00
< FormattedMessage id = 'soapbox_settings.hints.promo_panel_fields' defaultMessage = 'You can have custom defined links displayed on the left panel of the timelines page' / >
2020-07-21 06:30:51 -07:00
< / s p a n >
{
2020-07-23 17:45:34 -07:00
this . state . promoItems . 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' ) }
onChange = { this . handlePromoItemsChange ( i , 'icon' ) }
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' ) }
onChange = { this . handlePromoItemsChange ( i , 'text' ) }
/ >
< TextInput
label = { intl . formatMessage ( messages . promoItemURL ) }
placeholder = { intl . formatMessage ( messages . promoItemURL ) }
value = { field . get ( 'url' ) }
onChange = { this . handlePromoItemsChange ( i , 'url' ) }
2020-07-21 06:30:51 -07:00
/ >
< / d i v >
) )
}
2020-07-24 17:30:33 -07:00
< div className = 'actions' >
< button name = 'button' type = 'submit' className = 'btn button button-secondary' >
< FormattedMessage id = 'soapbox_settings.fields.promo_panel.add' defaultMessage = 'Add new Promo panel item' / >
< / b u t t o n >
< / 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-07-24 17:30:33 -07:00
< label > < FormattedMessage id = 'soapbox_settings.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-07-24 17:30:33 -07:00
< FormattedMessage id = 'soapbox_settings.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-07-23 17:45:34 -07:00
this . state . homeFooterItems . 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' ) }
onChange = { this . handleHomeFooterItemsChange ( i , 'title' ) }
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' ) }
onChange = { this . handleHomeFooterItemsChange ( i , 'url' ) }
/ >
< / d i v >
) )
}
< div className = 'actions' >
< button name = 'button' type = 'submit' className = 'btn button button-secondary' >
< FormattedMessage id = 'soapbox_settings.fields.home_footer.add' defaultMessage = 'Add new Home Footer Item' / >
< / b u t t o n >
< / d i v >
< / d i v >
< / d i v >
2020-07-25 13:25:33 -07:00
< div className = 'input with_block_label' >
2020-07-25 07:55:01 -07:00
< label > < FormattedMessage id = 'soapbox_settings.fields.custom_css_fields_label' defaultMessage = 'Custom CSS' / > < / l a b e l >
< span className = 'hint' >
< FormattedMessage id = 'soapbox_settings.hints.custom_css_fields' defaultMessage = 'You can have custom CSS definitions' / >
< / s p a n >
{
this . state . customCssItems . map ( ( field , i ) => (
< div className = 'row' key = { i } >
< TextInput
label = { intl . formatMessage ( messages . customCssLabel ) }
placeholder = { intl . formatMessage ( messages . customCssLabel ) }
value = { field . get ( 'css' ) }
onChange = { this . handlecustomCSSChange ( i , 'css' ) }
/ >
< / d i v >
) )
}
< div className = 'actions' >
< button name = 'button' type = 'submit' className = 'btn button button-secondary' >
< FormattedMessage id = 'soapbox_settings.fields.custom_css.add' defaultMessage = 'Add new Custom CSS item' / >
< / b u t t o n >
2020-07-23 07:14:28 -07:00
< / d i v >
2020-07-21 06:30:51 -07:00
< / d i v >
< / F i e l d s G r o u p >
< / f i e l d s e t >
< div className = 'actions' >
< button name = 'button' type = 'submit' className = 'btn button button-primary' >
2020-07-22 17:31:41 -07:00
< FormattedMessage id = 'soapbox_settings.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 >
) ;
}
}