-
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
[Remove SitesList] Add isSiteCustomizable selector #13160
Changes from all commits
b383007
d86591f
19debab
accd0a8
dc4e889
4933c75
2f66f23
aa56e2a
4489fdf
158c8f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { canCurrentUser } from 'state/selectors'; | ||
import { getCurrentUserId } from 'state/current-user/selectors'; | ||
import { getRawSite } from 'state/sites/selectors'; | ||
|
||
/** | ||
* Returns true if the site can be customized by the user, false if the | ||
* site cannot be customized, or null if customizing ability cannot be | ||
* determined. | ||
* | ||
* @param {Object} state Global state tree | ||
* @param {Number} siteId Site ID | ||
* @return {?Boolean} Whether site is customizable | ||
*/ | ||
export default function isSiteCustomizable( state, siteId ) { | ||
// Cannot determine site customizing ability if there is no current user | ||
if ( ! getCurrentUserId( state ) || ! getRawSite( state, siteId ) ) { | ||
return null; | ||
} | ||
|
||
return canCurrentUser( state, siteId, 'edit_theme_options' ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isSiteCustomizable } from '../'; | ||
|
||
describe( 'isSiteCustomizable()', () => { | ||
it( 'should return null if the capability is not set for the current user', () => { | ||
const isCustomizable = isSiteCustomizable( { | ||
sites: { | ||
items: { | ||
77203199: { | ||
ID: 77203199, | ||
URL: 'https://example.com' | ||
} | ||
} | ||
}, | ||
currentUser: { | ||
id: 12345678, | ||
capabilities: { | ||
77203199: {} | ||
} | ||
} | ||
}, 77203199 ); | ||
|
||
expect( isCustomizable ).to.be.null; | ||
} ); | ||
|
||
it( 'should return true is the corresponding user capability is true for this site', () => { | ||
const isCustomizable = isSiteCustomizable( { | ||
sites: { | ||
items: { | ||
77203199: { | ||
ID: 77203199, | ||
URL: 'http://example.com', | ||
options: { | ||
unmapped_url: 'http://example.com' | ||
} | ||
} | ||
} | ||
}, | ||
currentUser: { | ||
id: 12345678, | ||
capabilities: { | ||
77203199: { | ||
edit_theme_options: true | ||
} | ||
} | ||
} | ||
}, 77203199 ); | ||
|
||
expect( isCustomizable ).to.be.true; | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,6 @@ import { isHttps, withoutHttp, addQueryArgs, urlToSlug } from 'lib/url'; | |
import createSelector from 'lib/create-selector'; | ||
import { fromApi as seoTitleFromApi } from 'components/seo/meta-title-editor/mappings'; | ||
import versionCompare from 'lib/version-compare'; | ||
import getComputedAttributes from 'lib/site/computed-attributes'; | ||
import { getCustomizerFocus } from 'my-sites/customize/panels'; | ||
|
||
/** | ||
|
@@ -63,18 +62,40 @@ export const getSite = createSelector( | |
|
||
return { | ||
...site, | ||
...getComputedAttributes( site ), | ||
...getJetpackComputedAttributes( state, siteId ), | ||
hasConflict: isSiteConflicting( state, siteId ), | ||
title: getSiteTitle( state, siteId ), | ||
slug: getSiteSlug( state, siteId ), | ||
domain: getSiteDomain( state, siteId ), | ||
is_previewable: isSitePreviewable( state, siteId ) | ||
is_previewable: isSitePreviewable( state, siteId ), | ||
options: computeSiteOptions( state, siteId ), | ||
}; | ||
}, | ||
( state ) => state.sites.items | ||
); | ||
|
||
export function computeSiteOptions( state, siteId ) { | ||
const site = getRawSite( state, siteId ); | ||
if ( ! site ) { | ||
return null; | ||
} | ||
|
||
const isWpcomMappedDomain = getSiteOption( state, siteId, 'is_mapped_domain' ) && ! isJetpackSite( state, siteId ); | ||
const wpcomUrl = withoutHttp( getSiteOption( state, siteId, 'unmapped_url' ) ); | ||
|
||
// The 'standard' post format is saved as an option of '0' | ||
let defaultPostFormat = getSiteOption( state, siteId, 'default_post_format' ); | ||
if ( ! defaultPostFormat || defaultPostFormat === '0' ) { | ||
defaultPostFormat = 'standard'; | ||
} | ||
|
||
return { | ||
...site.options, | ||
...isWpcomMappedDomain && { wpcom_url: wpcomUrl }, | ||
default_post_format: defaultPostFormat | ||
}; | ||
} | ||
|
||
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. I see that the other new selector is in the 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. Not sure about that. 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. well we have been adding all new selectors to the selector directory. I should probably rephrase the question: is there a good reason why we aren't adding it to the selector directory instead? 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. First, I don't feel strongly on this so if you really think this is a blocker I can simply move it to My logic is that all the selectors used to assemble the site in the In the case of 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. let's see if @aduth has an opinion on this. 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. Having authored the yet-unmerged #10234, I tend to sway more on the extreme of forbidding new selectors outside 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. Yes, let's put everything new on |
||
export function getJetpackComputedAttributes( state, siteId ) { | ||
if ( ! isJetpackSite( state, siteId ) ) { | ||
return {}; | ||
|
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.
tests like this are good reasons for normalizing data form the API on entry into Calypso. instead of scattering around the checks and converting everywhere the data is used it would be helpful if it never came into the system in the odd formats. that's a side-comment 😉