Skip to content

Commit

Permalink
fix: more bugs when switching between multiple MC audiences
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Oct 15, 2024
1 parent 9aff88c commit f59cece
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public static function fetch_segment( $segment_id, $list_id ) {
}
$response = ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/segment/$segment_id",
"lists/$list_id/segments/$segment_id",
[
'fields' => 'id,name,member_count,type,options,list_id',
],
Expand Down
18 changes: 16 additions & 2 deletions src/newsletter-editor/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Sender from './sender';
import SendTo from './send-to';
import { getServiceProvider } from '../../service-providers';
import withApiHandler from '../../components/with-api-handler';
import { fetchNewsletterData, useIsRetrieving, useNewsletterData, useNewsletterDataError } from '../store';
import { fetchNewsletterData, updateNewsletterData, useIsRetrieving, useNewsletterData, useNewsletterDataError } from '../store';
import { isSupportedESP } from '../utils';
import './style.scss';

Expand Down Expand Up @@ -49,22 +49,36 @@ const Sidebar = ( {
// Reconcile stored campaign data with data fetched from ESP.
useEffect( () => {
const updatedMeta = {};
const updatedNewsletterData = { ...newsletterData };

if ( newsletterData?.senderEmail ) {
updatedMeta.senderEmail = newsletterData.senderEmail;
delete updatedNewsletterData.senderEmail;
}
if ( newsletterData?.senderName ) {
updatedMeta.senderName = newsletterData.senderName;
delete updatedNewsletterData.senderName;
}
if ( newsletterData?.send_list_id ) {
updatedMeta.send_list_id = newsletterData.send_list_id;
delete updatedNewsletterData.send_list_id;
}
if ( newsletterData?.send_sublist_id ) {
updatedMeta.send_sublist_id = newsletterData.send_sublist_id;
delete updatedNewsletterData.send_sublist_id;
}
if ( Object.keys( updatedMeta ).length ) {
updateMeta( updatedMeta );
}
}, [ newsletterData ] );
if ( Object.keys( updatedNewsletterData ).length ) {
updateNewsletterData( updatedNewsletterData );
}
}, [
newsletterData?.senderEmail,
newsletterData?.senderName,
newsletterData?.send_list_id,
newsletterData?.send_sublist_id
] );

useEffect( () => {
if ( stringifiedCampaignDefaults ) {
Expand Down
12 changes: 4 additions & 8 deletions src/newsletter-editor/sidebar/send-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const SendTo = () => {
const { labels } = newspack_newsletters_data || {};
const listLabel = labels?.list || __( 'list', 'newspack-newsletters' );
const sublistLabel = labels?.sublist || __( 'sublist', 'newspack-newsletters' );
const selectedList = lists.find( item => item.id === listId );
const selectedSublist = sublists?.find( item => item.id === sublistId );
const selectedList = listId ? lists.find( item => item.id.toString() === listId.toString() ) : null;
const selectedSublist = sublistId ? sublists?.find( item => item.id.toString() === sublistId.toString() ) : null;
const prevListId = usePrevious( listId );

// Cancel any queued fetches on unmount.
Expand All @@ -57,14 +57,10 @@ const SendTo = () => {
fetchSendLists( { ids: [ sublistId ], type: 'sublist', parent_id: listId } );
}

// Prefetch sublist info when selecting a new list ID.
if ( listId && ! sublistId && newsletterData?.sublists && 1 >= newsletterData.sublists.length ) {
fetchSendLists( { type: 'sublist', parent_id: listId } );
}

// If selecting a new list entirely.
if ( listId && listId !== prevListId ) {
if ( listId && prevListId && listId !== prevListId ) {
fetchSendLists( { type: 'sublist', parent_id: listId }, true );
updateMeta( { send_sublist_id: null } );
}
}, [ newsletterData, listId, sublistId ] );

Expand Down

0 comments on commit f59cece

Please sign in to comment.