-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #840 from Automattic/try/themes-redux-transition
Themes: Switch data fetching to Redux
- Loading branch information
Showing
27 changed files
with
385 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,47 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
var React = require( 'react' ); | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import omit from 'lodash/object/omit'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
var CurrentThemeStore = require( 'lib/themes/stores/current-theme' ); | ||
|
||
function getState( props ) { | ||
return { | ||
isActivating: CurrentThemeStore.isActivating(), | ||
hasActivated: CurrentThemeStore.hasActivated(), | ||
currentTheme: CurrentThemeStore.getCurrentTheme( props.siteId ) | ||
}; | ||
} | ||
import { | ||
isActivating, | ||
hasActivated, | ||
getCurrentTheme | ||
} from 'lib/themes/selectors/current-theme'; | ||
|
||
/** | ||
* Passes the activating state of themes to the supplied child component. | ||
*/ | ||
var ActivatingThemeData = React.createClass( { | ||
const ActivatingThemeData = React.createClass( { | ||
|
||
propTypes: { | ||
children: React.PropTypes.element.isRequired | ||
}, | ||
|
||
getInitialState: function() { | ||
return getState( this.props ); | ||
}, | ||
|
||
componentWillMount: function() { | ||
CurrentThemeStore.on( 'change', this.onActivatingTheme ); | ||
}, | ||
|
||
componentWillUnmount: function() { | ||
CurrentThemeStore.off( 'change', this.onActivatingTheme ); | ||
}, | ||
|
||
onActivatingTheme: function() { | ||
this.setState( getState( this.props ) ); | ||
children: React.PropTypes.element.isRequired, | ||
// Connected props | ||
isActivating: React.PropTypes.bool.isRequired, | ||
hasActivated: React.PropTypes.bool.isRequired, | ||
currentTheme: React.PropTypes.shape( { | ||
name: React.PropTypes.string, | ||
id: React.PropTypes.string | ||
} ) | ||
}, | ||
|
||
render: function() { | ||
return React.cloneElement( this.props.children, this.state ); | ||
render() { | ||
return React.cloneElement( this.props.children, omit( this.props, 'children' ) ); | ||
} | ||
} ); | ||
|
||
module.exports = ActivatingThemeData; | ||
export default connect( | ||
( state, props ) => Object.assign( {}, | ||
props, | ||
{ | ||
isActivating: isActivating( state ), | ||
hasActivated: hasActivated( state ), | ||
currentTheme: getCurrentTheme( state, props.siteId ) | ||
} | ||
) | ||
)( ActivatingThemeData ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,64 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
var React = require( 'react' ); | ||
import React from 'react'; | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import omit from 'lodash/object/omit'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
var CurrentThemeStore = require( 'lib/themes/stores/current-theme' ), | ||
Actions = require( 'lib/themes/flux-actions' ); | ||
import { fetchCurrentTheme } from 'lib/themes/actions'; | ||
import { getCurrentTheme } from 'lib/themes/selectors/current-theme'; | ||
|
||
/** | ||
* Fetches the currently active theme of the supplied site | ||
* and passes it to the supplied child component. | ||
*/ | ||
var CurrentThemeData = React.createClass( { | ||
const CurrentThemeData = React.createClass( { | ||
|
||
propTypes: { | ||
children: React.PropTypes.element.isRequired, | ||
site: React.PropTypes.oneOfType( [ | ||
React.PropTypes.object, | ||
React.PropTypes.bool | ||
] ).isRequired, | ||
children: React.PropTypes.element.isRequired | ||
// Connected props | ||
currentTheme: React.PropTypes.shape( { | ||
name: React.PropTypes.string, | ||
id: React.PropTypes.string | ||
} ), | ||
fetchCurrentTheme: React.PropTypes.func.isRequired | ||
}, | ||
|
||
getInitialState: function() { | ||
return { | ||
currentTheme: CurrentThemeStore.getCurrentTheme( this.props.site.ID ) | ||
}; | ||
componentDidMount() { | ||
this.refresh( this.props ); | ||
}, | ||
|
||
componentWillMount: function() { | ||
CurrentThemeStore.on( 'change', this.onCurrentThemeChange ); | ||
|
||
if ( ! this.state.currentTheme && this.props.site ) { | ||
Actions.fetchCurrentTheme( this.props.site ); | ||
} | ||
}, | ||
|
||
componentWillReceiveProps: function( nextProps ) { | ||
if ( this.state.currentTheme ) { | ||
return; | ||
} | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( nextProps.site && nextProps.site !== this.props.site ) { | ||
Actions.fetchCurrentTheme( nextProps.site ); | ||
this.refresh( nextProps ); | ||
} | ||
}, | ||
|
||
componentWillUnmount: function() { | ||
CurrentThemeStore.off( 'change', this.onCurrentThemeChange ); | ||
}, | ||
|
||
onCurrentThemeChange: function() { | ||
this.setState( { | ||
currentTheme: CurrentThemeStore.getCurrentTheme( this.props.site.ID ) | ||
} ); | ||
refresh( props ) { | ||
if ( ! this.props.currentTheme && props.site ) { | ||
this.props.fetchCurrentTheme( props.site ); | ||
} | ||
}, | ||
|
||
render: function() { | ||
return React.cloneElement( this.props.children, this.state ); | ||
render() { | ||
return React.cloneElement( this.props.children, omit( this.props, 'children' ) ); | ||
} | ||
} ); | ||
|
||
module.exports = CurrentThemeData; | ||
export default connect( | ||
( state, props ) => Object.assign( {}, | ||
props, | ||
{ | ||
currentTheme: getCurrentTheme( state, props.site.ID ) | ||
} | ||
), | ||
bindActionCreators.bind( null, { fetchCurrentTheme } ) | ||
)( CurrentThemeData ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.