From c686e2979ff68b0e81c7fc428bf4542ef4dbf4aa Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 20 Dec 2024 19:06:24 +0800 Subject: [PATCH 1/5] Correct the JSDoc description for the `saveSettings` action. --- js/src/data/actions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/src/data/actions.js b/js/src/data/actions.js index 812dad900e..01b33c47f7 100644 --- a/js/src/data/actions.js +++ b/js/src/data/actions.js @@ -325,10 +325,10 @@ export function* fetchSettings() { } /** - * Save the the MC settings. + * Save the plugin settings. * - * @param {SettingsData} settings settings - * @return {Object} Action object to save target audience. + * @param {SettingsData} settings Plugin settings + * @return {Object} Action object to save the plugin settings. */ export function* saveSettings( settings ) { yield apiFetch( { From 64145d173ccd75617da3a4b85900bacb44c097c4 Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 20 Dec 2024 19:07:57 +0800 Subject: [PATCH 2/5] Add the `saveSettings` action as one of the return values to the `useSettings` hook. --- js/src/hooks/useSettings.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/js/src/hooks/useSettings.js b/js/src/hooks/useSettings.js index ebd0bab40d..9e6b0e5132 100644 --- a/js/src/hooks/useSettings.js +++ b/js/src/hooks/useSettings.js @@ -6,21 +6,25 @@ import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ -import { STORE_KEY } from '~/data'; +import { STORE_KEY, useAppDispatch } from '~/data'; /** - * Returns an object `{ settings }` to be used in the Setup Free Listing page. + * Returns an object `{ settings, saveSettings }` to be used in the Setup Free Listing page. * * `settings` is the saved values retrieved from API. + * `saveSettings` action to save the plugin settings. */ const useSettings = () => { - return useSelect( ( select ) => { - const settings = select( STORE_KEY ).getSettings(); + const { saveSettings } = useAppDispatch(); - return { - settings, - }; + const settings = useSelect( ( select ) => { + return select( STORE_KEY ).getSettings(); }, [] ); + + return { + settings, + saveSettings, + }; }; export default useSettings; From 4b8ceeab82e0dbd220e35d8f8a89c4ef206677b4 Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 20 Dec 2024 19:10:27 +0800 Subject: [PATCH 3/5] Change the use of `saveSettings` action to the one returned from the `useSettings` hook. --- js/src/pages/edit-free-listings/index.js | 4 ++-- js/src/pages/onboarding/setup-stepper/saved-setup-stepper.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/js/src/pages/edit-free-listings/index.js b/js/src/pages/edit-free-listings/index.js index ae2934950c..7e640ce285 100644 --- a/js/src/pages/edit-free-listings/index.js +++ b/js/src/pages/edit-free-listings/index.js @@ -49,8 +49,8 @@ const EditFreeListings = () => { const { targetAudience: savedTargetAudience, getFinalCountries } = useTargetAudienceFinalCountryCodes(); - const { settings: savedSettings } = useSettings(); - const { saveTargetAudience, saveSettings } = useAppDispatch(); + const { settings: savedSettings, saveSettings } = useSettings(); + const { saveTargetAudience } = useAppDispatch(); const { saveShippingRates } = useSaveShippingRates(); const { saveShippingTimes } = useSaveShippingTimes(); diff --git a/js/src/pages/onboarding/setup-stepper/saved-setup-stepper.js b/js/src/pages/onboarding/setup-stepper/saved-setup-stepper.js index 62d7f6652c..2aed34243a 100644 --- a/js/src/pages/onboarding/setup-stepper/saved-setup-stepper.js +++ b/js/src/pages/onboarding/setup-stepper/saved-setup-stepper.js @@ -38,7 +38,7 @@ import { const SavedSetupStepper = ( { savedStep } ) => { const [ step, setStep ] = useState( savedStep ); - const { settings } = useSettings(); + const { settings, saveSettings } = useSettings(); const { data: suggestedAudience } = useTargetAudienceWithSuggestions(); const { targetAudience, getFinalCountries } = useTargetAudienceFinalCountryCodes(); @@ -51,7 +51,7 @@ const SavedSetupStepper = ( { savedStep } ) => { data: shippingTimes, } = useShippingTimes(); - const { saveTargetAudience, saveSettings } = useAppDispatch(); + const { saveTargetAudience } = useAppDispatch(); const { saveShippingRates } = useSaveShippingRates(); const { saveShippingTimes } = useSaveShippingTimes(); const { createNotice } = useDispatchCoreNotices(); From 1af7a3228679f32e3c4e8cd870a1cd43424f2069 Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 20 Dec 2024 19:14:26 +0800 Subject: [PATCH 4/5] Create a new action `syncSettings` and add it as one of the return values to the `useSettings` hook. --- js/src/data/actions.js | 12 ++++++++++++ js/src/hooks/useSettings.js | 7 +++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/js/src/data/actions.js b/js/src/data/actions.js index 01b33c47f7..6a823478a9 100644 --- a/js/src/data/actions.js +++ b/js/src/data/actions.js @@ -343,6 +343,18 @@ export function* saveSettings( settings ) { }; } +/** + * Sync the shipping and tax rate settings to Google Merchant Center. + * + * @throws Will throw an error if the request failed. + */ +export function* syncSettings() { + yield apiFetch( { + path: `${ API_NAMESPACE }/mc/settings/sync`, + method: 'POST', + } ); +} + export function* fetchJetpackAccount() { try { const response = yield apiFetch( { diff --git a/js/src/hooks/useSettings.js b/js/src/hooks/useSettings.js index 9e6b0e5132..cd70870b4e 100644 --- a/js/src/hooks/useSettings.js +++ b/js/src/hooks/useSettings.js @@ -9,13 +9,15 @@ import { useSelect } from '@wordpress/data'; import { STORE_KEY, useAppDispatch } from '~/data'; /** - * Returns an object `{ settings, saveSettings }` to be used in the Setup Free Listing page. + * Returns an object `{ settings, saveSettings, syncSettings }` to + * be used in the Setup Free Listing page. * * `settings` is the saved values retrieved from API. * `saveSettings` action to save the plugin settings. + * `syncSettings` action to sync the shipping and tax rate settings to Google Merchant Center. */ const useSettings = () => { - const { saveSettings } = useAppDispatch(); + const { saveSettings, syncSettings } = useAppDispatch(); const settings = useSelect( ( select ) => { return select( STORE_KEY ).getSettings(); @@ -24,6 +26,7 @@ const useSettings = () => { return { settings, saveSettings, + syncSettings, }; }; From bc9175888abcb4a3bef8244d60b742eff81b7d0d Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 20 Dec 2024 19:21:07 +0800 Subject: [PATCH 5/5] Change the API calls for syncing settings to the `syncSettings` action. --- js/src/pages/edit-free-listings/index.js | 14 +++++++------- .../onboarding/setup-stepper/setup-paid-ads.js | 9 +++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/js/src/pages/edit-free-listings/index.js b/js/src/pages/edit-free-listings/index.js index 7e640ce285..83c103c9e4 100644 --- a/js/src/pages/edit-free-listings/index.js +++ b/js/src/pages/edit-free-listings/index.js @@ -14,7 +14,6 @@ import TopBar from '~/components/stepper/top-bar'; import SetupFreeListings from '~/components/free-listings/setup-free-listings'; import useTargetAudienceFinalCountryCodes from '~/hooks/useTargetAudienceFinalCountryCodes'; import useSettings from '~/hooks/useSettings'; -import useApiFetchCallback from '~/hooks/useApiFetchCallback'; import useLayout from '~/hooks/useLayout'; import useNavigateAwayPromptEffect from '~/hooks/useNavigateAwayPromptEffect'; import useShippingRates from '~/hooks/useShippingRates'; @@ -49,7 +48,12 @@ const EditFreeListings = () => { const { targetAudience: savedTargetAudience, getFinalCountries } = useTargetAudienceFinalCountryCodes(); - const { settings: savedSettings, saveSettings } = useSettings(); + const { + settings: savedSettings, + saveSettings, + syncSettings, + } = useSettings(); + const { saveTargetAudience } = useAppDispatch(); const { saveShippingRates } = useSaveShippingRates(); const { saveShippingTimes } = useSaveShippingTimes(); @@ -87,10 +91,6 @@ const EditFreeListings = () => { [ savedShippingTimes ] ); - const [ fetchSettingsSync ] = useApiFetchCallback( { - path: `/wc/gla/mc/settings/sync`, - method: 'POST', - } ); const { createNotice } = useDispatchCoreNotices(); // Check what've changed to show prompt, and send requests only to save changed things. @@ -155,7 +155,7 @@ const EditFreeListings = () => { ); // Sync data once our changes are saved, even partially succesfully. - await fetchSettingsSync(); + await syncSettings(); if ( errorMessage ) { createNotice( 'error', errorMessage ); diff --git a/js/src/pages/onboarding/setup-stepper/setup-paid-ads.js b/js/src/pages/onboarding/setup-stepper/setup-paid-ads.js index 770ae8cc1b..7842dbcec7 100644 --- a/js/src/pages/onboarding/setup-stepper/setup-paid-ads.js +++ b/js/src/pages/onboarding/setup-stepper/setup-paid-ads.js @@ -2,7 +2,6 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import apiFetch from '@wordpress/api-fetch'; import { useState } from '@wordpress/element'; import { noop } from 'lodash'; @@ -18,7 +17,7 @@ import CampaignAssetsForm from '~/components/paid-ads/campaign-assets-form'; import AppButton from '~/components/app-button'; import useGoogleAdsAccountBillingStatus from '~/hooks/useGoogleAdsAccountBillingStatus'; import { getProductFeedUrl } from '~/utils/urls'; -import { API_NAMESPACE } from '~/data/constants'; +import { useAppDispatch } from '~/data'; import { GUIDE_NAMES, GOOGLE_ADS_BILLING_STATUS } from '~/constants'; import { ACTION_COMPLETE, ACTION_SKIP } from './constants'; import SkipButton from './skip-button'; @@ -48,6 +47,7 @@ export default function SetupPaidAds() { useBudgetRecommendation( countryCodes ); const [ handleSetupComplete ] = useAdsSetupCompleteCallback(); const { billingStatus } = useGoogleAdsAccountBillingStatus(); + const { syncSettings } = useAppDispatch(); const isBillingCompleted = billingStatus?.status === GOOGLE_ADS_BILLING_STATUS.APPROVED; @@ -55,10 +55,7 @@ export default function SetupPaidAds() { const finishOnboardingSetup = async ( onBeforeFinish = noop ) => { try { await onBeforeFinish(); - await apiFetch( { - path: `${ API_NAMESPACE }/mc/settings/sync`, - method: 'POST', - } ); + await syncSettings(); } catch ( e ) { setCompleting( null );