Skip to content

Commit

Permalink
Merge pull request #2742 from woocommerce/dev/reorganize-frontend-set…
Browse files Browse the repository at this point in the history
…tings-apis

Reorganizes the frontend code related to the calls of settings APIs
  • Loading branch information
eason9487 authored Dec 30, 2024
2 parents 9a53718 + bc91758 commit 2f71864
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
18 changes: 15 additions & 3 deletions js/src/data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( {
Expand All @@ -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( {
Expand Down
21 changes: 14 additions & 7 deletions js/src/hooks/useSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ 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, 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 = () => {
return useSelect( ( select ) => {
const settings = select( STORE_KEY ).getSettings();
const { saveSettings, syncSettings } = useAppDispatch();

return {
settings,
};
const settings = useSelect( ( select ) => {
return select( STORE_KEY ).getSettings();
}, [] );

return {
settings,
saveSettings,
syncSettings,
};
};

export default useSettings;
16 changes: 8 additions & 8 deletions js/src/pages/edit-free-listings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -49,8 +48,13 @@ const EditFreeListings = () => {
const { targetAudience: savedTargetAudience, getFinalCountries } =
useTargetAudienceFinalCountryCodes();

const { settings: savedSettings } = useSettings();
const { saveTargetAudience, saveSettings } = useAppDispatch();
const {
settings: savedSettings,
saveSettings,
syncSettings,
} = useSettings();

const { saveTargetAudience } = useAppDispatch();
const { saveShippingRates } = useSaveShippingRates();
const { saveShippingTimes } = useSaveShippingTimes();

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions js/src/pages/onboarding/setup-stepper/saved-setup-stepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
9 changes: 3 additions & 6 deletions js/src/pages/onboarding/setup-stepper/setup-paid-ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';
Expand Down Expand Up @@ -48,17 +47,15 @@ export default function SetupPaidAds() {
useBudgetRecommendation( countryCodes );
const [ handleSetupComplete ] = useAdsSetupCompleteCallback();
const { billingStatus } = useGoogleAdsAccountBillingStatus();
const { syncSettings } = useAppDispatch();

const isBillingCompleted =
billingStatus?.status === GOOGLE_ADS_BILLING_STATUS.APPROVED;

const finishOnboardingSetup = async ( onBeforeFinish = noop ) => {
try {
await onBeforeFinish();
await apiFetch( {
path: `${ API_NAMESPACE }/mc/settings/sync`,
method: 'POST',
} );
await syncSettings();
} catch ( e ) {
setCompleting( null );

Expand Down

0 comments on commit 2f71864

Please sign in to comment.