Skip to content

Commit

Permalink
Themes: Add current-theme query component
Browse files Browse the repository at this point in the history
Replaces the old-style fetcher component.
  • Loading branch information
seear committed May 12, 2016
1 parent c65ca89 commit 85aa01e
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 91 deletions.
8 changes: 0 additions & 8 deletions client/components/data/current-theme/README.md

This file was deleted.

65 changes: 0 additions & 65 deletions client/components/data/current-theme/index.js

This file was deleted.

52 changes: 52 additions & 0 deletions client/components/data/query-current-theme/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/** @ssr-ready **/

/**
* External dependencies
*/
import { Component, PropTypes } from 'react';
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
import { fetchCurrentTheme } from 'state/themes/actions';
import { isRequestingCurrentTheme } from 'state/themes/current-theme/selectors';

class QueryCurrentTheme extends Component {

componentDidMount() {
const { requestingCurrentTheme, site } = this.props;
this.refresh( requestingCurrentTheme, site );
}

componentWillReceiveProps( nextProps ) {
if ( nextProps.site === this.props.site ) {
return;
}
const { requestingCurrentTheme, site } = nextProps;
this.refresh( requestingCurrentTheme, site );
}

refresh( requestingCurrentTheme, site ) {
if ( ! requestingCurrentTheme && site ) {
this.props.fetchCurrentTheme( site.ID );
}
}

render() {
return null;
}
}

QueryCurrentTheme.propTypes = {
site: PropTypes.oneOfType( [ PropTypes.object, PropTypes.bool ] ),
requestingCurrentTheme: PropTypes.bool,
fetchCurrentTheme: PropTypes.func,
};

export default connect(
( state, props ) => (
{ requestingCurrentTheme: isRequestingCurrentTheme( state, props.site && props.site.ID ) }
),
{ fetchCurrentTheme }
)( QueryCurrentTheme );
12 changes: 10 additions & 2 deletions client/my-sites/themes/current-theme/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
*/
var React = require( 'react' ),
classNames = require( 'classnames' );
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
var Card = require( 'components/card' ),
Helpers = require( '../helpers' ),
CurrentThemeButton = require( './button' );
import { getCurrentTheme } from 'state/themes/current-theme/selectors';
import QueryCurrentTheme from 'components/data/query-current-theme';

/**
* Show current active theme for a site, with
Expand All @@ -31,12 +34,13 @@ var CurrentTheme = React.createClass( {
render: function() {
var currentTheme = this.props.currentTheme,
placeholderText = <span className="current-theme__placeholder">loading...</span>,
text = currentTheme ? currentTheme.name : placeholderText,
text = currentTheme && currentTheme.name ? currentTheme.name : placeholderText,
site = this.props.site,
displaySupportButton = Helpers.isPremium( currentTheme ) && ! site.jetpack;

return (
<Card className="current-theme">
<QueryCurrentTheme site={ site }/>
<div className="current-theme__info">
<span className="current-theme__label">
{ this.translate( 'Current Theme' ) }
Expand Down Expand Up @@ -70,4 +74,8 @@ var CurrentTheme = React.createClass( {
}
} );

module.exports = CurrentTheme;
export default connect(
( state, props ) => (
{ currentTheme: getCurrentTheme( state, props.site && props.site.ID ) }
)
)( CurrentTheme );
9 changes: 3 additions & 6 deletions client/my-sites/themes/single-site.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var React = require( 'react' ),
* Internal dependencies
*/
var Main = require( 'components/main' ),
CurrentThemeData = require( 'components/data/current-theme' ),
ActivatingTheme = require( 'components/data/activating-theme' ),
Action = require( 'state/themes/actions' ),
ThemePreview = require( './theme-preview' ),
Expand Down Expand Up @@ -153,11 +152,9 @@ var ThemesSingleSite = React.createClass( {
source={ 'list' }
clearActivated={ bindActionCreators( Action.clearActivated, this.props.dispatch ) } />
</ActivatingTheme>
<CurrentThemeData site={ site }>
<CurrentTheme
site={ site }
canCustomize={ site && site.isCustomizable() } />
</CurrentThemeData>
<CurrentTheme
site={ site }
canCustomize={ site && site.isCustomizable() } />
<UpgradeNudge
title={ this.translate( 'Get Custom Design with Premium' ) }
message={ this.translate( 'Customize your theme using premium fonts, color palettes, and the CSS editor.' ) }
Expand Down
2 changes: 2 additions & 0 deletions client/state/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ export const THEME_CUSTOMIZE = 'THEME_CUSTOMIZE';
export const THEME_DETAILS_RECEIVE = 'THEME_DETAILS_RECEIVE';
export const THEME_PURCHASE = 'THEME_PURCHASE';
export const THEME_RECEIVE_CURRENT = 'THEMES_RECEIVE_CURRENT';
export const THEME_REQUEST_CURRENT = 'THEME_REQUEST_CURRENT';
export const THEME_REQUEST_CURRENT_FAILURE = 'THEME_REQUEST_CURRENT_FAILURE';
export const THEME_SIGNUP_WITH = 'THEME_SIGNUP_WITH';
export const THEMES_INCREMENT_PAGE = 'THEMES_INCREMENT_PAGE';
export const THEMES_QUERY = 'THEMES_QUERY';
Expand Down
21 changes: 17 additions & 4 deletions client/state/themes/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
THEME_DETAILS_RECEIVE,
THEME_PURCHASE,
THEME_RECEIVE_CURRENT,
THEME_REQUEST_CURRENT,
THEME_REQUEST_CURRENT_FAILURE,
THEME_SIGNUP_WITH,
THEMES_INCREMENT_PAGE,
THEMES_QUERY,
Expand Down Expand Up @@ -75,19 +77,30 @@ export function incrementThemesPage( site ) {
};
}

export function fetchCurrentTheme( site ) {
export function fetchCurrentTheme( siteId ) {
return dispatch => {
wpcom.undocumented().activeTheme( site.ID )
dispatch( {
type: THEME_REQUEST_CURRENT,
siteId,
} );

wpcom.undocumented().activeTheme( siteId )
.then( theme => {
debug( 'Received current theme', theme );
dispatch( {
type: THEME_RECEIVE_CURRENT,
site: site,
siteId,
themeId: theme.id,
themeName: theme.name,
themeCost: theme.cost
} );
} ); // TODO: add .catch() error handler
} ).catch( error => {
dispatch( {
type: THEME_REQUEST_CURRENT_FAILURE,
siteId,
error,
} );
} );
};
}

Expand Down
21 changes: 15 additions & 6 deletions client/state/themes/current-theme/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,39 @@ import {
THEME_ACTIVATE,
THEME_ACTIVATED,
THEME_CLEAR_ACTIVATED,
THEME_RECEIVE_CURRENT
THEME_RECEIVE_CURRENT,
THEME_REQUEST_CURRENT,
THEME_REQUEST_CURRENT_FAILURE,
} from 'state/action-types';

export const initialState = fromJS( {
isActivating: false,
hasActivated: false,
currentThemes: {}
currentThemes: {},
requesting: {},
} );

export default ( state = initialState, action ) => {
switch ( action.type ) {
case THEME_RECEIVE_CURRENT:
return state.setIn( [ 'currentThemes', action.site.ID ], {
return state.setIn( [ 'currentThemes', action.siteId ], {
name: action.themeName,
id: action.themeId,
cost: action.themeCost
} );
cost: action.themeCost,
} )
.setIn( [ 'requesting', action.siteId ], false );
case THEME_REQUEST_CURRENT:
return state.setIn( [ 'requesting', action.siteId ], true );
case THEME_REQUEST_CURRENT_FAILURE:
//TODO: show notification
return state.setIn( [ 'requesting', action.siteId ], false );
case THEME_ACTIVATE:
return state.set( 'isActivating', true );
case THEME_ACTIVATED:
return state
.set( 'isActivating', false )
.set( 'hasActivated', true )
.setIn( [ 'currentThemes', action.site.ID ], action.theme );
.setIn( [ 'currentThemes', action.siteId ], action.theme );
case THEME_CLEAR_ACTIVATED:
return state.set( 'hasActivated', false );
case DESERIALIZE:
Expand Down
4 changes: 4 additions & 0 deletions client/state/themes/current-theme/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export function isActivating( state ) {
export function hasActivated( state ) {
return state.themes.currentTheme.get( 'hasActivated' );
}

export function isRequestingCurrentTheme( state, siteId ) {
return !! state.themes.currentTheme.get( 'requesting' ).get( siteId );
}

0 comments on commit 85aa01e

Please sign in to comment.