From 107b68a91b072112d212d26e4e46f858f8642432 Mon Sep 17 00:00:00 2001 From: Allison Levine Date: Mon, 23 Dec 2024 17:11:45 -0500 Subject: [PATCH] Show error notice when categories are enabled but not selected. --- .../newsletter/newsletter-categories.jsx | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/projects/plugins/jetpack/_inc/client/newsletter/newsletter-categories.jsx b/projects/plugins/jetpack/_inc/client/newsletter/newsletter-categories.jsx index 1cc0a17981914..1e7bf2b9bfd9e 100644 --- a/projects/plugins/jetpack/_inc/client/newsletter/newsletter-categories.jsx +++ b/projects/plugins/jetpack/_inc/client/newsletter/newsletter-categories.jsx @@ -3,6 +3,7 @@ import { __ } from '@wordpress/i18n'; import clsx from 'clsx'; import React, { useCallback, useMemo } from 'react'; import { connect } from 'react-redux'; +import { createNotice } from 'components/global-notices/state/notices/actions'; import SettingsCard from 'components/settings-card'; import SettingsGroup from 'components/settings-group'; import { FEATURE_NEWSLETTER_JETPACK } from 'lib/plans/constants'; @@ -39,7 +40,6 @@ const mapCategoriesIds = category => { */ function NewsletterCategories( props ) { const { - updateFormStateModuleOption, isSubscriptionsActive, isNewsletterCategoriesEnabled, newsletterCategories, @@ -49,12 +49,9 @@ function NewsletterCategories( props ) { updateFormStateOptionValue, isSavingAnyOption, siteHasConnectedUser, + dispatch, } = props; - const handleEnableNewsletterCategoriesToggleChange = useCallback( () => { - updateFormStateModuleOption( SUBSCRIPTIONS_MODULE_NAME, NEWSLETTER_CATEGORIES_ENABLED_OPTION ); - }, [ updateFormStateModuleOption ] ); - const checkedCategoriesIds = newsletterCategories.map( mapCategoriesIds ); const mappedCategories = useMemo( @@ -82,6 +79,13 @@ function NewsletterCategories( props ) { [ checkedCategoriesIds, updateFormStateOptionValue ] ); + const handleEnableNewsletterCategoriesToggleChange = useCallback( () => { + updateFormStateOptionValue( + NEWSLETTER_CATEGORIES_ENABLED_OPTION, + ! isNewsletterCategoriesEnabled + ); + }, [ updateFormStateOptionValue, isNewsletterCategoriesEnabled ] ); + const isSaving = isSavingAnyOption( [ NEWSLETTER_CATEGORIES_ENABLED_OPTION, NEWSLETTER_CATEGORIES_OPTION, @@ -89,6 +93,27 @@ function NewsletterCategories( props ) { const disabled = ! siteHasConnectedUser || ! isSubscriptionsActive || unavailableInOfflineMode || isSaving; + const handleSubmitForm = useCallback( + event => { + if ( isNewsletterCategoriesEnabled && checkedCategoriesIds.length === 0 ) { + event.preventDefault(); + dispatch( + createNotice( + 'is-error', + __( + 'Please select at least one category when newsletter categories are enabled.', + 'jetpack' + ), + { id: 'newsletter-categories-error' } + ) + ); + return; + } + props.onSubmit?.( event ); + }, + [ isNewsletterCategoriesEnabled, checkedCategoriesIds, props, dispatch ] + ); + return (