From 9e37b563116e569768b3cd6464c3bfdfdc9a447b Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 12 Aug 2024 10:48:13 -0300 Subject: [PATCH 1/2] fix(subscription-block): remove default list --- src/blocks/subscribe/index.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/blocks/subscribe/index.php b/src/blocks/subscribe/index.php index dbed4a359..e1101a980 100644 --- a/src/blocks/subscribe/index.php +++ b/src/blocks/subscribe/index.php @@ -115,10 +115,6 @@ function render_block( $attrs ) { $list_map = array_flip( $lists ); $available_lists = array_values( array_intersect( $attrs['lists'], $lists ) ); - if ( empty( $available_lists ) ) { - $available_lists = [ $lists[0] ]; - } - /** * Filters the lists that are about to be displayed in the Subscription block * From 8c193d1a2531fdb94e62d34ac4405a1244cd7400 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 12 Aug 2024 11:40:22 -0300 Subject: [PATCH 2/2] fix: handle missing list in the editor --- src/blocks/subscribe/edit.js | 44 ++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/blocks/subscribe/edit.js b/src/blocks/subscribe/edit.js index 5db5c62a8..f6f310a1b 100644 --- a/src/blocks/subscribe/edit.js +++ b/src/blocks/subscribe/edit.js @@ -4,7 +4,6 @@ * External dependencies. */ import classnames from 'classnames'; -import { intersection } from 'lodash'; /** * WordPress dependencies @@ -84,21 +83,47 @@ export default function SubscribeEdit( { const [ editedState, setEditedState ] = useState( editedStateOptions[ 0 ].value ); const [ inFlight, setInFlight ] = useState( false ); const [ listConfig, setListConfig ] = useState( {} ); + const [ hasFetchedLists, setHasFetchedLists ] = useState( false ); + const [ hasMissingLists, setHasMissingLists ] = useState( false ); + const fetchLists = () => { setInFlight( true ); apiFetch( { path: '/newspack-newsletters/v1/lists_config', } ) .then( setListConfig ) - .finally( () => setInFlight( false ) ); + .finally( () => { + setHasFetchedLists( true ); + setInFlight( false ) + } ); }; useEffect( fetchLists, [] ); + + // Whether any of the selected lists are missing from the list config. + const isConfigMissingList = () => { + return lists.some( listId => ! listConfig.hasOwnProperty( listId ) ); + } + useEffect( () => { - const listIds = Object.keys( listConfig ); - if ( listIds.length && ( ! lists.length || ! intersection( lists, listIds ).length ) ) { - setAttributes( { lists: [ Object.keys( listConfig )[ 0 ] ] } ); + if ( ! hasFetchedLists ) { + return; + } + if ( lists.length ) { + if ( ! hasMissingLists && isConfigMissingList() ) { + setHasMissingLists( true ); + // Remove the missing lists from the selected lists. + setAttributes( { lists: lists.filter( listId => listConfig.hasOwnProperty( listId ) ) } ); + } + } else { + setAttributes( { lists: [ Object.keys( listConfig )[0] ] } ); } - }, [ listConfig ] ); + }, [ hasFetchedLists ] ); + + useEffect( () => { + if ( lists.length && hasMissingLists && ! isConfigMissingList() ) { + setHasMissingLists( false ); + } + }, [ lists ] ); const onChangeBackgroundColor = newBackgroundColor => { setAttributes( { backgroundColorName: getColorName( newBackgroundColor ) } ); @@ -282,7 +307,7 @@ export default function SubscribeEdit( { - { inFlight ? ( + { ! hasFetchedLists ? ( ) : (
{ editedState === 'initial' && (
ev.preventDefault() }> + { hasMissingLists && ( + + { __( 'A previously selected list is no longer available. Please, revise the subscription lists selection.', 'newspack-newsletters' ) } + + ) } { lists.length > 1 && (