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

Theme Showcase: Update Controllers for an "All Sites" View #87000

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions client/my-sites/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,34 @@ export function selectSiteIfLoggedIn( context, next ) {
selectSite( context );
}

export function selectSiteIfLoggedInWithSites( context, next ) {
/**
* If the section has an "all sites" view to delay the site selection,
* only handle the site selection with 0 or 1 sites.
*/
export function selectSiteOrSkipIfLoggedInWithMultipleSites( context, next ) {
const state = context.store.getState();
if ( isUserLoggedIn( state ) && getCurrentUserSiteCount( state ) && ! context.params.site_id ) {
const isLoggedIn = isUserLoggedIn( state );
const siteCount = getCurrentUserSiteCount( state );
const siteFragment =
context.params.site || context.params.site_id || getSiteFragment( context.path );

// If the user is logged out, has 0 sites, or the path contains a site fragment,
// proceed with the regular site selection.
if ( ! isLoggedIn || ! siteCount || !! siteFragment ) {
siteSelection( context, next );
return;
}
Comment on lines +893 to +898
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this matches with the name of the function. From the name I was expecting this to select the site if only one site was available or to proceed with the chain, and right now the function is doing 2 different things or proceeding the chain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm not very happy with both naming and flow, but I couldn't find a better idea. 🤔

Do you have a suggestion for making this cleaner?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could extract this to a new command (maybe called selectSiteIfLoggedIn) that we will add to the chain after the selectSiteIfLoggedInWithOneSite command. That would make things cleaner.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, we could just rename this to something more generic like handleSiteSelection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've renamed it as selectSiteOrSkipIfLoggedInWithMultipleSites.
It's a mouthful, but it's also descriptive of what it does. 🙂


// If the user only has 1 site and the path doesn't contain a site fragment,
// select the site automatically and move on.
if ( siteCount === 1 ) {
selectSite( context );
return;
}

siteSelection( context, next );
// If the user has multiple sites and the path doesn't contain a site fragment,
// proceed with rendering the page, delaying the site selection.
next();
}

export function hideNavigationIfLoggedInWithNoSites( context, next ) {
Expand Down
4 changes: 2 additions & 2 deletions client/my-sites/theme/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getLanguageRouteParam } from '@automattic/i18n-utils';
import { translate } from 'i18n-calypso';
import { makeLayout, redirectWithoutLocaleParamIfLoggedIn } from 'calypso/controller';
import {
selectSiteIfLoggedInWithSites,
selectSiteOrSkipIfLoggedInWithMultipleSites,
redirectToLoginIfSiteRequested,
} from 'calypso/my-sites/controller';
import { getTheme } from 'calypso/state/themes/selectors';
Expand Down Expand Up @@ -31,7 +31,7 @@ export default function ( router ) {
redirectWithoutLocaleParamIfLoggedIn,
redirectToLoginIfSiteRequested,
setTitleIfThemeExisted,
selectSiteIfLoggedInWithSites,
selectSiteOrSkipIfLoggedInWithMultipleSites,
fetchThemeDetailsData,
details,
makeLayout
Expand Down
4 changes: 2 additions & 2 deletions client/my-sites/themes/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'calypso/controller';
import {
navigation,
selectSiteIfLoggedInWithSites,
selectSiteOrSkipIfLoggedInWithMultipleSites,
siteSelection,
sites,
hideNavigationIfLoggedInWithNoSites,
Expand Down Expand Up @@ -73,7 +73,7 @@ export default function ( router ) {
routesWithoutSites,
redirectWithoutLocaleParamIfLoggedIn,
fetchAndValidateVerticalsAndFilters,
selectSiteIfLoggedInWithSites,
selectSiteOrSkipIfLoggedInWithMultipleSites,
renderThemes,
hideNavigationIfLoggedInWithNoSites,
addNavigationIfLoggedIn,
Expand Down
Loading