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

21908 double request to semrush api #21913

Merged
merged 10 commits into from
Dec 23, 2024
16 changes: 13 additions & 3 deletions packages/js/src/components/SEMrushRelatedKeyphrasesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ class SEMrushRelatedKeyphrasesModal extends Component {
* @returns {void}
*/
onModalOpen() {
if ( ! this.props.keyphrase.trim() ) {
this.props.onOpenWithNoKeyphrase();
const {
keyphrase,
onOpenWithNoKeyphrase,
onOpen,
location,
newRequest,
countryCode,
} = this.props;
if ( ! keyphrase.trim() ) {
onOpenWithNoKeyphrase();
return;
}

this.props.onOpen( this.props.location );
onOpen( location );
newRequest( countryCode, keyphrase );
}

/**
Expand Down Expand Up @@ -217,6 +226,7 @@ SEMrushRelatedKeyphrasesModal.propTypes = {
onAuthentication: PropTypes.func.isRequired,
countryCode: PropTypes.string,
learnMoreLink: PropTypes.string,
newRequest: PropTypes.func.isRequired,
};

SEMrushRelatedKeyphrasesModal.defaultProps = {
Expand Down
61 changes: 29 additions & 32 deletions packages/js/src/components/SEMrushRelatedKeyphrasesModalContent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* External dependencies */
import { KeyphrasesTable, UserMessage, PremiumUpsell } from "@yoast/related-keyphrase-suggestions";
import { useCallback, useState } from "@wordpress/element";
import { CountrySelector, KeyphrasesTable, UserMessage, PremiumUpsell } from "@yoast/related-keyphrase-suggestions";
import { Root } from "@yoast/ui-library";
import PropTypes from "prop-types";
import { isEmpty } from "lodash";

/* Internal dependencies */
import SEMrushCountrySelector from "./modals/SEMrushCountrySelector";

/**
* Determines whether the error property is present in the passed response object.
*
Expand All @@ -15,6 +12,9 @@ import SEMrushCountrySelector from "./modals/SEMrushCountrySelector";
* @returns {boolean} Whether or not the error property is present.
*/
export function hasError( response ) {
if ( response?.code === "invalid_json" || response?.code === "fetch_error" ) {
return true;
}
return ! isEmpty( response ) && "error" in response;
}

Expand Down Expand Up @@ -71,27 +71,34 @@ export function getUserMessage( props ) {
*/
export default function RelatedKeyphraseModalContent( props ) {
const {
response = {},
lastRequestKeyphrase = "",
keyphrase = "",
newRequest,
setCountry,
relatedKeyphrases = [],
renderAction = null,
countryCode,
requestLimitReached = false,
setRequestFailed,
setNoResultsFound,
relatedKeyphrases = [],
setRequestSucceeded,
setRequestLimitReached,
isPending,
countryCode = "us",
setCountry,
newRequest,
response = {},
isRtl = false,
isPremium = false,
userLocale = "en_US",
isPending = false,
isPremium = false,
semrushUpsellLink = "",
premiumUpsellLink = "",
} = props;

const [ activeCountryCode, setActiveCountryCode ] = useState( countryCode );

/**
* Sends a new related keyphrases request to SEMrush and updates the semrush_country_code value in the database.
*
* @returns {void}
*/
const relatedKeyphrasesRequest = useCallback( async() => {
newRequest( countryCode, keyphrase );
setActiveCountryCode( countryCode );
}, [ countryCode, keyphrase, newRequest ] );

return (
<Root context={ { isRtl } }>

Expand All @@ -100,17 +107,12 @@ export default function RelatedKeyphraseModalContent( props ) {
className="yst-mb-4"
/> }

{ ! requestLimitReached && <SEMrushCountrySelector
{ ! requestLimitReached && <CountrySelector
countryCode={ countryCode }
setCountry={ setCountry }
newRequest={ newRequest }
keyphrase={ keyphrase }
setRequestFailed={ setRequestFailed }
setNoResultsFound={ setNoResultsFound }
setRequestSucceeded={ setRequestSucceeded }
setRequestLimitReached={ setRequestLimitReached }
response={ response }
lastRequestKeyphrase={ lastRequestKeyphrase }
activeCountryCode={ activeCountryCode }
onChange={ setCountry }
onClick={ relatedKeyphrasesRequest }
className="yst-mb-4"
userLocale={ userLocale.split( "_" )[ 0 ] }
/> }

Expand Down Expand Up @@ -139,12 +141,7 @@ RelatedKeyphraseModalContent.propTypes = {
countryCode: PropTypes.string.isRequired,
setCountry: PropTypes.func.isRequired,
newRequest: PropTypes.func.isRequired,
setRequestSucceeded: PropTypes.func.isRequired,
setRequestLimitReached: PropTypes.func.isRequired,
setRequestFailed: PropTypes.func.isRequired,
setNoResultsFound: PropTypes.func.isRequired,
response: PropTypes.object,
lastRequestKeyphrase: PropTypes.string,
isRtl: PropTypes.bool,
userLocale: PropTypes.string,
isPending: PropTypes.bool,
Expand Down
147 changes: 0 additions & 147 deletions packages/js/src/components/modals/SEMrushCountrySelector.js

This file was deleted.

16 changes: 0 additions & 16 deletions packages/js/src/containers/SEMrushRelatedKeyphrases.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ export default compose( [
const {
setSEMrushChangeCountry,
setSEMrushNewRequest,
setSEMrushRequestSucceeded,
setSEMrushRequestFailed,
setSEMrushSetRequestLimitReached,
setSEMrushNoResultsFound,
} = dispatch( "yoast-seo/editor" );
return {
setCountry: ( countryCode ) => {
Expand All @@ -54,18 +50,6 @@ export default compose( [
newRequest: ( countryCode, keyphrase ) => {
setSEMrushNewRequest( countryCode, keyphrase );
},
setRequestSucceeded: ( response ) => {
setSEMrushRequestSucceeded( response );
},
setRequestFailed: ( response ) => {
setSEMrushRequestFailed( response );
},
setRequestLimitReached: () => {
setSEMrushSetRequestLimitReached();
},
setNoResultsFound: () => {
setSEMrushNoResultsFound();
},
};
} ),
] )( RelatedKeyphrasesModalContent );
6 changes: 6 additions & 0 deletions packages/js/src/containers/SEMrushRelatedKeyphrasesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default compose( [
getSEMrushSelectedCountry,
getPreference,
selectLinkParams,
getFocusKeyphrase,
} = select( "yoast-seo/editor" );

return {
Expand All @@ -19,6 +20,7 @@ export default compose( [
countryCode: getSEMrushSelectedCountry(),
isRtl: getPreference( "isRtl", false ),
learnMoreLink: addQueryArgs( "https://yoa.st/3-v", selectLinkParams() ),
keyphrase: getFocusKeyphrase(),
};
} ),
withDispatch( ( dispatch ) => {
Expand All @@ -27,6 +29,7 @@ export default compose( [
setSEMrushOpenModal,
setSEMrushDismissModal,
setSEMrushLoginStatus,
setSEMrushNewRequest,
} = dispatch( "yoast-seo/editor" );

return {
Expand All @@ -42,6 +45,9 @@ export default compose( [
onAuthentication: ( status ) => {
setSEMrushLoginStatus( status );
},
newRequest: ( countryCode, keyphrase ) => {
setSEMrushNewRequest( countryCode, keyphrase );
},
};
} ),
] )( SEMrushRelatedKeyphrasesModal );
Loading
Loading