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-22 19:50:41 -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
2020-07-21 16:38:10 -07:00
const MAX _FIELDS = 6 ; // Max promoPanel fields
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' } ,
promoPanelIcon : { id : 'soapbox_settings.promo_panel.meta_fields.icon_placeholder' , defaultMessage : 'Icon' } ,
promoPanelLabel : { id : 'soapbox_settings.promo_panel.meta_fields.label_placeholder' , defaultMessage : 'Label' } ,
promoPanelURL : { id : 'soapbox_settings.promo_panel.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
homeFooterLabel : { id : 'soapbox_settings.home_footer.meta_fields.label_placeholder' , defaultMessage : 'Label' } ,
homeFooterURL : { id : 'soapbox_settings.home_footer.meta_fields.url_placeholder' , defaultMessage : 'URL' } ,
2020-07-20 16:18:54 -07:00
} ) ;
2020-07-21 06:30:51 -07:00
const mapStateToProps = state => {
const soapbox = state . get ( 'soapbox' ) ;
2020-07-22 17:31:41 -07:00
console . log ( soapbox ) ;
console . log ( generateThemeCss ( soapbox . get ( 'brandColor' ) ) ) ;
console . log ( soapbox . get ( 'logo' ) ) ;
console . log ( soapbox . get ( 'promoPanel' ) ) ;
console . log ( soapbox . get ( 'extensions' ) ) ;
console . log ( soapbox . get ( 'defaultSettings' ) ) ;
console . log ( soapbox . get ( 'copyright' ) ) ;
console . log ( soapbox . get ( 'navLinks' ) ) ;
2020-07-21 06:30:51 -07:00
return {
2020-07-22 17:31:41 -07:00
themeCss : generateThemeCss ( soapbox . get ( 'brandColor' ) ) ,
logo : soapbox . get ( 'logo' ) ,
promoPanel : soapbox . get ( 'promoPanel' ) ,
patronEnabled : soapbox . get ( 'extensions' ) ,
autoPlayGif : soapbox . get ( 'defaultSettings' ) ,
copyright : soapbox . get ( 'copyright' ) ,
homeFooter : soapbox . get ( 'navLinks' ) ,
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 ,
logo : PropTypes . string ,
2020-07-21 16:38:10 -07:00
promoPanel : ImmutablePropTypes . map ,
2020-07-22 17:31:41 -07:00
patronEnabled : PropTypes . object ,
autoPlayGif : PropTypes . object ,
copyright : PropTypes . string ,
2020-07-21 16:38:10 -07:00
homeFooter : ImmutablePropTypes . map ,
2020-07-20 16:18:54 -07:00
} ;
2020-07-21 06:30:51 -07:00
state = {
isLoading : false ,
2020-07-22 17:31:41 -07:00
// promoPanel: normalizeFields(Array.from({ length: MAX_FIELDS })),
2020-07-21 06:30:51 -07:00
}
2020-07-22 17:31:41 -07:00
// makePreviewLogo = () => {
// const { logo } = this.props;
// return logo.merge(ImmutableMap({
// header: this.state.header,
// avatar: this.state.avatar,
// display_name: this.state.display_name,
// }));
// }
2020-07-21 06:30:51 -07:00
2020-07-21 16:38:10 -07:00
getPromoPanelParams = ( ) => {
2020-07-21 06:30:51 -07:00
let params = ImmutableMap ( ) ;
2020-07-21 16:38:10 -07:00
this . state . promoPanel . forEach ( ( f , i ) =>
2020-07-21 06:30:51 -07:00
params = params
2020-07-21 16:38:10 -07:00
. set ( ` promo_panel_attributes[ ${ i } ][name] ` , f . get ( 'name' ) )
. set ( ` promo_panel_attributes[ ${ i } ][value] ` , f . get ( 'value' ) )
2020-07-21 06:30:51 -07:00
) ;
return params ;
}
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 ,
homeFooter : state . homeFooter ,
} , this . getPromoPanelParams ( ) . 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-21 16:38:10 -07:00
const shouldAppend = Boolean ( data [ key ] || key . startsWith ( 'promo_panel_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 } ) ;
}
handleTextChange = e => {
this . setState ( { [ e . target . name ] : e . target . value } ) ;
}
2020-07-21 16:38:10 -07:00
handlePromoPanelChange = ( i , key ) => {
2020-07-21 06:30:51 -07:00
return ( e ) => {
this . setState ( {
2020-07-22 17:31:41 -07:00
promoPanel : this . state . promoPanel . 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-21 16:38:10 -07:00
< Column icon = 'gears' heading = { intl . formatMessage ( messages . heading ) } backBtnSlim >
2020-07-21 06:30:51 -07:00
< SimpleForm onSubmit = { this . handleSubmit } >
< fieldset disabled = { this . state . isLoading } >
< FieldsGroup >
< TextInput
2020-07-22 17:31:41 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.display_name_label' defaultMessage = 'Display name' / > }
2020-07-21 06:30:51 -07:00
name = 'display_name'
value = { this . state . display _name }
onChange = { this . handleTextChange }
2020-07-20 16:18:54 -07:00
/ >
2020-07-21 06:30:51 -07:00
< TextInput
2020-07-22 17:31:41 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.bio_label' defaultMessage = 'Bio' / > }
2020-07-21 06:30:51 -07:00
name = 'note'
value = { this . state . note }
onChange = { this . handleTextChange }
2020-07-20 16:18:54 -07:00
/ >
2020-07-21 06:30:51 -07:00
< div className = 'fields-row' >
< div className = 'fields-row__column fields-row__column-6' >
2020-07-22 17:31:41 -07:00
< StillImage src = { this . state . 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-22 17:31:41 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.header_label' defaultMessage = 'Header' / > }
2020-07-21 06:30:51 -07:00
name = 'header'
2020-07-22 17:31:41 -07:00
hint = { < FormattedMessage id = 'soapbox_settings.hints.header' defaultMessage = 'PNG, GIF or JPG. At most 2 MB. Will be downscaled to 1500x500px' / > }
2020-07-21 06:30:51 -07:00
onChange = { this . handleFileChange }
/ >
< FileChooser
2020-07-22 17:31:41 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.avatar_label' defaultMessage = 'Avatar' / > }
2020-07-21 06:30:51 -07:00
name = 'avatar'
2020-07-22 17:31:41 -07:00
hint = { < FormattedMessage id = 'soapbox_settings.hints.avatar' 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 >
< Checkbox
2020-07-22 17:31:41 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.locked_label' defaultMessage = 'Lock account' / > }
hint = { < FormattedMessage id = 'soapbox_settings.hints.locked' defaultMessage = 'Requires you to manually approve followers' / > }
2020-07-21 06:30:51 -07:00
name = 'locked'
checked = { this . state . locked }
onChange = { this . handleCheckboxChange }
2020-07-20 16:18:54 -07:00
/ >
2020-07-21 06:30:51 -07:00
< Checkbox
2020-07-22 17:31:41 -07:00
label = { < FormattedMessage id = 'soapbox_settings.fields.bot_label' defaultMessage = 'This is a bot account' / > }
hint = { < FormattedMessage id = 'soapbox_settings.hints.bot' defaultMessage = 'This account mainly performs automated actions and might not be monitored' / > }
2020-07-21 06:30:51 -07:00
name = 'bot'
checked = { this . state . bot }
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-22 17:31:41 -07:00
< label > < FormattedMessage id = 'soapbox_settings.fields.meta_fields_label' defaultMessage = 'Profile metadata' / > < / l a b e l >
2020-07-21 06:30:51 -07:00
< span className = 'hint' >
2020-07-22 17:31:41 -07:00
< FormattedMessage id = 'soapbox_settings.hints.meta_fields' defaultMessage = 'You can have up to {count, plural, one {# item} other {# items}} displayed as a table on your profile' values = { { count : MAX _FIELDS } } / >
2020-07-21 06:30:51 -07:00
< / s p a n >
{
2020-07-22 17:31:41 -07:00
this . state . promoPanel . map ( ( field , i ) => (
2020-07-21 06:30:51 -07:00
< div className = 'row' key = { i } >
< TextInput
placeholder = { intl . formatMessage ( messages . metaFieldLabel ) }
value = { field . get ( 'name' ) }
2020-07-21 16:38:10 -07:00
onChange = { this . handlePromoPanelChange ( i , 'name' ) }
2020-07-21 06:30:51 -07:00
/ >
< TextInput
placeholder = { intl . formatMessage ( messages . metaFieldContent ) }
value = { field . get ( 'value' ) }
2020-07-21 16:38:10 -07:00
onChange = { this . handlePromoPanelChange ( i , 'value' ) }
2020-07-21 06:30:51 -07:00
/ >
< / d i v >
) )
}
< / d i v >
< / 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 >
) ;
}
}