-
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: Add theme-details Redux subtree and data fetcher component #3011
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35effa4
wpcom-undocumented: Fix two debug strings
ockham 3fe5d98
current-theme/test: Move from external to internal deps
ockham 86aacf7
Themes: Add theme-details Redux subtree
ockham 0f1cbec
client/components/theme-details: Add
ockham 7cb9958
Data components: omit redundant bindActionCreators()
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ThemeDetailsData | ||
================ | ||
|
||
A component to decouple the fetching of a theme's details from any rendering. | ||
|
||
## Usage | ||
|
||
A child component wrapped with `<ThemeDetailsData />` will be passed the prop | ||
`themeDetails`, a theme object. |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import omit from 'lodash/object/omit'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { fetchThemeDetails } from 'state/themes/actions'; | ||
import { getThemeDetails } from 'state/themes/theme-details/selectors'; | ||
|
||
/** | ||
* Fetches details for a theme specified by its ID | ||
* and passes it to the supplied child component. | ||
*/ | ||
const ThemeDetailsData = React.createClass( { | ||
|
||
propTypes: { | ||
children: React.PropTypes.element.isRequired, | ||
id: React.PropTypes.string.isRequired, | ||
// Connected props | ||
name: React.PropTypes.string, | ||
author: React.PropTypes.string, | ||
fetchThemeDetails: React.PropTypes.func.isRequired | ||
}, | ||
|
||
componentDidMount() { | ||
this.refresh( this.props ); | ||
}, | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( nextProps.id && nextProps.id !== this.props.id ) { | ||
this.refresh( nextProps ); | ||
} | ||
}, | ||
|
||
refresh( props ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could maybe take |
||
if ( ! this.props.name && props.id ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the |
||
this.props.fetchThemeDetails( props.id ); | ||
} | ||
}, | ||
|
||
render() { | ||
return React.cloneElement( this.props.children, omit( this.props, 'children' ) ); | ||
} | ||
} ); | ||
|
||
export default connect( | ||
( state, props ) => Object.assign( {}, | ||
props, | ||
getThemeDetails( state, props.id ) | ||
), | ||
{ fetchThemeDetails } | ||
)( ThemeDetailsData ); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
UI ?= bdd | ||
REPORTER ?= spec | ||
COMPILERS ?= js:babel/register | ||
NODE_BIN := $(shell npm bin) | ||
MOCHA ?= $(NODE_BIN)/mocha | ||
BASE_DIR := $(NODE_BIN)/../.. | ||
NODE_PATH := test:$(BASE_DIR)/client | ||
|
||
# In order to simply stub modules, add test to the NODE_PATH | ||
test: | ||
@NODE_ENV=test NODE_PATH=$(NODE_PATH) $(MOCHA) --compilers $(COMPILERS) --reporter $(REPORTER) --ui $(UI) | ||
|
||
.PHONY: test |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Map, fromJS } from 'immutable'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ActionTypes from '../action-types'; | ||
import { DESERIALIZE, SERIALIZE } from '../../action-types'; | ||
|
||
export default ( state = Map(), action ) => { | ||
switch ( action.type ) { | ||
case ActionTypes.RECEIVE_THEME_DETAILS: | ||
return state | ||
.set( action.themeId, Map( { | ||
name: action.themeName, | ||
author: action.themeAuthor | ||
} ) ) | ||
case DESERIALIZE: | ||
return fromJS( state ); | ||
case SERIALIZE: | ||
return state.toJS(); | ||
} | ||
return state; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getThemeDetails( state, id ) { | ||
return state.themes.themeDetails.get( id ).toJS(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
import { Map, fromJS } from 'immutable'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { RECEIVE_THEME_DETAILS } from '../../action-types'; | ||
import { DESERIALIZE, SERIALIZE } from '../../../action-types'; | ||
import reducer from '../reducer'; | ||
|
||
describe( 'reducer', () => { | ||
it( 'should default to an empty Immutable Map', () => { | ||
const state = reducer( undefined, {} ); | ||
|
||
expect( state.toJS() ).to.be.empty; | ||
} ); | ||
|
||
it( 'should set theme details for the given ID', () => { | ||
const state = reducer( undefined, { | ||
type: RECEIVE_THEME_DETAILS, | ||
themeId: 'mood', | ||
themeName: 'Mood', | ||
themeAuthor: 'Automattic' | ||
} ); | ||
|
||
expect( state.get( 'mood' ).toJS() ).to.eql( { | ||
name: 'Mood', | ||
author: 'Automattic' | ||
} ); | ||
} ); | ||
|
||
describe( 'persistence', () => { | ||
const initialState = Map(); | ||
|
||
it( 'persists state and converts to a plain JS object', () => { | ||
const jsObject = Object.freeze( { | ||
mood: { | ||
name: 'Mood', | ||
author: 'Automattic' | ||
} | ||
} ); | ||
const state = fromJS( jsObject ); | ||
const persistedState = reducer( state, { type: SERIALIZE } ); | ||
expect( persistedState ).to.eql( jsObject ); | ||
} ); | ||
it( 'loads valid persisted state and converts to immutable.js object', () => { | ||
const jsObject = Object.freeze( { | ||
mood: { | ||
name: 'Mood', | ||
author: 'Automattic' | ||
} | ||
} ); | ||
const state = reducer( jsObject, { type: DESERIALIZE } ); | ||
expect( state ).to.eql( fromJS( jsObject ) ); | ||
} ); | ||
|
||
it.skip( 'should ignore loading data with invalid keys ', () => { | ||
const jsObject = Object.freeze( { | ||
missingKey: true, | ||
mood: { | ||
name: 'Mood', | ||
author: 'Automattic' | ||
} | ||
} ); | ||
const state = reducer( jsObject, { type: DESERIALIZE } ); | ||
expect( state ).to.eql( initialState ); | ||
} ); | ||
|
||
it.skip( 'should ignore loading data with invalid values ', () => { | ||
const jsObject = Object.freeze( { | ||
mood: 'foo', | ||
} ); | ||
const state = reducer( jsObject, { type: DESERIALIZE } ); | ||
expect( state ).to.eql( initialState ); | ||
} ); | ||
} ); | ||
} ); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
import { Map } from 'immutable'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getThemeDetails } from '../selectors'; | ||
|
||
describe( 'selectors', () => { | ||
describe( '#getThemeDetails()', () => { | ||
it( 'should return details for a theme given its ID', () => { | ||
const details = getThemeDetails( { | ||
themes: { | ||
themeDetails: Map( { | ||
mood: Map( { | ||
name: 'Mood', | ||
author: 'Automattic' | ||
} ) | ||
} ) | ||
} | ||
}, 'mood' ); | ||
|
||
expect( details ).to.eql( { name: 'Mood', author: 'Automattic' } ); | ||
} ); | ||
} ); | ||
} ); |
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.
Why remove the binding? Doesn't this break fetching in
refresh
?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.
https://github.com/rackt/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options:
So
bindActionCreators
is apparently redundant here and can be safely dropped, sinceconnect()
does the binding for us.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.
Aha! I didn't remember that at all. :)