Skip to content

Commit 13db281

Browse files
authored
Merge pull request #3095 from Parsely/fix/potential-duplicated-requests
PCH: Fix issue with potential duplicate API calls in settings and smart links
2 parents 1937793 + b5da794 commit 13db281

File tree

7 files changed

+48
-98
lines changed

7 files changed

+48
-98
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '3a7734d69011884c8267');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '8c7ccdbe52c4ca11726b');

build/content-helper/dashboard-widget.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => 'fe15e7e38c33aff43523');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => '8f1a8e633d9114a34892');

build/content-helper/editor-sidebar.js

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content-helper/common/settings/provider.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ const SettingsContext = createContext<SettingsContextType<Settings>>( {
5959
* // Using the useSettings hook with a specific type
6060
* const { settings, setSettings } = useSettings<SidebarSettings>();
6161
*/
62-
export const useSettings = <T = Settings>(): SettingsContextType<T> => {
62+
export function useSettings<T = Settings>(): SettingsContextType<T> {
6363
const context = useContext( SettingsContext );
6464
if ( context === undefined ) {
6565
throw new Error( 'useSettings must be used within a SettingsProvider' );
6666
}
6767
return context as unknown as SettingsContextType<T>;
68-
};
68+
}
6969

7070
/**
7171
* Custom types for brevity and for avoiding a "type React is undefined" error.
@@ -88,14 +88,23 @@ const useSaveSettings = (
8888
endpoint: string, data: Settings, deps: ReactDeps
8989
): void => {
9090
const isFirstRender = useRef( true );
91+
const previousData = useRef<Settings>( data );
9192

9293
useEffect( () => {
9394
// Don't save settings on the first render.
9495
if ( isFirstRender.current ) {
9596
isFirstRender.current = false;
97+
previousData.current = data;
98+
return;
99+
}
100+
101+
// Only save if the data has actually changed.
102+
if ( JSON.stringify( previousData.current ) === JSON.stringify( data ) ) {
96103
return;
97104
}
98105

106+
previousData.current = data;
107+
99108
apiFetch( {
100109
path: '/wp-parsely/v2/settings/' + endpoint,
101110
method: 'PUT',

src/content-helper/editor-sidebar/smart-linking/component.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getBlockContent } from '@wordpress/blocks';
66
import { Button, Notice, PanelRow } from '@wordpress/components';
77
import { useDebounce } from '@wordpress/compose';
88
import { select, useDispatch, useSelect } from '@wordpress/data';
9-
import { useEffect, useMemo, useState } from '@wordpress/element';
9+
import { useEffect, useMemo, useRef, useState } from '@wordpress/element';
1010
import { __, _n, sprintf } from '@wordpress/i18n';
1111
import { Icon, external } from '@wordpress/icons';
1212

@@ -191,6 +191,7 @@ export const SmartLinkingPanel = ( {
191191
};
192192
}, [] );
193193

194+
const initializationRef = useRef( false );
194195
/**
195196
* Handles the initialization of the Smart Linking existing links by getting the
196197
* existing smart links from the post content and the database.
@@ -207,6 +208,12 @@ export const SmartLinkingPanel = ( {
207208
return;
208209
}
209210

211+
if ( initializationRef.current ) {
212+
// Return early if the component has already started initialization.
213+
return;
214+
}
215+
initializationRef.current = true;
216+
210217
// Get the existing smart links from the post content.
211218
const existingSmartLinks = getAllSmartLinksInPost();
212219

@@ -236,12 +243,13 @@ export const SmartLinkingPanel = ( {
236243
} );
237244
}
238245
}, [
246+
permissions.SmartLinking,
247+
ready,
248+
postId,
239249
addInboundSmartLinks,
240250
addSmartLinks,
241-
postId,
242-
ready,
243251
setIsReady,
244-
permissions.SmartLinking,
252+
initializationRef,
245253
] );
246254

247255
/**

src/rest-api/settings/class-endpoint-excerpt-suggestions-settings.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)