diff --git a/client/signup/steps/dss/index.jsx b/client/signup/steps/dss/index.jsx index 5f3580cf2eac0..3d4215c327855 100644 --- a/client/signup/steps/dss/index.jsx +++ b/client/signup/steps/dss/index.jsx @@ -14,6 +14,7 @@ import StepWrapper from 'signup/step-wrapper'; import ThemeHelper from 'lib/themes/helpers'; import DynamicScreenshotsActions from 'lib/dss/actions'; import DSSImageStore from 'lib/dss/image-store'; +import ThemePreviewStore from 'lib/dss/preview-store'; import ImagePreloader from 'components/image-preloader'; const debug = debugFactory( 'calypso:dss' ); @@ -43,22 +44,39 @@ export default React.createClass( { return { isLoading: false, renderComplete: false, + markupAndStyles: {}, dssImage: null }; }, componentWillMount() { + ThemePreviewStore.on( 'change', this.updateMarkup ); DSSImageStore.on( 'change', this.updateScreenshot ); + this.loadThemePreviews(); }, componentWillUnmount() { + ThemePreviewStore.off( 'change', this.updateMarkup ); DSSImageStore.off( 'change', this.updateScreenshot ); }, + componentWillReceiveProps() { + this.loadThemePreviews(); + }, + + loadThemePreviews() { + debug( 'loading theme previews for these themes', this.props.themes ); + this.props.themes.map( theme => DynamicScreenshotsActions.fetchThemePreview( 'pub/' + ThemeHelper.getSlugFromName( theme ) ) ); + }, + + updateMarkup() { + this.setState( { markupAndStyles: ThemePreviewStore.get() } ); + }, + updateScreenshot() { const { isLoading, lastKey, imageResultsByKey } = DSSImageStore.get(); if ( ! imageResultsByKey[ lastKey ] ) { - return this.setState( Object.assign( {}, this.getInitialState(), { isLoading } ) ); + return this.setState( { isLoading, renderComplete: false, dssImage: null } ); } const dssImage = imageResultsByKey[ lastKey ]; this.setState( { isLoading, dssImage } ); @@ -114,6 +132,9 @@ export default React.createClass( { themeName={ theme } themeSlug={ ThemeHelper.getSlugFromName( theme ) } themeRepoSlug={ 'pub/' + ThemeHelper.getSlugFromName( theme ) } + isLoading={ this.state.isLoading } + dssImage={ this.state.dssImage } + markupAndStyles={ this.state.markupAndStyles[ 'pub/' + ThemeHelper.getSlugFromName( theme ) ] } renderComplete={ this.state.renderComplete } { ...this.props }/>; } ) } diff --git a/client/signup/steps/dss/screenshot.jsx b/client/signup/steps/dss/screenshot.jsx index d5cc39445041d..b812fb5ba63fa 100644 --- a/client/signup/steps/dss/screenshot.jsx +++ b/client/signup/steps/dss/screenshot.jsx @@ -2,18 +2,8 @@ * External dependencies */ import React from 'react'; -import debugFactory from 'debug'; import classnames from 'classnames'; -/** - * Internal dependencies - */ -import DynamicScreenshotsActions from 'lib/dss/actions'; -import ThemePreviewStore from 'lib/dss/preview-store'; -import DSSImageStore from 'lib/dss/image-store'; - -const debug = debugFactory( 'calypso:dss:screenshot' ); - function replaceMarkupWithImage( markup, imageUrl ) { return markup.replace( /(]+)src=['"][^'"]+['"]([^>]*>)/g, ( ...imgMatches ) => { if ( imgMatches.length < 3 ) { @@ -34,67 +24,45 @@ export default React.createClass( { screenshotUrl: React.PropTypes.string.isRequired, themeRepoSlug: React.PropTypes.string.isRequired, themeSlug: React.PropTypes.string.isRequired, + isLoading: React.PropTypes.bool, + dssImage: React.PropTypes.object, + markupAndStyles: React.PropTypes.object, renderComplete: React.PropTypes.bool }, - getInitialState() { - return { - isLoading: false, - markup: '', - styles: '', - dssImage: null - }; - }, - - componentWillMount() { - ThemePreviewStore.on( 'change', this.updateScreenshot ); - DSSImageStore.on( 'change', this.updateScreenshot ); - DynamicScreenshotsActions.fetchThemePreview( this.props.themeRepoSlug ); - }, - - componentWillUnmount() { - ThemePreviewStore.off( 'change', this.updateScreenshot ); - DSSImageStore.off( 'change', this.updateScreenshot ); - }, - getAdditionalStyles( imageUrl ) { return `#theme-${this.props.themeSlug} .hero.with-featured-image{background-image:url(${imageUrl});}`; }, - getMarkupAndStyles() { - if ( ! this.state.markup || ! this.state.styles ) { - return ThemePreviewStore.get()[ this.props.themeRepoSlug ] || { markup: null, styles: null }; - } - return { markup: this.state.markup, styles: this.state.styles }; + getPreviewStyles() { + return { __html: this.props.markupAndStyles.styles }; }, - updateScreenshot() { - const { isLoading, lastKey, imageResultsByKey } = DSSImageStore.get(); - if ( ! imageResultsByKey[ lastKey ] ) { - return this.setState( Object.assign( {}, this.getInitialState(), { isLoading } ) ); - } - const dssImage = imageResultsByKey[ lastKey ]; - debug( 'replacing images in ' + this.props.themeRepoSlug + ' screenshot with', dssImage.url ); - const { markup, styles } = this.getMarkupAndStyles(); - return this.setState( { dssImage, markup, styles, isLoading } ); + getPreviewAdditionalStyles() { + return { __html: this.props.dssImage ? this.getAdditionalStyles( this.props.dssImage.url ) : '' }; + }, + + getPreviewMarkup() { + return { __html: this.props.dssImage ? replaceMarkupWithImage( this.props.markupAndStyles.markup, this.props.dssImage.url ) : this.props.markupAndStyles.markup }; }, render() { + const { markup, styles } = this.props.markupAndStyles || { markup: null, styles: null }; const containerClassNames = classnames( 'dss-screenshot', { - 'is-loading': this.state.isLoading, - 'is-preview-ready': this.state.markup && this.state.styles && this.props.renderComplete + 'is-loading': this.props.isLoading, + 'is-preview-ready': markup && styles && this.props.renderComplete } ); - if ( this.state.markup && this.state.styles ) { + if ( markup && styles ) { return (
{
-