Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(subscription-block): remove default list when misconfigured #1608

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions src/blocks/subscribe/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* External dependencies.
*/
import classnames from 'classnames';
import { intersection } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -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 ) } );
Expand Down Expand Up @@ -282,7 +307,7 @@ export default function SubscribeEdit( {
</div>
</div>

{ inFlight ? (
{ ! hasFetchedLists ? (
<Spinner />
) : (
<div
Expand All @@ -294,6 +319,11 @@ export default function SubscribeEdit( {
>
{ editedState === 'initial' && (
<form onSubmit={ ev => ev.preventDefault() }>
{ hasMissingLists && (
<Notice isDismissible={ false } status="error">
{ __( 'A previously selected list is no longer available. Please, revise the subscription lists selection.', 'newspack-newsletters' ) }
</Notice>
) }
{ lists.length > 1 && (
<div className="newspack-newsletters-lists">
<ul>
Expand Down
4 changes: 0 additions & 4 deletions src/blocks/subscribe/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down