2019-06-26 02:22:44 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import { Button } from 'newspack-components';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2019-06-28 07:42:20 +00:00
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import { TabPanel, Tooltip } from '@wordpress/components';
|
2019-06-26 02:22:44 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { Card, H } from '@woocommerce/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal depdencies
|
|
|
|
*/
|
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
import './style.scss';
|
2019-07-01 18:13:29 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-07-08 02:54:26 +00:00
|
|
|
import ThemeUploader from './uploader';
|
2019-08-01 23:27:38 +00:00
|
|
|
import ThemePreview from './preview';
|
2019-06-26 02:22:44 +00:00
|
|
|
|
|
|
|
class Theme extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
activeTab: 'all',
|
2019-08-01 23:27:38 +00:00
|
|
|
demo: null,
|
2019-07-08 02:54:26 +00:00
|
|
|
uploadedThemes: [],
|
2019-06-26 02:22:44 +00:00
|
|
|
};
|
|
|
|
|
2019-07-08 02:54:26 +00:00
|
|
|
this.handleUploadComplete = this.handleUploadComplete.bind( this );
|
2019-06-26 02:22:44 +00:00
|
|
|
this.onChoose = this.onChoose.bind( this );
|
2019-08-01 23:27:38 +00:00
|
|
|
this.onClosePreview = this.onClosePreview.bind( this );
|
2019-06-26 02:22:44 +00:00
|
|
|
this.onSelectTab = this.onSelectTab.bind( this );
|
|
|
|
this.openDemo = this.openDemo.bind( this );
|
|
|
|
}
|
|
|
|
|
2019-08-01 23:27:38 +00:00
|
|
|
async onChoose( theme, location = '' ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice, goToNextStep, isError, updateProfileItems } = this.props;
|
2019-06-26 02:22:44 +00:00
|
|
|
|
2019-08-01 23:27:38 +00:00
|
|
|
recordEvent( 'storeprofiler_store_theme_choose', { theme, location } );
|
2019-06-26 02:22:44 +00:00
|
|
|
await updateProfileItems( { theme } );
|
|
|
|
|
|
|
|
if ( ! isError ) {
|
|
|
|
// @todo This should send profile information to woocommerce.com.
|
|
|
|
goToNextStep();
|
|
|
|
} else {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__( 'There was a problem selecting your store theme.', 'woocommerce-admin' )
|
|
|
|
);
|
2019-06-26 02:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 23:27:38 +00:00
|
|
|
onClosePreview() {
|
|
|
|
const { demo } = this.state;
|
|
|
|
recordEvent( 'storeprofiler_store_theme_demo_close', { theme: demo.slug } );
|
|
|
|
document.body.classList.remove( 'woocommerce-theme-preview-active' );
|
|
|
|
this.setState( { demo: null } );
|
|
|
|
}
|
2019-07-01 18:13:29 +00:00
|
|
|
|
2019-08-01 23:27:38 +00:00
|
|
|
openDemo( theme ) {
|
|
|
|
recordEvent( 'storeprofiler_store_theme_live_demo', { theme: theme.slug } );
|
|
|
|
document.body.classList.add( 'woocommerce-theme-preview-active' );
|
|
|
|
this.setState( { demo: theme } );
|
2019-06-26 02:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTheme( theme ) {
|
2019-06-28 07:42:20 +00:00
|
|
|
const { demo_url, has_woocommerce_support, image, slug, title } = theme;
|
2019-06-26 02:22:44 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Card className="woocommerce-profile-wizard__theme" key={ theme.slug }>
|
|
|
|
{ image && (
|
|
|
|
<img alt={ title } src={ image } className="woocommerce-profile-wizard__theme-image" />
|
|
|
|
) }
|
|
|
|
<div className="woocommerce-profile-wizard__theme-details">
|
2019-06-28 07:42:20 +00:00
|
|
|
<H className="woocommerce-profile-wizard__theme-name">
|
|
|
|
{ title }
|
|
|
|
{ ! has_woocommerce_support && (
|
|
|
|
<Tooltip
|
|
|
|
text={ __( 'This theme does not support WooCommerce.', 'woocommerce-admin' ) }
|
|
|
|
>
|
|
|
|
<span>
|
|
|
|
<Gridicon icon="info" role="img" aria-hidden="true" focusable="false" />
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
|
|
|
) }
|
|
|
|
</H>
|
|
|
|
<p className="woocommerce-profile-wizard__theme-status">
|
|
|
|
{ this.getThemeStatus( theme ) }
|
2019-06-26 02:22:44 +00:00
|
|
|
</p>
|
|
|
|
<div className="woocommerce-profile-wizard__theme-actions">
|
2019-06-28 07:42:20 +00:00
|
|
|
<Button
|
|
|
|
isPrimary={ Boolean( demo_url ) }
|
|
|
|
isDefault={ ! Boolean( demo_url ) }
|
2019-08-01 23:27:38 +00:00
|
|
|
onClick={ () => this.onChoose( slug, 'card' ) }
|
2019-06-28 07:42:20 +00:00
|
|
|
>
|
2019-06-26 02:22:44 +00:00
|
|
|
{ __( 'Choose', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
2019-06-28 07:42:20 +00:00
|
|
|
{ demo_url && (
|
2019-08-01 23:27:38 +00:00
|
|
|
<Button isDefault onClick={ () => this.openDemo( theme ) }>
|
2019-06-28 07:42:20 +00:00
|
|
|
{ __( 'Live Demo', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
2019-06-26 02:22:44 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:42:20 +00:00
|
|
|
getThemeStatus( theme ) {
|
2019-07-04 06:12:33 +00:00
|
|
|
const { is_installed, price, slug } = theme;
|
2019-06-28 07:42:20 +00:00
|
|
|
const { activeTheme } = wcSettings.onboarding;
|
|
|
|
|
|
|
|
if ( activeTheme === slug ) {
|
|
|
|
return __( 'Currently active theme', 'woocommerce-admin' );
|
|
|
|
}
|
2019-07-04 06:12:33 +00:00
|
|
|
if ( is_installed ) {
|
2019-06-28 07:42:20 +00:00
|
|
|
return __( 'Installed', 'woocommerce-admin' );
|
|
|
|
} else if ( this.getPriceValue( price ) <= 0 ) {
|
|
|
|
return __( 'Free', 'woocommerce-admin' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf( __( '%s per year', 'woocommerce-admin' ), decodeEntities( price ) );
|
|
|
|
}
|
|
|
|
|
2019-06-26 02:22:44 +00:00
|
|
|
onSelectTab( tab ) {
|
2019-07-01 18:13:29 +00:00
|
|
|
recordEvent( 'storeprofiler_store_theme_navigate', { navigation: tab } );
|
2019-06-26 02:22:44 +00:00
|
|
|
this.setState( { activeTab: tab } );
|
|
|
|
}
|
|
|
|
|
|
|
|
getPriceValue( string ) {
|
|
|
|
return Number( decodeEntities( string ).replace( /[^0-9.-]+/g, '' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
getThemes() {
|
2019-07-08 02:54:26 +00:00
|
|
|
const { activeTab, uploadedThemes } = this.state;
|
2019-06-26 02:22:44 +00:00
|
|
|
const { themes } = wcSettings.onboarding;
|
2019-07-08 02:54:26 +00:00
|
|
|
themes.concat( uploadedThemes );
|
|
|
|
const allThemes = [ ...themes, ...uploadedThemes ];
|
2019-06-26 02:22:44 +00:00
|
|
|
|
|
|
|
switch ( activeTab ) {
|
|
|
|
case 'paid':
|
2019-07-08 02:54:26 +00:00
|
|
|
return allThemes.filter( theme => this.getPriceValue( theme.price ) > 0 );
|
2019-06-26 02:22:44 +00:00
|
|
|
case 'free':
|
2019-07-08 02:54:26 +00:00
|
|
|
return allThemes.filter( theme => this.getPriceValue( theme.price ) <= 0 );
|
2019-06-26 02:22:44 +00:00
|
|
|
case 'all':
|
|
|
|
default:
|
2019-07-08 02:54:26 +00:00
|
|
|
return allThemes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUploadComplete( upload ) {
|
|
|
|
if ( 'success' === upload.status && upload.theme_data ) {
|
|
|
|
this.setState( {
|
|
|
|
uploadedThemes: [ ...this.state.uploadedThemes, upload.theme_data ],
|
|
|
|
} );
|
2019-08-01 23:27:38 +00:00
|
|
|
|
|
|
|
recordEvent( 'storeprofiler_store_theme_upload', { theme: upload.theme_data.slug } );
|
2019-06-26 02:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const themes = this.getThemes();
|
2019-08-01 23:27:38 +00:00
|
|
|
const { demo } = this.state;
|
2019-06-26 02:22:44 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<H className="woocommerce-profile-wizard__header-title">
|
|
|
|
{ __( 'Choose a theme', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
|
|
|
<H className="woocommerce-profile-wizard__header-subtitle">
|
2019-08-08 18:41:26 +00:00
|
|
|
{ __(
|
|
|
|
"Choose how your store appears to customers. And don't worry, you can always switch themes and edit them later.",
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-06-26 02:22:44 +00:00
|
|
|
</H>
|
|
|
|
<TabPanel
|
|
|
|
className="woocommerce-profile-wizard__themes-tab-panel"
|
|
|
|
activeClass="is-active"
|
|
|
|
onSelect={ this.onSelectTab }
|
|
|
|
tabs={ [
|
|
|
|
{
|
|
|
|
name: 'all',
|
|
|
|
title: __( 'All themes', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'paid',
|
|
|
|
title: __( 'Paid themes', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'free',
|
|
|
|
title: __( 'Free themes', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
>
|
|
|
|
{ () => (
|
|
|
|
<div className="woocommerce-profile-wizard__themes">
|
2019-06-28 07:42:20 +00:00
|
|
|
{ themes && themes.map( theme => this.renderTheme( theme ) ) }
|
2019-07-08 02:54:26 +00:00
|
|
|
<ThemeUploader onUploadComplete={ this.handleUploadComplete } />
|
2019-06-26 02:22:44 +00:00
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</TabPanel>
|
2019-08-01 23:27:38 +00:00
|
|
|
{ demo && (
|
|
|
|
<ThemePreview theme={ demo } onChoose={ this.onChoose } onClose={ this.onClosePreview } />
|
|
|
|
) }
|
2019-06-26 02:22:44 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
|
|
|
const { getProfileItemsError } = select( 'wc-api' );
|
|
|
|
|
|
|
|
const isError = Boolean( getProfileItemsError() );
|
|
|
|
|
|
|
|
return { isError };
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { updateProfileItems } = dispatch( 'wc-api' );
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2019-06-26 02:22:44 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2019-06-26 02:22:44 +00:00
|
|
|
updateProfileItems,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Theme );
|