Skip to content

Commit

Permalink
fix: refetch sublists when selecting a new list
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Oct 14, 2024
1 parent 9d95c3c commit 60ee0f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/newsletter-editor/sidebar/send-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useEffect, useState } from '@wordpress/element';
*/
import Autocomplete from './autocomplete';
import { fetchSendLists, useNewsletterData } from '../store';
import { usePrevious } from '../utils';

// The container for list + sublist autocomplete fields.
const SendTo = () => {
Expand All @@ -36,6 +37,7 @@ const SendTo = () => {
const sublistLabel = labels?.sublist || __( 'sublist', 'newspack-newsletters' );
const selectedList = lists.find( item => item.id === listId );
const selectedSublist = sublists?.find( item => item.id === sublistId );
const prevListId = usePrevious( listId );

// Cancel any queued fetches on unmount.
useEffect( () => {
Expand All @@ -59,6 +61,11 @@ const SendTo = () => {
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 ) {
fetchSendLists( { type: 'sublist', parent_id: listId }, true );
}
}, [ newsletterData, listId, sublistId ] );

const renderSelectedSummary = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/newsletter-editor/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const fetchSyncErrors = async postId => {
}

// Dispatcher to fetch send lists and sublists from the connected ESP and update the newsletterData in store.
export const fetchSendLists = debounce( async ( opts ) => {
export const fetchSendLists = debounce( async ( opts, replace = false ) => {
updateNewsletterDataError( null );
try {
const { name } = getServiceProvider();
Expand Down Expand Up @@ -190,7 +190,7 @@ export const fetchSendLists = debounce( async ( opts ) => {
}

const updatedNewsletterData = { ...newsletterData };
const updatedSendLists = [ ...sendLists ];
const updatedSendLists = replace ? [] : [ ...sendLists ];

// If no existing items found, fetch from the ESP.
const isRetrieving = coreSelect( STORE_NAMESPACE ).getIsRetrieving();
Expand Down
14 changes: 14 additions & 0 deletions src/newsletter-editor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -84,3 +85,16 @@ export const refreshEmailHtml = async ( postId, postTitle, postContent ) => {
return html;
};

/**
* Custom hook to fetch a previous state or prop value.
*
* @param {string} value of the prop or state to fetch.
* @return {*} The previous value of the prop or state.
*/
export const usePrevious = value => {
const ref = useRef();
useEffect( () => {
ref.current = value;
},[ value ] );
return ref.current;
}

0 comments on commit 60ee0f3

Please sign in to comment.