From f3e95bba51814089e09fa26d7db32d26a5e97be7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 5 May 2022 16:18:43 -0500 Subject: [PATCH] SoapboxConfig: convert home footer links into Streamfield --- .../components/footer-link-input.tsx | 41 +++++++++ app/soapbox/features/soapbox_config/index.tsx | 90 +++---------------- .../normalizers/soapbox/soapbox_config.ts | 7 ++ 3 files changed, 61 insertions(+), 77 deletions(-) create mode 100644 app/soapbox/features/soapbox_config/components/footer-link-input.tsx diff --git a/app/soapbox/features/soapbox_config/components/footer-link-input.tsx b/app/soapbox/features/soapbox_config/components/footer-link-input.tsx new file mode 100644 index 000000000..ac4accc19 --- /dev/null +++ b/app/soapbox/features/soapbox_config/components/footer-link-input.tsx @@ -0,0 +1,41 @@ +import React from 'react'; +import { useIntl, defineMessages } from 'react-intl'; + +import { HStack, Input } from 'soapbox/components/ui'; +import { StreamfieldComponent } from 'soapbox/components/ui/streamfield/streamfield'; + +import type { FooterItem } from 'soapbox/types/soapbox'; + +const messages = defineMessages({ + label: { id: 'soapbox_config.home_footer.meta_fields.label_placeholder', defaultMessage: 'Label' }, + url: { id: 'soapbox_config.home_footer.meta_fields.url_placeholder', defaultMessage: 'URL' }, +}); + +const PromoPanelInput: StreamfieldComponent = ({ value, onChange }) => { + const intl = useIntl(); + + const handleChange = (key: 'title' | 'url'): React.ChangeEventHandler => { + return e => { + onChange(value.set(key, e.currentTarget.value)); + }; + }; + + return ( + + + + + ); +}; + +export default PromoPanelInput; diff --git a/app/soapbox/features/soapbox_config/index.tsx b/app/soapbox/features/soapbox_config/index.tsx index c74e69d98..6964f0f30 100644 --- a/app/soapbox/features/soapbox_config/index.tsx +++ b/app/soapbox/features/soapbox_config/index.tsx @@ -5,9 +5,8 @@ import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; import { updateConfig } from 'soapbox/actions/admin'; import { uploadMedia } from 'soapbox/actions/media'; import snackbar from 'soapbox/actions/snackbar'; -import Icon from 'soapbox/components/icon'; import { Column, Form, FormActions, Button } from 'soapbox/components/ui'; -import Streamfield, { StreamfieldComponent } from 'soapbox/components/ui/streamfield/streamfield'; +import Streamfield from 'soapbox/components/ui/streamfield/streamfield'; import { FieldsGroup, TextInput, @@ -24,6 +23,7 @@ import Accordion from '../ui/components/accordion'; import ColorWithPicker from './components/color-with-picker'; import CryptoAddressInput from './components/crypto-address-input'; +import FooterLinkInput from './components/footer-link-input'; import PromoPanelInput from './components/promo-panel-input'; import SitePreview from './components/site-preview'; @@ -33,8 +33,6 @@ const messages = defineMessages({ heading: { id: 'column.soapbox_config', defaultMessage: 'Soapbox config' }, saved: { id: 'soapbox_config.saved', defaultMessage: 'Soapbox config saved!' }, copyrightFooterLabel: { id: 'soapbox_config.copyright_footer.meta_fields.label_placeholder', defaultMessage: 'Copyright footer' }, - homeFooterItemLabel: { id: 'soapbox_config.home_footer.meta_fields.label_placeholder', defaultMessage: 'Label' }, - homeFooterItemURL: { id: 'soapbox_config.home_footer.meta_fields.url_placeholder', defaultMessage: 'URL' }, cryptoDonatePanelLimitLabel: { id: 'soapbox_config.crypto_donate_panel_limit.meta_fields.limit_placeholder', defaultMessage: 'Number of items to display in the crypto homepage widget' }, customCssLabel: { id: 'soapbox_config.custom_css.meta_fields.url_placeholder', defaultMessage: 'URL' }, rawJSONLabel: { id: 'soapbox_config.raw_json_label', defaultMessage: 'Advanced: Edit raw JSON data' }, @@ -137,39 +135,6 @@ const SoapboxConfig: React.FC = () => { }; }; - const handleAddItem = (path: ConfigPath, template: ImmutableMap) => { - const value = (soapbox.getIn(path) || ImmutableList()) as ImmutableList; - - return () => { - setConfig( - path, - value.push(template), - ); - }; - }; - - const deleteItem = (path: ConfigPath) => { - const newData = data.deleteIn(path); - setData(newData); - }; - - const handleDeleteItem = (path: ConfigPath) => () => deleteItem(path); - - const handleItemChange = ( - path: Array, - key: string, - field: ImmutableMap, - template: Template, - getValue: ValueGetter = e => e.target.value, - ) => { - return handleChange( - path, (e) => - template - .merge(field) - .set(key, getValue(e)), - ); - }; - const handleStreamItemChange = (path: ConfigPath) => { return (values: any[]) => { setConfig(path, ImmutableList(values)); @@ -185,16 +150,11 @@ const SoapboxConfig: React.FC = () => { const deleteStreamItem = (path: ConfigPath) => { return (i: number) => { - deleteItem([...path, i]); + const newData = data.deleteIn([...path, i]); + setData(newData); }; }; - const handleHomeFooterItemChange = (index: number, key: string, field: any, getValue?: ValueGetter) => { - return handleItemChange( - ['navlinks', 'homeFooter', index], key, field, templates.footerItem, getValue, - ); - }; - const handleEditJSON: React.ChangeEventHandler = e => { setRawJSON(e.target.value); }; @@ -323,39 +283,15 @@ const SoapboxConfig: React.FC = () => { onRemoveItem={deleteStreamItem(['promoPanel', 'items'])} /> - -
- - - - - { - soapbox.navlinks.get('homeFooter')?.map((field, i) => ( -
- - - -
- )) - } -
-
- - -
-
-
-
+ } + hint={} + component={FooterLinkInput} + values={soapbox.navlinks.get('homeFooter')?.toArray() || []} + onChange={handleStreamItemChange(['navlinks', 'homeFooter'])} + onAddItem={addStreamItem(['navlinks', 'homeFooter'], templates.footerItem)} + onRemoveItem={deleteStreamItem(['navlinks', 'homeFooter'])} + /> } diff --git a/app/soapbox/normalizers/soapbox/soapbox_config.ts b/app/soapbox/normalizers/soapbox/soapbox_config.ts index 07fd5aff3..aad2a13da 100644 --- a/app/soapbox/normalizers/soapbox/soapbox_config.ts +++ b/app/soapbox/normalizers/soapbox/soapbox_config.ts @@ -171,6 +171,12 @@ const normalizePromoPanel = (soapboxConfig: SoapboxConfigMap): SoapboxConfigMap return soapboxConfig.set('promoPanel', promoPanel.set('items', items)); }; +const normalizeFooterLinks = (soapboxConfig: SoapboxConfigMap): SoapboxConfigMap => { + const path = ['navlinks', 'homeFooter']; + const items = (soapboxConfig.getIn(path, ImmutableList()) as ImmutableList).map(FooterItemRecord); + return soapboxConfig.setIn(path, items); +}; + export const normalizeSoapboxConfig = (soapboxConfig: Record) => { return SoapboxConfigRecord( ImmutableMap(fromJS(soapboxConfig)).withMutations(soapboxConfig => { @@ -178,6 +184,7 @@ export const normalizeSoapboxConfig = (soapboxConfig: Record) => { normalizeAccentColor(soapboxConfig); normalizeColors(soapboxConfig); normalizePromoPanel(soapboxConfig); + normalizeFooterLinks(soapboxConfig); maybeAddMissingColors(soapboxConfig); normalizeCryptoAddresses(soapboxConfig); }),