diff --git a/plugins/woocommerce-admin/client/dashboard/customizable.js b/plugins/woocommerce-admin/client/dashboard/customizable.js index bf85fe85a0d..4040f2d4c9d 100644 --- a/plugins/woocommerce-admin/client/dashboard/customizable.js +++ b/plugins/woocommerce-admin/client/dashboard/customizable.js @@ -4,7 +4,7 @@ import { __, sprintf } from '@wordpress/i18n'; import { Fragment, Suspense, lazy, useState } from '@wordpress/element'; import { compose } from '@wordpress/compose'; -import { partial, get } from 'lodash'; +import { partial } from 'lodash'; import { Dropdown, Button, Icon } from '@wordpress/components'; import { applyFilters } from '@wordpress/hooks'; import { Icon as WPIcon, plusCircleFilled } from '@wordpress/icons'; @@ -15,6 +15,7 @@ import { Icon as WPIcon, plusCircleFilled } from '@wordpress/icons'; import { H, Spinner } from '@woocommerce/components'; import { SETTINGS_STORE_NAME, + OPTIONS_STORE_NAME, useUserPreferences, } from '@woocommerce/data'; @@ -329,8 +330,7 @@ const CustomizableDashboard = ( { export default compose( withSelect( ( select ) => { - const { getOptions } = select( 'wc-api' ); - + const { getOption } = select( OPTIONS_STORE_NAME ); const { woocommerce_default_date_range: defaultDateRange } = select( SETTINGS_STORE_NAME ).getSetting( 'wc_admin', 'wcAdminSettings' ); @@ -340,16 +340,13 @@ export default compose( }; if ( isOnboardingEnabled() ) { - const options = getOptions( [ - 'woocommerce_task_list_complete', - 'woocommerce_task_list_hidden', - ] ); + withSelectData.homepageEnabled = + window.wcAdminFeatures.homepage && + getOption( 'woocommerce_homescreen_enabled' ) === 'yes'; withSelectData.taskListHidden = - get( options, [ 'woocommerce_task_list_hidden' ], 'no' ) === - 'yes'; + getOption( 'woocommerce_task_list_hidden' ) === 'yes'; withSelectData.taskListComplete = - get( options, [ 'woocommerce_task_list_complete' ], 'no' ) === - 'yes'; + getOption( 'woocommerce_task_list_complete' ) === 'yes'; } return withSelectData; diff --git a/plugins/woocommerce-admin/client/dashboard/utils.js b/plugins/woocommerce-admin/client/dashboard/utils.js index 173efc823c6..f93a6f468b2 100644 --- a/plugins/woocommerce-admin/client/dashboard/utils.js +++ b/plugins/woocommerce-admin/client/dashboard/utils.js @@ -51,6 +51,7 @@ export function getProductIdsForCart( installedPlugins ) { const onboarding = getSetting( 'onboarding', {} ); + const productIds = []; // The population of onboarding.productTypes only happens if the task list should be shown // so bail early if it isn't present. @@ -58,7 +59,6 @@ export function getProductIdsForCart( return productIds; } - const productIds = []; const productTypes = profileItems.product_types || []; productTypes.forEach( ( productType ) => { diff --git a/plugins/woocommerce-admin/client/homepage/layout.js b/plugins/woocommerce-admin/client/homepage/layout.js index fce4e79463c..20912768a24 100644 --- a/plugins/woocommerce-admin/client/homepage/layout.js +++ b/plugins/woocommerce-admin/client/homepage/layout.js @@ -11,9 +11,13 @@ import { } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import classnames from 'classnames'; -import { get } from 'lodash'; import PropTypes from 'prop-types'; +/** + * WooCommerce dependencies + */ +import { OPTIONS_STORE_NAME } from '@woocommerce/data'; + /** * Internal dependencies */ @@ -21,9 +25,9 @@ import QuickLinks from '../quick-links'; import StatsOverview from './stats-overview'; import './style.scss'; import { isOnboardingEnabled } from 'dashboard/utils'; -import withSelect from 'wc-api/with-select'; import TaskListPlaceholder from '../task-list/placeholder'; import InboxPanel from '../header/activity-panel/panels/inbox'; +import withWCApiSelect from 'wc-api/with-select'; const TaskList = lazy( () => import( /* webpackChunkName: "task-list" */ '../task-list' ) @@ -138,32 +142,27 @@ Layout.propTypes = { }; export default compose( - withSelect( ( select ) => { + withWCApiSelect( ( select ) => { const { - getOptions, getUndoDismissRequesting, - isGetOptionsRequesting, } = select( 'wc-api' ); + const { isUndoRequesting } = getUndoDismissRequesting(); + const { getOption, isResolving } = select( OPTIONS_STORE_NAME ); if ( isOnboardingEnabled() ) { - const options = getOptions( [ - 'woocommerce_task_list_complete', - 'woocommerce_task_list_hidden', - ] ); - const { isUndoRequesting } = getUndoDismissRequesting(); - return { isUndoRequesting, - requestingTaskList: isGetOptionsRequesting( [ - 'woocommerce_task_list_complete', - 'woocommerce_task_list_hidden', - ] ), taskListComplete: - get( options, [ 'woocommerce_task_list_complete' ] ) === - 'yes', + getOption( 'woocommerce_task_list_complete' ) === 'yes', taskListHidden: - get( options, [ 'woocommerce_task_list_hidden' ] ) === - 'yes', + getOption( 'woocommerce_task_list_hidden' ) === 'yes', + requestingTaskList: + isResolving( 'getOption', [ + 'woocommerce_task_list_complete', + ] ) || + isResolving( 'getOption', [ + 'woocommerce_task_list_hidden', + ] ), }; } diff --git a/plugins/woocommerce-admin/client/layout/index.js b/plugins/woocommerce-admin/client/layout/index.js index ef6f1a09477..ae4e1e03104 100644 --- a/plugins/woocommerce-admin/client/layout/index.js +++ b/plugins/woocommerce-admin/client/layout/index.js @@ -6,7 +6,7 @@ import { withSelect } from '@wordpress/data'; import { Component, lazy, Suspense } from '@wordpress/element'; import { Router, Route, Switch } from 'react-router-dom'; import PropTypes from 'prop-types'; -import { get, isFunction } from 'lodash'; +import { get, isFunction, identity } from 'lodash'; /** * WooCommerce dependencies @@ -14,7 +14,12 @@ import { get, isFunction } from 'lodash'; import { useFilters, Spinner } from '@woocommerce/components'; import { getHistory } from '@woocommerce/navigation'; import { getSetting } from '@woocommerce/wc-admin-settings'; -import { PLUGINS_STORE_NAME, withPluginsHydration } from '@woocommerce/data'; +import { + OPTIONS_STORE_NAME, + PLUGINS_STORE_NAME, + withPluginsHydration, + withOptionsHydration, +} from '@woocommerce/data'; /** * Internal dependencies @@ -25,7 +30,6 @@ import Header from 'header'; import Notices from './notices'; import { recordPageView } from 'lib/tracks'; import TransientNotices from './transient-notices'; -import withWCApiSelect from 'wc-api/with-select'; const StoreAlerts = lazy( () => import( /* webpackChunkName: "store-alerts" */ './store-alerts' ) ); @@ -211,13 +215,16 @@ class _PageLayout extends Component { export const PageLayout = compose( // Use the useFilters HoC so PageLayout is re-rendered when filters are used to add new pages or reports useFilters( [ PAGES_FILTER, REPORTS_FILTER ] ), - withWCApiSelect( ( select ) => { - const { getOptions } = select( 'wc-api' ); - const options = getOptions( [ 'woocommerce_homescreen_enabled' ] ); + window.wcSettings.preloadOptions + ? withOptionsHydration( { + ...window.wcSettings.preloadOptions, + } ) + : identity, + withSelect( ( select ) => { + const { getOption } = select( OPTIONS_STORE_NAME ); const homepageEnabled = window.wcAdminFeatures.homepage && - get( options, [ 'woocommerce_homescreen_enabled' ], false ) === - 'yes'; + getOption( 'woocommerce_homescreen_enabled' ) === 'yes'; return { homepageEnabled }; } ) )( _PageLayout ); diff --git a/plugins/woocommerce-admin/client/marketing/overview/index.js b/plugins/woocommerce-admin/client/marketing/overview/index.js index d790c9a9c9b..71ed80c4d80 100644 --- a/plugins/woocommerce-admin/client/marketing/overview/index.js +++ b/plugins/woocommerce-admin/client/marketing/overview/index.js @@ -3,6 +3,11 @@ */ import { getSetting } from '@woocommerce/wc-admin-settings'; +/** + * WooCommerce dependencies + */ +import { withOptionsHydration } from '@woocommerce/data'; + /** * Internal dependencies */ @@ -26,4 +31,6 @@ const MarketingOverview = () => { ); }; -export default MarketingOverview; +export default withOptionsHydration( { + ...( window.wcSettings.preloadOptions || {} ), +} )( MarketingOverview ); diff --git a/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js b/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js index a4ce3f3d906..00fc4f40918 100644 --- a/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js +++ b/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js @@ -2,24 +2,23 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { get } from 'lodash'; import { Button } from '@wordpress/components'; import Gridicon from 'gridicons'; import { compose } from '@wordpress/compose'; -import { withDispatch } from '@wordpress/data'; +import { withDispatch, withSelect } from '@wordpress/data'; import PropTypes from 'prop-types'; /** * WooCommerce dependencies */ import { Card } from '@woocommerce/components'; +import { OPTIONS_STORE_NAME } from '@woocommerce/data'; /** * Internal dependencies */ import './style.scss'; import { recordEvent } from 'lib/tracks'; -import withSelect from 'wc-api/with-select'; import WelcomeImage from './images/welcome.svg'; const WelcomeCard = ( { @@ -72,17 +71,17 @@ export { WelcomeCard } // default export export default compose( withSelect( ( select ) => { - const { getOptions, isUpdateOptionsRequesting } = select( 'wc-api' ); - const hideOptionName = 'woocommerce_marketing_overview_welcome_hidden'; - const options = getOptions( [ hideOptionName ] ); - const isHidden = get( options, [ hideOptionName ], 'no' ) === 'yes'; - const isUpdateRequesting = Boolean( isUpdateOptionsRequesting( [ hideOptionName ] ) ); + const { getOption, isOptionsUpdating } = select( OPTIONS_STORE_NAME ); + const isUpdateRequesting = isOptionsUpdating(); + return { - isHidden: isHidden || isUpdateRequesting, + isHidden: + getOption( 'woocommerce_marketing_overview_welcome_hidden' ) === + 'yes' || isUpdateRequesting, }; } ), withDispatch( ( dispatch ) => { - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { updateOptions, }; diff --git a/plugins/woocommerce-admin/client/profile-wizard/index.js b/plugins/woocommerce-admin/client/profile-wizard/index.js index 0a7bad83191..0d7376631fe 100644 --- a/plugins/woocommerce-admin/client/profile-wizard/index.js +++ b/plugins/woocommerce-admin/client/profile-wizard/index.js @@ -247,7 +247,9 @@ const hydrateSettings = export default compose( withSelect( ( select ) => { const { getNotes } = select( 'wc-api' ); - const { getProfileItems, getOnboardingError } = select( ONBOARDING_STORE_NAME ); + const { getProfileItems, getOnboardingError } = select( + ONBOARDING_STORE_NAME + ); const { getActivePlugins } = select( PLUGINS_STORE_NAME ); const notesQuery = { diff --git a/plugins/woocommerce-admin/client/profile-wizard/steps/benefits/index.js b/plugins/woocommerce-admin/client/profile-wizard/steps/benefits/index.js index 3f6985d9e83..8559a1a415b 100644 --- a/plugins/woocommerce-admin/client/profile-wizard/steps/benefits/index.js +++ b/plugins/woocommerce-admin/client/profile-wizard/steps/benefits/index.js @@ -5,7 +5,7 @@ import { __, _n, sprintf } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; -import { withDispatch } from '@wordpress/data'; +import { withDispatch, withSelect } from '@wordpress/data'; import { filter } from 'lodash'; /** @@ -17,6 +17,7 @@ import { pluginNames, ONBOARDING_STORE_NAME, PLUGINS_STORE_NAME, + OPTIONS_STORE_NAME } from '@woocommerce/data'; /** @@ -28,7 +29,6 @@ import ManagementIcon from './images/management'; import SalesTaxIcon from './images/sales_tax'; import ShippingLabels from './images/shipping_labels'; import SpeedIcon from './images/speed'; -import withSelect from 'wc-api/with-select'; import { recordEvent } from 'lib/tracks'; class Benefits extends Component { @@ -108,12 +108,12 @@ class Benefits extends Component { goToNextStep(); } - async startPluginInstall() { + startPluginInstall() { const { updateProfileItems, updateOptions } = this.props; this.setState( { isInstalling: true } ); - await updateOptions( { + updateOptions( { woocommerce_setup_jetpack_opted_in: true, } ); @@ -328,7 +328,7 @@ export default compose( } ), withDispatch( ( dispatch ) => { const { updateProfileItems } = dispatch( ONBOARDING_STORE_NAME ); - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); const { createNotice } = dispatch( 'core/notices' ); return { diff --git a/plugins/woocommerce-admin/client/profile-wizard/steps/usage-modal.js b/plugins/woocommerce-admin/client/profile-wizard/steps/usage-modal.js index b65d96728bd..32c9d5d73b0 100644 --- a/plugins/woocommerce-admin/client/profile-wizard/steps/usage-modal.js +++ b/plugins/woocommerce-admin/client/profile-wizard/steps/usage-modal.js @@ -4,8 +4,7 @@ import { __ } from '@wordpress/i18n'; import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; -import { withDispatch } from '@wordpress/data'; -import { get } from 'lodash'; +import { withDispatch, withSelect } from '@wordpress/data'; import interpolateComponents from 'interpolate-components'; import { Button, @@ -14,11 +13,15 @@ import { Modal, } from '@wordpress/components'; +/** + * WooCommerce dependencies + */ +import { OPTIONS_STORE_NAME } from '@woocommerce/data'; + /** * Internal dependencies */ import { Link } from '@woocommerce/components'; -import withSelect from 'wc-api/with-select'; class UsageModal extends Component { constructor( props ) { @@ -159,20 +162,15 @@ class UsageModal extends Component { export default compose( withSelect( ( select ) => { const { - getOptions, - getOptionsError, - isUpdateOptionsRequesting, - } = select( 'wc-api' ); + getOption, + getOptionsUpdatingError, + isOptionsUpdating, + } = select( OPTIONS_STORE_NAME ); - const options = getOptions( [ 'woocommerce_allow_tracking' ] ); const allowTracking = - get( options, [ 'woocommerce_allow_tracking' ], false ) === 'yes'; - const isRequesting = Boolean( - isUpdateOptionsRequesting( [ 'woocommerce_allow_tracking' ] ) - ); - const hasErrors = Boolean( - getOptionsError( [ 'woocommerce_allow_tracking' ] ) - ); + getOption( 'woocommerce_allow_tracking' ) === 'yes'; + const isRequesting = Boolean( isOptionsUpdating() ); + const hasErrors = Boolean( getOptionsUpdatingError() ); return { allowTracking, @@ -182,7 +180,7 @@ export default compose( } ), withDispatch( ( dispatch ) => { const { createNotice } = dispatch( 'core/notices' ); - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { createNotice, diff --git a/plugins/woocommerce-admin/client/task-list/index.js b/plugins/woocommerce-admin/client/task-list/index.js index 2777d8c6826..c0f35d8fb17 100644 --- a/plugins/woocommerce-admin/client/task-list/index.js +++ b/plugins/woocommerce-admin/client/task-list/index.js @@ -3,7 +3,7 @@ */ import { __ } from '@wordpress/i18n'; import { Component, cloneElement, Fragment } from '@wordpress/element'; -import { get, isEqual } from 'lodash'; +import { isEqual } from 'lodash'; import { compose } from '@wordpress/compose'; import classNames from 'classnames'; import { @@ -21,7 +21,11 @@ import { Icon, check, chevronRight } from '@wordpress/icons'; */ import { H, List, EllipsisMenu } from '@woocommerce/components'; import { updateQueryString } from '@woocommerce/navigation'; -import { ONBOARDING_STORE_NAME, PLUGINS_STORE_NAME } from '@woocommerce/data'; +import { + PLUGINS_STORE_NAME, + OPTIONS_STORE_NAME, + ONBOARDING_STORE_NAME, +} from '@woocommerce/data'; /** * Internal dependencies @@ -118,7 +122,7 @@ class TaskDashboard extends Component { return getAllTasks( { profileItems, - options: taskListPayments, + taskListPayments, query, toggleCartModal: this.toggleCartModal.bind( this ), installedPlugins, @@ -383,7 +387,7 @@ class TaskDashboard extends Component { export default compose( withSelect( ( select, props ) => { const { getProfileItems } = select( ONBOARDING_STORE_NAME ); - const { getOptions } = select( 'wc-api' ); + const { getOption } = select( OPTIONS_STORE_NAME ); const { getActivePlugins, getInstalledPlugins, @@ -391,28 +395,18 @@ export default compose( } = select( PLUGINS_STORE_NAME ); const profileItems = getProfileItems(); - const options = getOptions( [ - 'woocommerce_task_list_welcome_modal_dismissed', - 'woocommerce_task_list_hidden', - 'woocommerce_task_list_tracked_completed_tasks', - ] ); - const modalDismissed = get( - options, - [ 'woocommerce_task_list_welcome_modal_dismissed' ], - false - ); - const taskListPayments = getOptions( [ - 'woocommerce_task_list_payments', - ] ); - const trackedCompletedTasks = get( - options, - [ 'woocommerce_task_list_tracked_completed_tasks' ], - [] - ); + const modalDismissed = + getOption( 'woocommerce_task_list_welcome_modal_dismissed' ) || + false; + const taskListPayments = getOption( 'woocommerce_task_list_payments' ); + const trackedCompletedTasks = + getOption( 'woocommerce_task_list_tracked_completed_tasks' ) || []; + const payments = getOption( 'woocommerce_task_list_payments' ); + const installedPlugins = getInstalledPlugins(); const tasks = getAllTasks( { profileItems, - options: getOptions( [ 'woocommerce_task_list_payments' ] ), + options: payments, query: props.query, installedPlugins, } ); @@ -437,7 +431,7 @@ export default compose( }; } ), withDispatch( ( dispatch ) => { - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { updateOptions, }; diff --git a/plugins/woocommerce-admin/client/task-list/tasks.js b/plugins/woocommerce-admin/client/task-list/tasks.js index d97432af28a..10ddc82fcf5 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks.js +++ b/plugins/woocommerce-admin/client/task-list/tasks.js @@ -4,7 +4,6 @@ import { __ } from '@wordpress/i18n'; import { applyFilters } from '@wordpress/hooks'; -import { get } from 'lodash'; /** * WooCommerce dependencies @@ -25,7 +24,7 @@ import Payments from './tasks/payments'; export function getAllTasks( { profileItems, - options, + taskListPayments, query, toggleCartModal, installedPlugins, @@ -55,15 +54,11 @@ export function getAllTasks( { installedPlugins ); - const paymentsCompleted = get( - options, - [ 'woocommerce_task_list_payments', 'completed' ], - false + const paymentsCompleted = Boolean( + taskListPayments && taskListPayments.completed ); - const paymentsSkipped = get( - options, - [ 'woocommerce_task_list_payments', 'skipped' ], - false + const paymentsSkipped = Boolean( + taskListPayments && taskListPayments.skipped ); const tasks = [ diff --git a/plugins/woocommerce-admin/client/task-list/tasks/appearance.js b/plugins/woocommerce-admin/client/task-list/tasks/appearance.js index 9dbdfd38020..d54881fbc82 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks/appearance.js +++ b/plugins/woocommerce-admin/client/task-list/tasks/appearance.js @@ -6,8 +6,8 @@ import apiFetch from '@wordpress/api-fetch'; import { Button } from '@wordpress/components'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; -import { difference, filter } from 'lodash'; -import { withDispatch } from '@wordpress/data'; +import { filter } from 'lodash'; +import { withDispatch, withSelect } from '@wordpress/data'; /** * WooCommerce dependencies @@ -20,13 +20,13 @@ import { } from '@woocommerce/components'; import { getHistory, getNewPath } from '@woocommerce/navigation'; import { getSetting, setSetting } from '@woocommerce/wc-admin-settings'; +import { OPTIONS_STORE_NAME } from '@woocommerce/data'; /** * Internal dependencies */ import { queueRecordEvent, recordEvent } from 'lib/tracks'; import { WC_ADMIN_NAMESPACE } from 'wc-api/constants'; -import withSelect from 'wc-api/with-select'; class Appearance extends Component { constructor( props ) { @@ -43,7 +43,9 @@ class Appearance extends Component { isPending: false, logo: null, stepIndex: 0, - storeNoticeText: props.options.woocommerce_demo_store_notice || '', + isUpdatingLogo: false, + isUpdatingNotice: false, + storeNoticeText: props.demoStoreNotice || '', }; this.completeStep = this.completeStep.bind( this ); @@ -63,18 +65,9 @@ class Appearance extends Component { } } - async componentDidUpdate( prevProps ) { - const { isPending, logo, stepIndex } = this.state; - const { - createNotice, - errors, - hasErrors, - isRequesting, - options, - } = this.props; - const step = this.getSteps()[ stepIndex ].key; - const isRequestSuccessful = - ! isRequesting && prevProps.isRequesting && ! hasErrors; + componentDidUpdate( prevProps ) { + const { isPending, logo } = this.state; + const { demoStoreNotice } = this.props; if ( logo && ! logo.url && ! isPending ) { /* eslint-disable react/no-did-update-set-state */ @@ -93,38 +86,15 @@ class Appearance extends Component { } if ( - options.woocommerce_demo_store_notice && - prevProps.options.woocommerce_demo_store_notice !== - options.woocommerce_demo_store_notice + demoStoreNotice && + prevProps.demoStoreNotice !== demoStoreNotice ) { /* eslint-disable react/no-did-update-set-state */ this.setState( { - storeNoticeText: options.woocommerce_demo_store_notice, + storeNoticeText: demoStoreNotice, } ); /* eslint-enable react/no-did-update-set-state */ } - - if ( step === 'logo' && isRequestSuccessful ) { - createNotice( - 'success', - __( 'Store logo updated sucessfully.', 'woocommerce-admin' ) - ); - this.completeStep(); - } - - if ( step === 'notice' && isRequestSuccessful ) { - createNotice( - 'success', - __( - "🎨 Your store is looking great! Don't forget to continue personalizing it.", - 'woocommerce-admin' - ) - ); - this.completeStep(); - } - - const newErrors = difference( errors, prevProps.errors ); - newErrors.map( ( error ) => createNotice( 'error', error ) ); } completeStep() { @@ -222,8 +192,8 @@ class Appearance extends Component { } ); } - updateLogo() { - const { updateOptions } = this.props; + async updateLogo() { + const { updateOptions, createNotice } = this.props; const { logo } = this.state; const { stylesheet, themeMods } = getSetting( 'onboarding', {} ); const updatedThemeMods = { @@ -238,13 +208,25 @@ class Appearance extends Component { themeMods: updatedThemeMods, } ); - updateOptions( { + this.setState( { isUpdatingLogo: true } ); + const update = await updateOptions( { [ `theme_mods_${ stylesheet }` ]: updatedThemeMods, } ); + + if ( update.success ) { + this.setState( { isUpdatingLogo: false } ); + createNotice( + 'success', + __( 'Store logo updated sucessfully.', 'woocommerce-admin' ) + ); + this.completeStep(); + } else { + createNotice( 'error', update.message ); + } } - updateNotice() { - const { updateOptions } = this.props; + async updateNotice() { + const { updateOptions, createNotice } = this.props; const { storeNoticeText } = this.state; recordEvent( 'tasklist_appearance_set_store_notice', { @@ -256,16 +238,36 @@ class Appearance extends Component { isAppearanceComplete: true, } ); - updateOptions( { + this.setState( { isUpdatingNotice: true } ); + const update = await updateOptions( { woocommerce_task_list_appearance_complete: true, woocommerce_demo_store: storeNoticeText.length ? 'yes' : 'no', woocommerce_demo_store_notice: storeNoticeText, } ); + + if ( update.success ) { + this.setState( { isUpdatingNotice: false } ); + createNotice( + 'success', + __( + "🎨 Your store is looking great! Don't forget to continue personalizing it.", + 'woocommerce-admin' + ) + ); + this.completeStep(); + } else { + createNotice( 'error', update.message ); + } } getSteps() { - const { isDirty, isPending, logo, storeNoticeText } = this.state; - const { isRequesting } = this.props; + const { + isDirty, + isPending, + logo, + storeNoticeText, + isUpdatingLogo, + } = this.state; const steps = [ { @@ -340,7 +342,7 @@ class Appearance extends Component { ) : ( - ) } @@ -357,18 +355,15 @@ class Payments extends Component { export default compose( withSelect( ( select ) => { const { getProfileItems } = select( ONBOARDING_STORE_NAME ); - const { - getOptions, - getUpdateOptionsError, - isUpdateOptionsRequesting, - } = select( 'wc-api' ); - + const { getOption, isOptionsUpdating } = select( OPTIONS_STORE_NAME ); const { getActivePlugins, isJetpackConnected } = select( PLUGINS_STORE_NAME ); + const activePlugins = getActivePlugins(); const profileItems = getProfileItems(); - const options = getOptions( [ + + const optionNames = [ 'woocommerce_default_country', 'woocommerce_woocommerce_payments_settings', 'woocommerce_stripe_settings', @@ -381,7 +376,12 @@ export default compose( 'woocommerce_cod_settings', 'woocommerce_bacs_settings', 'woocommerce_bacs_accounts', - ] ); + ]; + + const options = optionNames.reduce( ( result, name ) => { + result[ name ] = getOption( name ); + return result; + }, {} ); const countryCode = getCountryCode( options.woocommerce_default_country ); @@ -394,20 +394,10 @@ export default compose( profileItems, } ); - const errors = {}; - const requesting = {}; - methods.forEach( ( method ) => { - errors[ method.key ] = Boolean( - getUpdateOptionsError( [ method.optionName ] ) - ); - requesting[ method.key ] = Boolean( - isUpdateOptionsRequesting( [ method.optionName ] ) - ); - } ); + const requesting = isOptionsUpdating(); return { countryCode, - errors, profileItems, activePlugins, options, @@ -417,7 +407,7 @@ export default compose( } ), withDispatch( ( dispatch ) => { const { createNotice } = dispatch( 'core/notices' ); - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { createNotice, updateOptions, diff --git a/plugins/woocommerce-admin/client/task-list/tasks/payments/payfast.js b/plugins/woocommerce-admin/client/task-list/tasks/payments/payfast.js index 65d7aac89f6..fc7042ff27e 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks/payments/payfast.js +++ b/plugins/woocommerce-admin/client/task-list/tasks/payments/payfast.js @@ -6,17 +6,13 @@ import { Component, Fragment } from '@wordpress/element'; import { Button } from '@wordpress/components'; import interpolateComponents from 'interpolate-components'; import { compose } from '@wordpress/compose'; -import { withDispatch } from '@wordpress/data'; +import { withDispatch, withSelect } from '@wordpress/data'; /** * WooCommerce dependencies */ import { Form, Link, Stepper, TextControl } from '@woocommerce/components'; - -/** - * Internal dependencies - */ -import withSelect from 'wc-api/with-select'; +import { OPTIONS_STORE_NAME } from '@woocommerce/data'; class PayFast extends Component { getInitialConfigValues = () => { @@ -54,39 +50,12 @@ class PayFast extends Component { return errors; }; - componentDidUpdate( prevProps ) { - const { - createNotice, - isOptionsRequesting, - hasOptionsError, - markConfigured, - } = this.props; - - if ( prevProps.isOptionsRequesting && ! isOptionsRequesting ) { - if ( ! hasOptionsError ) { - markConfigured( 'payfast' ); - createNotice( - 'success', - __( 'PayFast connected successfully', 'woocommerce-admin' ) - ); - } else { - createNotice( - 'error', - __( - 'There was a problem saving your payment setings', - 'woocommerce-admin' - ) - ); - } - } - } - - updateSettings = ( values ) => { - const { updateOptions } = this.props; + updateSettings = async ( values ) => { + const { updateOptions, createNotice, markConfigured } = this.props; // Because the PayFast extension only works with the South African Rand // currency, force the store to use it while setting the PayFast settings - updateOptions( { + const update = await updateOptions( { woocommerce_currency: 'ZAR', woocommerce_payfast_settings: { merchant_id: values.merchant_id, @@ -95,6 +64,22 @@ class PayFast extends Component { enabled: 'yes', }, } ); + + if ( update.success ) { + markConfigured( 'payfast' ); + createNotice( + 'success', + __( 'PayFast connected successfully', 'woocommerce-admin' ) + ); + } else { + createNotice( + 'error', + __( + 'There was a problem saving your payment setings', + 'woocommerce-admin' + ) + ); + } }; renderConnectStep() { @@ -190,28 +175,16 @@ class PayFast extends Component { export default compose( withSelect( ( select ) => { - const { getOptionsError, isUpdateOptionsRequesting } = select( - 'wc-api' - ); - const isOptionsRequesting = Boolean( - isUpdateOptionsRequesting( [ - 'woocommerce_currency', - 'woocommerce_payfast_settings', - ] ) - ); - const hasOptionsError = getOptionsError( [ - 'woocommerce_currency', - 'woocommerce_payfast_settings', - ] ); + const { isOptionsUpdating } = select( OPTIONS_STORE_NAME ); + const isOptionsRequesting = isOptionsUpdating(); return { - hasOptionsError, isOptionsRequesting, }; } ), withDispatch( ( dispatch ) => { const { createNotice } = dispatch( 'core/notices' ); - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { createNotice, diff --git a/plugins/woocommerce-admin/client/task-list/tasks/payments/paypal.js b/plugins/woocommerce-admin/client/task-list/tasks/payments/paypal.js index a4fac2b1981..20aba7a9cd3 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks/payments/paypal.js +++ b/plugins/woocommerce-admin/client/task-list/tasks/payments/paypal.js @@ -7,7 +7,7 @@ import { Button } from '@wordpress/components'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import interpolateComponents from 'interpolate-components'; -import { withDispatch } from '@wordpress/data'; +import { withDispatch, withSelect } from '@wordpress/data'; /** * WooCommerce dependencies @@ -15,8 +15,7 @@ import { withDispatch } from '@wordpress/data'; import { Form, Link, Stepper, TextControl } from '@woocommerce/components'; import { getQuery } from '@woocommerce/navigation'; import { WC_ADMIN_NAMESPACE } from 'wc-api/constants'; -import withSelect from 'wc-api/with-select'; -import { PLUGINS_STORE_NAME } from '@woocommerce/data'; +import { PLUGINS_STORE_NAME, OPTIONS_STORE_NAME } from '@woocommerce/data'; class PayPal extends Component { constructor( props ) { @@ -119,13 +118,12 @@ class PayPal extends Component { async updateSettings( values ) { const { createNotice, - isSettingsError, options, updateOptions, markConfigured, } = this.props; - await updateOptions( { + const update = await updateOptions( { woocommerce_ppec_paypal_settings: { ...options.woocommerce_ppec_paypal_settings, api_username: values.api_username, @@ -134,7 +132,7 @@ class PayPal extends Component { }, } ); - if ( ! isSettingsError ) { + if ( update.success ) { createNotice( 'success', __( 'PayPal connected successfully.', 'woocommerce-admin' ) @@ -178,7 +176,7 @@ class PayPal extends Component { } renderManualConfig() { - const { isOptionsRequesting } = this.props; + const { isOptionsUpdating } = this.props; const link = ( { __( 'Proceed', 'woocommerce-admin' ) } @@ -291,23 +289,20 @@ PayPal.defaultProps = { export default compose( withSelect( ( select ) => { - const { getOptions, isGetOptionsRequesting } = select( 'wc-api' ); + const { getOption, isOptionsUpdating } = select( OPTIONS_STORE_NAME ); const { getActivePlugins } = select( PLUGINS_STORE_NAME ); - const options = getOptions( [ 'woocommerce_ppec_paypal_settings' ] ); - const isOptionsRequesting = Boolean( - isGetOptionsRequesting( [ 'woocommerce_ppec_paypal_settings' ] ) - ); + const options = getOption( 'woocommerce_ppec_paypal_settings' ); const activePlugins = getActivePlugins(); return { activePlugins, options, - isOptionsRequesting, + isOptionsUpdating: isOptionsUpdating(), }; } ), withDispatch( ( dispatch ) => { const { createNotice } = dispatch( 'core/notices' ); - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { createNotice, updateOptions, diff --git a/plugins/woocommerce-admin/client/task-list/tasks/payments/square.js b/plugins/woocommerce-admin/client/task-list/tasks/payments/square.js index a1eba4c43bd..f899d11f3a2 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks/payments/square.js +++ b/plugins/woocommerce-admin/client/task-list/tasks/payments/square.js @@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n'; import { Component, Fragment } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; import { Button } from '@wordpress/components'; -import { withDispatch } from '@wordpress/data'; +import { withDispatch, withSelect } from '@wordpress/data'; import { compose } from '@wordpress/compose'; /** @@ -13,9 +13,9 @@ import { compose } from '@wordpress/compose'; */ import { getQuery } from '@woocommerce/navigation'; import { WC_ADMIN_NAMESPACE } from 'wc-api/constants'; -import withSelect from 'wc-api/with-select'; import { Stepper } from '@woocommerce/components'; import { getAdminLink } from '@woocommerce/wc-admin-settings'; +import { OPTIONS_STORE_NAME } from '@woocommerce/data'; class Square extends Component { constructor( props ) { @@ -145,15 +145,11 @@ class Square extends Component { export default compose( withSelect( ( select ) => { - const { getOptions, isGetOptionsRequesting } = select( 'wc-api' ); - const options = getOptions( [ + const { getOption, isResolving } = select( OPTIONS_STORE_NAME ); + const options = getOption( 'woocommerce_square_credit_card_settings' ); + const optionsIsRequesting = isResolving( 'getOption', [ 'woocommerce_square_credit_card_settings', ] ); - const optionsIsRequesting = Boolean( - isGetOptionsRequesting( [ - 'woocommerce_square_credit_card_settings', - ] ) - ); return { options, @@ -162,7 +158,7 @@ export default compose( } ), withDispatch( ( dispatch ) => { const { createNotice } = dispatch( 'core/notices' ); - const { updateOptions } = dispatch( 'wc-api' ); + const { updateOptions } = dispatch( OPTIONS_STORE_NAME ); return { createNotice, updateOptions, diff --git a/plugins/woocommerce-admin/client/task-list/tasks/payments/stripe.js b/plugins/woocommerce-admin/client/task-list/tasks/payments/stripe.js index e8c78f6c32c..438f1d049fe 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks/payments/stripe.js +++ b/plugins/woocommerce-admin/client/task-list/tasks/payments/stripe.js @@ -5,10 +5,9 @@ import { __ } from '@wordpress/i18n'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import apiFetch from '@wordpress/api-fetch'; -import { withDispatch } from '@wordpress/data'; +import { withDispatch, withSelect } from '@wordpress/data'; import interpolateComponents from 'interpolate-components'; import { Button } from '@wordpress/components'; -import { get } from 'lodash'; /** * WooCommerce dependencies @@ -17,8 +16,7 @@ import { Form, Link, Stepper, TextControl } from '@woocommerce/components'; import { getAdminLink } from '@woocommerce/wc-admin-settings'; import { getQuery } from '@woocommerce/navigation'; import { WCS_NAMESPACE } from 'wc-api/constants'; -import withSelect from 'wc-api/with-select'; -import { PLUGINS_STORE_NAME } from '@woocommerce/data'; +import { PLUGINS_STORE_NAME, OPTIONS_STORE_NAME } from '@woocommerce/data'; class Stripe extends Component { constructor( props ) { @@ -54,26 +52,7 @@ class Stripe extends Component { } componentDidUpdate( prevProps ) { - const { - activePlugins, - createNotice, - isOptionsRequesting, - hasOptionsError, - } = this.props; - - if ( prevProps.isOptionsRequesting && ! isOptionsRequesting ) { - if ( ! hasOptionsError ) { - this.completeMethod(); - } else { - createNotice( - 'error', - __( - 'There was a problem saving your payment setings', - 'woocommerce-admin' - ) - ); - } - } + const { activePlugins } = this.props; if ( ! prevProps.activePlugins.includes( @@ -154,10 +133,10 @@ class Stripe extends Component { ); } - updateSettings( values ) { - const { updateOptions, stripeSettings } = this.props; + async updateSettings( values ) { + const { updateOptions, stripeSettings, createNotice } = this.props; - updateOptions( { + const update = await updateOptions( { woocommerce_stripe_settings: { ...stripeSettings, publishable_key: values.publishable_key, @@ -165,6 +144,18 @@ class Stripe extends Component { enabled: 'yes', }, } ); + + if ( update.success ) { + this.completeMethod(); + } else { + createNotice( + 'error', + __( + 'There was a problem saving your payment setings', + 'woocommerce-admin' + ) + ); + } } getInitialConfigValues() { @@ -194,7 +185,7 @@ class Stripe extends Component { } renderManualConfig() { - const { isOptionsRequesting } = this.props; + const { isOptionsUpdating } = this.props; const stripeHelp = interpolateComponents( { mixedString: __( 'Your API details can be obtained from your {{docsLink}}Stripe account{{/docsLink}}. Don’t have a Stripe account? {{registerLink}}Create one.{{/registerLink}}', @@ -246,7 +237,7 @@ class Stripe extends Component {