From 52c81230c38de12064c6a85d7e8180dc4bda430c Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Mon, 30 Dec 2019 18:11:50 +0800 Subject: [PATCH] Onboarding: Install free themes during profiler (https://github.com/woocommerce/woocommerce-admin/pull/3484) * Add theme name to theme activation response * Install and activate theme on choose in profiler * Only auto install free themes and not currently active themes * Move getPriceValue to dashboard utils * Don't add free themes to the cart --- .../profile-wizard/steps/theme/index.js | 64 ++++++++++++++++--- .../client/dashboard/utils.js | 14 +++- .../src/API/OnboardingThemes.php | 1 + 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/theme/index.js b/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/theme/index.js index cb6ee55aa60..53df1ff1fa3 100644 --- a/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/theme/index.js +++ b/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/theme/index.js @@ -3,6 +3,7 @@ * External dependencies */ import { __, sprintf } from '@wordpress/i18n'; +import apiFetch from '@wordpress/api-fetch'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import { decodeEntities } from '@wordpress/html-entities'; @@ -14,7 +15,7 @@ import { withDispatch } from '@wordpress/data'; * WooCommerce dependencies */ import { Card, H } from '@woocommerce/components'; -import { getSetting } from '@woocommerce/wc-admin-settings'; +import { getSetting, setSetting } from '@woocommerce/wc-admin-settings'; /** * Internal dependencies @@ -24,6 +25,7 @@ import './style.scss'; import { recordEvent } from 'lib/tracks'; import ThemeUploader from './uploader'; import ThemePreview from './preview'; +import { getPriceValue } from 'dashboard/utils'; class Theme extends Component { constructor() { @@ -68,12 +70,56 @@ class Theme extends Component { } } - async onChoose( theme, location = '' ) { + onChoose( theme, location = '' ) { const { updateProfileItems } = this.props; + const { price, slug } = theme; + const { activeTheme = '' } = getSetting( 'onboarding', {} ); - this.setState( { chosen: theme } ); - recordEvent( 'storeprofiler_store_theme_choose', { theme, location } ); - updateProfileItems( { theme } ); + this.setState( { chosen: slug } ); + recordEvent( 'storeprofiler_store_theme_choose', { theme: slug, location } ); + + if ( theme !== activeTheme && getPriceValue( price ) <= 0 ) { + this.installTheme( slug ); + } else { + updateProfileItems( { theme: slug } ); + } + } + + installTheme( slug ) { + const { createNotice } = this.props; + + apiFetch( { path: '/wc-admin/onboarding/themes/install?theme=' + slug, method: 'POST' } ) + .then( () => { + this.activateTheme( slug ); + } ) + .catch( response => { + this.setState( { chosen: null } ); + createNotice( 'error', response.message ); + } ); + } + + activateTheme( slug ) { + const { createNotice, updateProfileItems } = this.props; + + apiFetch( { path: '/wc-admin/onboarding/themes/activate?theme=' + slug, method: 'POST' } ) + .then( response => { + createNotice( + 'success', + sprintf( + __( '%s was installed and activated on your site.', 'woocommerce-admin' ), + response.name + ) + ); + setSetting( 'onboarding', { + ...getSetting( 'onboarding', {} ), + activeTheme: response.slug, + } ); + updateProfileItems( { theme: slug } ); + } ) + .catch( response => { + this.setState( { chosen: null } ); + createNotice( 'error', response.message ); + } ); } onClosePreview() { @@ -123,7 +169,7 @@ class Theme extends Component {