Skip to content
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

Add themeRequestError reducer and getThemeRequestError selector and tests. #9791

Merged
merged 2 commits into from
Dec 4, 2016

Conversation

budzanowski
Copy link
Contributor

Info

The new themes Redux sub-tree requires error handling. This PR adds reducer and selector that that respond to theme request action success or error.

Testing

Not connected yet. Will be integrated in #9750,

@budzanowski budzanowski added [Feature Group] Appearance & Themes Features related to the appearance of sites. [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Dec 2, 2016
@budzanowski budzanowski added this to the Themes: JetPress Rally milestone Dec 2, 2016
@budzanowski budzanowski self-assigned this Dec 2, 2016
@matticbot
Copy link
Contributor

Copy link
Contributor

@seear seear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

For our use case we could store a boolean instead of the actual error object, since I don't think we ever make use of it. But I don't see any harm in storing the error.

} ),
[ THEME_REQUEST_SUCCESS ]: ( state, { siteId, themeId } ) => ( {
...state,
[ siteId ]: Object.assign( {}, state[ siteId ], { [ themeId ]: null } )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:
[ siteId ]: omit( state[ siteId ], [ themeId ] ),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the brackets around themeId, I think, tho...

* @return {Object} error object if present or null otherwise
*/
export function getThemeRequestsError( state, themeId, siteId ) {
const themes = get( state.themes.themeRequestsError, siteId, null );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name is misleading here since we're getting an error, not a themes array.

I think the whole selector could be shortened to

return get( state.themes.themeRequestsError, [ siteId, themeId ], null );

thanks to the way get works. (You could consider also dropping the fallback null -- it will then just return undefined, which should be expressive enough too, I think.)

export const themeRequestsError = createReducer( {}, {
[ THEME_REQUEST_FAILURE ]: ( state, { siteId, themeId, error } ) => ( {
...state,
[ siteId ]: Object.assign( {}, state[ siteId ], { [ themeId ]: { error } } )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will produce a themeRequestsError state like this (stolen from your unit tests 😛 ):

2916284: {
	vivaro: {
		error: 'Request error'
	}
 }

Let's drop the error layer since it bears no extra information:

2916284: {
	vivaro: 'Request error'
 }

i.e. just omit the curly braces around error so it doesn't produce an object:

[ siteId ]: Object.assign( {}, state[ siteId ], { [ themeId ]: error } )

which I think you can then write using destructuring as

[ siteId ]: {
	...state[ siteId ],
	[ themeId ]: error
}

* @param {Object} action Action payload
* @return {Object} Updated state
*/
export const themeRequestsError = createReducer( {}, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming nitpick: themeRequestErrors (since our other reducers are plural, too; the Request middle part should be singular, tho, I think 😉 ).

@ockham
Copy link
Contributor

ockham commented Dec 2, 2016

Left a couple of notes. Good to merge after you've addressed them. Love your unit tests, those are really helpful when reviewing and reasoning about state shape.

type: THEME_REQUEST_SUCCESS,
siteId: 2916284,
themeId: 841
themeId: 'twentysixteen'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good spot.

@ockham ockham added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Dec 2, 2016
@budzanowski budzanowski merged commit 7a361ad into master Dec 4, 2016
@budzanowski budzanowski deleted the add/themeRequestsError-reducer-and-tests branch December 4, 2016 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature Group] Appearance & Themes Features related to the appearance of sites.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants