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, TextInput, Checkbox, FileChooser, ColorWithPicker, FileChooserLogo, } from 'soapbox/features/forms'; import StillImage from 'soapbox/components/still_image'; import { Map as ImmutableMap, List as ImmutableList, getIn, } from 'immutable'; import { updateAdminConfig } from 'soapbox/actions/admin'; import Icon from 'soapbox/components/icon'; import { getSoapboxConfig } from 'soapbox/actions/soapbox'; const messages = defineMessages({ 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' }, }); const mapStateToProps = state => ({ soapbox: getSoapboxConfig(state), }); export default @connect(mapStateToProps) @injectIntl class SoapboxConfig extends ImmutablePureComponent { static propTypes = { soapbox: ImmutablePropTypes.map, dispatch: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, }; state = { isLoading: false, } constructor(props) { super(props); var promoPanelItems = getIn(this.props.soapbox, ['promoPanel'], ['items'], []).get('items'); if (promoPanelItems.size === 0) { this.state.promoPanelItems = ImmutableList([ ImmutableMap({ icon: '', text: '', url: '', }), ]); } else { this.state.promoPanelItems = promoPanelItems; }; var homeFooterItems = getIn(this.props.soapbox, ['navlinks'], ['homefooter'], []).get('homeFooter'); if (homeFooterItems.size === 0) { this.state.homeFooterItems = ImmutableList([ ImmutableMap({ title: '', url: '', }), ]); } else { this.state.homeFooterItems = homeFooterItems; }; var customCssItems = getIn(this.props.soapbox, ['customCss'], []); if (customCssItems.size === 0) { this.state.customCssItems = ImmutableList(['']); } else { this.state.customCssItems = customCssItems; }; this.state.patron = getIn(this.props.soapbox, ['extensions', 'patron'], false); this.state.autoPlayGif = getIn(this.props.soapbox, ['defaultSettings', 'autoPlayGif'], false); this.handlecustomCSSChange = this.handleCustomCSSChange.bind(this); this.handleAutoPlayGifCheckboxChange = this.handleAutoPlayGifCheckboxChange.bind(this); this.handlePatronCheckboxChange = this.handlePatronCheckboxChange.bind(this); } getParams = () => { const { state } = this; var obj = { configs: [{ group: ':pleroma', key: ':frontend_configurations', value: [{ tuple: [':soapbox_fe', { logo: '', banner: '', brandColor: '', customCss: [], promoPanel: { items: [], }, extensions: { patron: false, }, defaultSettings: { autoPlayGif: false, }, copyright: '', navlinks: { homeFooter: [], }, }, ], }], }], }; obj.configs[0].value[0].tuple[1].logo = (state.logo ? state.logo : getIn(this.props.soapbox, ['logo'], '')); obj.configs[0].value[0].tuple[1].banner = (state.banner ? state.banner : getIn(this.props.soapbox, ['banner'], '')); obj.configs[0].value[0].tuple[1].brandColor = (state.brandColor ? state.brandColor : getIn(this.props.soapbox, ['brandColor'], '')); obj.configs[0].value[0].tuple[1].extensions.patron = (state.patron !== undefined ? state.patron : getIn(this.props.soapbox, ['extensions', 'patron'], false)); obj.configs[0].value[0].tuple[1].defaultSettings.autoPlayGif = (state.autoPlayGif !== undefined ? state.autoPlayGif : getIn(this.props.soapbox, ['defaultSettings', 'autoPlayGif'], false)); obj.configs[0].value[0].tuple[1].copyright = (state.copyright ? state.copyright : getIn(this.props.soapbox, ['copyright'], '')); var homeFooterItems = (state.homeFooterItems ? state.homeFooterItems : getIn(this.props.soapbox, ['navlinks'], ['homeFooter'], [])); homeFooterItems.forEach((f) => obj.configs[0].value[0].tuple[1].navlinks.homeFooter.push({ title: f.get('title'), url: f.get('url') }) ); var promoPanelItems = (state.promoPanelItems ? state.promoPanelItems : getIn(this.props.soapbox, ['promoPanel'], ['items'], [])); promoPanelItems.forEach((f) => obj.configs[0].value[0].tuple[1].promoPanel.items.push({ icon: f.get('icon'), text: f.get('text'), url: f.get('url') }) ); var customCssItems = (state.customCssItems ? state.customCssItems : getIn(this.props.soapbox, ['customCss'], [])); customCssItems.forEach((f) => obj.configs[0].value[0].tuple[1].customCss.push(f) ); return obj; } handleSubmit = (event) => { const { dispatch } = this.props; dispatch(updateAdminConfig(this.getParams())).then(() => { this.setState({ isLoading: false }); }).catch((error) => { this.setState({ isLoading: false }); }); this.setState({ isLoading: true }); event.preventDefault(); } handlePatronCheckboxChange = e => { this.setState({ patron: !this.state.patron }); } handleAutoPlayGifCheckboxChange = e => { this.setState({ autoPlayGif: !this.state.autoPlayGif }); } handleBrandColorChange = e => { this.setState({ brandColor: e.hex, }); } handleTextChange = e => { this.setState({ [e.target.name]: e.target.value, }); } handlePromoItemsChange = (i, key) => { return (e) => { this.setState({ promoPanelItems: this.state.promoPanelItems.setIn([i, key], e.target.value), }); }; } handleHomeFooterItemsChange = (i, key) => { return (e) => { this.setState({ homeFooterItems: this.state.homeFooterItems.setIn([i, key], e.target.value), }); }; } handleCustomCSSChange = i => { return (e) => { this.setState({ customCssItems: this.state.customCssItems.setIn([i], e.target.value), }); }; } 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, }); } handleAddPromoPanelItem = () => { this.setState({ promoPanelItems: this.state.promoPanelItems.concat([ ImmutableMap({ icon: '', text: '', url: '', }), ]), }); } handleAddHomeFooterItem = () => { this.setState({ homeFooterItems: this.state.homeFooterItems.concat([ ImmutableMap({ title: '', url: '', }), ]), }); } handleAddCssItem = () => { this.setState({ customCssItems: this.state.customCssItems.concat(['']), }); } render() { const { intl } = this.props; const logo = (this.state.logo ? this.state.logo : getIn(this.props.soapbox, ['logo'], '')); const banner = (this.state.banner ? this.state.banner : getIn(this.props.soapbox, ['banner'], '')); const brandColor = (this.state.brandColor ? this.state.brandColor : getIn(this.props.soapbox, ['brandColor'], '')); const patron = (this.state.patron !== undefined ? this.state.patron : getIn(this.props.soapbox, ['extensions', 'patron'], false)); const autoPlayGif = (this.state.autoPlayGif !== undefined ? this.state.autoPlayGif : getIn(this.props.soapbox, ['defaultSettings', 'autoPlayGif'], false)); const promoPanelItems = (this.state.promoPanelItems ? this.state.promoPanelItems : getIn(this.props.soapbox, ['promoPanel'], ['items'], []).get('items')); const homeFooterItems = (this.state.homeFooterItems ? this.state.homeFooterItems : getIn(this.props.soapbox, ['navlinks'], ['homeFooter'], []).get('homeFooter')); const customCssItems = (this.state.customCssItems ? this.state.customCssItems : getIn(this.props.soapbox, ['customCss'], [])); const copyright = (this.state.copyright ? this.state.copyright : getIn(this.props.soapbox, ['copyright'], '')); return (
} name='logo' hint={
{this.state.banner ? () : ()}
} name='banner' hint={} onChange={this.handleFileChange} />
} value={brandColor} onChange={this.handleBrandColorChange} />
} hint={} name='patron' checked={patron} onChange={this.handlePatronCheckboxChange} /> } hint={} name='autoPlayGif' checked={autoPlayGif} onChange={this.handleAutoPlayGifCheckboxChange} />
Soapbox Icons List }} /> { promoPanelItems.valueSeq().map((field, i) => (
)) }
{ homeFooterItems.valueSeq().map((field, i) => (
)) }
{ customCssItems.valueSeq().map((field, i) => (
)) }
); } }