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, } from 'immutable'; import { postSoapbox } from 'soapbox/actions/soapbox'; const messages = defineMessages({ heading: { id: 'column.soapbox_settings', defaultMessage: 'Soapbox settings' }, copyrightFooterLabel: { id: 'soapbox_settings.copyright_footer.meta_fields.label_placeholder', defaultMessage: 'Copyright footer' }, 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' }, customCssLabel: { id: 'soapbox_settings.custom_css.meta_fields.url_placeholder', defaultMessage: 'URL' }, }); const mapStateToProps = state => { // const soapbox = state.get('soapbox'); return { soapbox: state.get('soapbox'), }; }; export default @connect(mapStateToProps) @injectIntl class ConfigSoapbox extends ImmutablePureComponent { static propTypes = { soapbox: ImmutablePropTypes.map, dispatch: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, }; state = { isLoading: false, } constructor(props) { super(props); const initialState = props.soapbox.withMutations(map => { }); this.state = initialState.toObject(); // this.state = ImmutableMap(props.soapbox); // console.log(this.state); // console.log(JSON.stringify(this.state, null, 2)); if (!this.state.logo) { this.state.logo = ''; } if (!this.state.banner) { this.state.banner = ''; } if (!this.state.defaultSettings.autoPlayGif) { this.state.defaultSettings.autoPlayGif = false; // console.log(this.state.defaultSettings.autoPlayGif); }; if (!this.state.extensions.patron) { this.state.extensions.patron = false; // console.log(this.state.extensions.patron); }; if (!this.state.promoPanel.items) { this.state.promoPanel.items = ImmutableList([ ImmutableMap({ icon: '', text: '', url: '', }), ]); }; if (!this.state.navlinks.homeFooter) { this.state.navlinks.homeFooter = ImmutableList([ ImmutableMap({ title: '', url: '', }), ]); }; if (!this.state.customCssItems) { this.state.customCssItems = ImmutableList([' ']); }; this.handlecustomCSSChange = this.handleCustomCSSChange.bind(this); this.handleAddPromoPanelItem = this.handleAddPromoPanelItem.bind(this); this.handleAddHomeFooterItem = this.handleAddHomeFooterItem.bind(this); this.handleAddCssItem = this.handleAddCssItem.bind(this); this.handleExtensionsCheckboxChange = this.handleExtensionsCheckboxChange.bind(this); this.handleDefaultSettingsCheckboxChange = this.handleDefaultSettingsCheckboxChange.bind(this); this.handleBrandColorChange = this.handleBrandColorChange.bind(this); this.getParams = this.getParams.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; obj.configs[0].value[0].tuple[1].banner = state.banner; obj.configs[0].value[0].tuple[1].brandColor = state.brandColor; obj.configs[0].value[0].tuple[1].extensions.patron = state.extensions.patron; obj.configs[0].value[0].tuple[1].defaultSettings.autoPlayGif = state.defaultSettings.autoPlayGif; obj.configs[0].value[0].tuple[1].copyright = state.copyright; this.state.homeFooterItems.forEach((f) => obj.configs[0].value[0].tuple[1].navlinks.homeFooter.push({ title: f.get('title'), url: f.get('url') }) ); this.state.promoItems.forEach((f) => obj.configs[0].value[0].tuple[1].promoPanel.items.push({ icon: f.get('icon'), text: f.get('text'), url: f.get('url') }) ); this.state.customCssItems.forEach((f) => obj.configs[0].value[0].tuple[1].customCss.push(f) ); console.log(JSON.stringify(obj, null, 2)); console.log(JSON.stringify(obj.configs[0].value[0].tuple[1], null, 2)); return obj; } handleSubmit = (event) => { const { dispatch } = this.props; dispatch(postSoapbox(this.getParams())).then(() => { this.setState({ isLoading: false }); }).catch((error) => { this.setState({ isLoading: false }); }); this.setState({ isLoading: true }); event.preventDefault(); } handleExtensionsCheckboxChange = e => { var extensions = { ...this.state.extensions }; if (e.target.name === 'patron') { extensions.patron = e.target.value; } this.setState({ extensions }); // this.setState({ // extensions: this.state.setIn(['extensions', e.target.name], e.target.value), // }); } handleDefaultSettingsCheckboxChange = e => { var defaultSettings = { ...this.state.defaultSettings }; if (e.target.name === 'autoPlayGif') { defaultSettings.autoPlayGif = e.target.value; } this.setState({ defaultSettings }); // this.setState({ // defaultSettings: this.state.setIn(['defaultSettings', '[e.target.name]'], e.target.value), // }); } handleBrandColorChange = e => { this.setState({ brandColor: e.hex, }); // this.state.setIn(['brandColor'], e.hex); } handleTextChange = e => { // this.state.soapbox.setIn(['{e.target.name}'], e.target.value); this.setState({ [e.target.name]: e.target.value, }); } handlePromoItemsChange = (i, key) => { return (e) => { // this.state.soapbox.promoItems.setIn([i, key], e.target.value); this.setState({ promoItems: this.state.promoItems.setIn([i, key], e.target.value), }); }; } handleHomeFooterItemsChange = (i, key) => { return (e) => { // this.state.soapbox.homeFooterItems.setIn([i, key], e.target.value); this.setState({ homeFooterItems: this.state.homeFooterItems.setIn([i, key], e.target.value), }); }; } handleCustomCSSChange = i => { return (e) => { // this.state.soapbox.customCssItems.setIn([i], e.target.value); 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.state.soapbox.setIn([name], url); // this.state.soapbox.setIn([`${name}_file`], file); this.setState({ [name]: url, [`${name}_file`]: file, }); } handleAddPromoPanelItem = () => { this.setState({ Items: this.state.promoPanel.Items.concat([ ImmutableMap({ icon: '', text: '', url: '', }), ]), }); } handleAddHomeFooterItem = () => { this.setState({ homeFooter: this.state.navlinks.homeFooter.concat([ ImmutableMap({ title: '', url: '', }), ]), }); } handleAddCssItem = () => { this.setState({ customCss: this.state.customCss.concat(['']), }); } render() { const { intl } = this.props; const { logo, banner, brandColor, extensions, defaultSettings, copyright, promoPanel, navlinks, customCss } = this.state; // console.log(navlinks.homeFooter); // console.log(promoPanel.items); return (
{logo ? () : ()}
} name='logo' hint={
{banner ? () : ()}
} name='banner' hint={} onChange={this.handleFileChange} />
} value={brandColor || '#0482d8'} onChange={this.handleBrandColorChange} />
} hint={} name='patron' checked={extensions.patron ? extensions.patron : false} onChange={this.handleExtensionsCheckboxChange} /> } hint={} name='autoPlayGif' checked={defaultSettings.autoPlayGif ? defaultSettings.autoPlayGif : false} onChange={this.handleDefaultSettingsCheckboxChange} />
Soapbox Icons List }} /> { promoPanel.items.map((field, i) => (
)) }
{ navlinks.homeFooter.map((field, i) => (
)) }
{ customCss.map((field, i) => (
)) }
); } }