-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Themes: Switch data fetching to Redux #840
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6b3e56a
README: Remove theme-last-event section
ockham 2389cbf
Themes: Switch data fetching to Redux
ockham e6a62c7
Data components: Inline getState()
ockham cbc8cd6
connect() main.jsx
ockham 8249dd0
Upgrades: Add note on Redux
ockham f7e2ff5
connect() customize/main.jsx
ockham 053dcc3
customize/actions: Add comment on future Reduxification
ockham 3ccd475
isJetpack(): Cast to bool (using git add shared/lib/themes/selectors.…
ockham ddaeee1
ThemesListFetcher: Add lastQuery propType
ockham 85d2411
Data components: Add comments on propTypes provided by connect()
ockham 77e4cc5
Split selectors.js into individual files
ockham a84cc81
Shorten propTypes comments
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to assign into the component's own props as you're doing here. Instead, this could be simplified to:
The connected props will be automatically merged with any other props passed to the component explicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing that up! That goes on my things-to-refactor in our Redux subtree list... I need to file an issue for that TODO... :-)