/** @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'; import { TabPanel } from '@wordpress/components'; 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'; class Theme extends Component { constructor() { super( ...arguments ); this.state = { activeTab: 'all', }; this.onChoose = this.onChoose.bind( this ); this.onSelectTab = this.onSelectTab.bind( this ); this.openDemo = this.openDemo.bind( this ); } async onChoose( theme ) { const { addNotice, goToNextStep, isError, updateProfileItems } = this.props; await updateProfileItems( { theme } ); if ( ! isError ) { // @todo This should send profile information to woocommerce.com. goToNextStep(); } else { addNotice( { status: 'error', message: __( 'There was a problem selecting your store theme.', 'woocommerce-admin' ), } ); } } openDemo() { // @todo This should open a theme demo preview. } renderTheme( theme ) { const { image, price, slug, title } = theme; return ( { image && ( { ) }
{ title }

{ this.getPriceValue( price ) <= 0 ? __( 'Free', 'woocommerce-admin' ) : sprintf( __( '%s per year', 'woocommerce-admin' ), decodeEntities( price ) ) }

); } onSelectTab( tab ) { this.setState( { activeTab: tab } ); } getPriceValue( string ) { return Number( decodeEntities( string ).replace( /[^0-9.-]+/g, '' ) ); } getThemes() { const { themes } = wcSettings.onboarding; const { activeTab } = this.state; switch ( activeTab ) { case 'paid': return themes.filter( theme => this.getPriceValue( theme.price ) > 0 ); case 'free': return themes.filter( theme => this.getPriceValue( theme.price ) <= 0 ); case 'all': default: return themes; } } render() { const themes = this.getThemes(); return ( { __( 'Choose a theme', 'woocommerce-admin' ) } { __( 'Your theme determines how your store appears to customers', 'woocommerce-admin' ) } { () => (
{ themes.map( theme => this.renderTheme( theme ) ) }
) }
); } } export default compose( withSelect( select => { const { getProfileItemsError } = select( 'wc-api' ); const isError = Boolean( getProfileItemsError() ); return { isError }; } ), withDispatch( dispatch => { const { addNotice, updateProfileItems } = dispatch( 'wc-api' ); return { addNotice, updateProfileItems, }; } ) )( Theme );