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

Reorganizes the frontend code related to the calls of settings APIs #2742

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

/**
* 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 @@
};
}

/**
* 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( {

Check warning on line 352 in js/src/data/actions.js

View check run for this annotation

Codecov / codecov/patch

js/src/data/actions.js#L351-L352

Added lines #L351 - L352 were not covered by tests
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 @@
/**
* 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();

Check warning on line 20 in js/src/hooks/useSettings.js

View check run for this annotation

Codecov / codecov/patch

js/src/hooks/useSettings.js#L20

Added line #L20 was not covered by tests

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

Check warning on line 23 in js/src/hooks/useSettings.js

View check run for this annotation

Codecov / codecov/patch

js/src/hooks/useSettings.js#L22-L23

Added lines #L22 - L23 were not covered by tests
}, [] );

return {

Check warning on line 26 in js/src/hooks/useSettings.js

View check run for this annotation

Codecov / codecov/patch

js/src/hooks/useSettings.js#L26

Added line #L26 was not covered by tests
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 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 { targetAudience: savedTargetAudience, getFinalCountries } =
useTargetAudienceFinalCountryCodes();

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

Check warning on line 55 in js/src/pages/edit-free-listings/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/pages/edit-free-listings/index.js#L55

Added line #L55 was not covered by tests

const { saveTargetAudience } = useAppDispatch();

Check warning on line 57 in js/src/pages/edit-free-listings/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/pages/edit-free-listings/index.js#L57

Added line #L57 was not covered by tests
const { saveShippingRates } = useSaveShippingRates();
const { saveShippingTimes } = useSaveShippingTimes();

Expand Down Expand Up @@ -87,10 +91,6 @@
[ 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 @@
);

// Sync data once our changes are saved, even partially succesfully.
await fetchSettingsSync();
await syncSettings();

Check warning on line 158 in js/src/pages/edit-free-listings/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/pages/edit-free-listings/index.js#L158

Added line #L158 was not covered by tests

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
Loading