-
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
State: Migrate lib/site-roles to Redux state #9249
Conversation
037c50a
to
793ebe4
Compare
@@ -118,7 +118,7 @@ UndocumentedSite.prototype.shortcodes = function( attributes, callback ) { | |||
}; | |||
|
|||
UndocumentedSite.prototype.getRoles = function( callback ) { | |||
this.wpcom.withLocale().req.get( '/sites/' + this._id + '/roles', {}, callback ); | |||
return this.wpcom.withLocale().req.get( '/sites/' + this._id + '/roles', {}, callback ); |
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.
If you rebase, this change will be omitted now (#9116), though merge will figure it out too in any case.
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.
👍 true - I rebased, and it did go away. Great job with #9116 btw.
|
||
const RoleSelect = ( props ) => { | ||
const { siteRoles, site, includeFollower, siteId, id, explanation, translate } = props; | ||
const roles = siteRoles && siteRoles.slice( 0 ); |
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.
Instead of Array#slice
to copy the array because we use Array#push
below, we could instead use Array#concat
:
let { siteRoles } = props;
if ( siteRoles && includeFollower ) {
siteRoles = siteRoles.concat( getWPCOMFollowerRole( props ) );
}
import { getSite } from 'state/sites/selectors'; | ||
import { getSiteRoles } from 'state/site-roles/selectors'; | ||
|
||
const getWPCOMFollowerRole = ( { site, translate } ) => { |
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.
Per naming conventions on abbreviations this should probably be getWpcomFollowerRole
return; | ||
return ( | ||
<FormFieldset key={ siteId } disabled={ ! roles }> | ||
{ siteId && |
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.
This won't work the way you think it does. The logic is such that it'll check the truthiness of each and stop at whichever is falsey or the final value, so it'd either render null
for an empty site ID or the <QuerySiteRoles />
component, but never the <QuerySites />
component.
Instead, you might want:
{ siteId && <QuerySites siteId={ siteId } /> }
{ siteId && <QuerySiteRoles siteId={ siteId } /> }
Or:
{ siteId && [
<QuerySites key="query-sites" siteId={ siteId } />,
<QuerySiteRoles key="query-site-roles" siteId={ siteId } />
] }
</FormSettingExplanation> | ||
<FormLabel htmlFor={ id }> | ||
{ translate( 'Role', { | ||
context: 'Text that is displayed in a label of a form.' |
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.
Acknowledging it was there previously, I find this to be a strange context
for the string. Why do we need context in this case? Role doesn't seem specific to a form.
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.
Makes sense - I don't think this would help the translators in any way, so removing that.
</FormLabel> | ||
<FormSelect { ...omit( props, omitProps ) }> | ||
{ | ||
roles && roles.map( ( role ) => { |
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.
The nice thing about Lodash's map
is that it would handle the empty roles
for you automatically.
I don't feel particularly strongly about changing this.
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.
Oh, I was left with the impression that the native Array.map
would be faster. Good call, thanks!
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.
With latest changes, you don't need the siteRoles &&
check anymore. map
will return an empty array if passed a falsey value.
Oh, I was left with the impression that the native
Array.map
would be faster.
Lodash "cheats" and sometimes forgoes full spec compliancy vs. native counterparts in favor of performance. In the case of array iteration ignoring sparse array checks.
See also:
dispatch( { | ||
type: SITE_ROLES_REQUEST_FAILURE, | ||
siteId, | ||
error: pick( error, [ 'error', 'status', 'message' ] ) |
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.
That's a lot of work to shape an object that we don't use 😄
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.
👍 Removing this, since we don't use the error
at all.
793ebe4
to
09c8fd5
Compare
Thanks for the review, @aduth, the PR is ready for a new one. |
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.
This works well in my testing. A few thoughts that may or may not make sense to address here vs. a subsequent pull request, but otherwise LGTM 👍
]; | ||
|
||
if ( site && siteRoles && includeFollower ) { | ||
siteRoles = siteRoles.concat( getWpcomFollowerRole( props ) ); |
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.
Do you think this is something that belongs in a state selector? In other words, should getSiteRoles
include the follower role automatically when the site is not private?
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.
Sure, that would make sense. I didn't want to overcomplicate this PR, so let's keep this for another one.
<FormLabel htmlFor={ id }> | ||
{ translate( 'Role' ) } | ||
</FormLabel> | ||
<FormSelect { ...omit( props, omitProps ) }> |
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.
If we could figure out based on usage what's expected for this component to pass through (e.g. onChange
), would be nice to avoid the omit
. I see in a few cases there's an id
and name
passed, though I'm doubtful those are really necessary.
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.
Good call. The list of omitProps
has grown. Let's keep this for another PR.
d331391
to
5e32301
Compare
This PR migrates from the legacy
lib/site-roles
Flux store to Redux - fixes #7921. The PR takes care of the following:QuerySiteRoles
query component.RoleSelect
component to use Redux and converts it to a stateless functional component.lib/site-roles
Flux store.To test:
Follower
.npm run test-client client/state/site-roles/