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

Site Picker: Show selected site first #1507

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions client/components/site-selector/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = React.createClass( {
} else {
sites = this.props.sites.getVisible();
}
sites = this.props.sites.withSelectedFirst( sites );

// Render sites
siteElements = sites.map( function( site ) {
Expand Down
24 changes: 19 additions & 5 deletions client/lib/sites-list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,7 @@ SitesList.prototype.getSite = function( siteID ) {
}

return this.get().filter( function( site ) {
// We need to check `slug` before `domain` to grab the correct site on certain
// clashes between a domain redirect and a Jetpack site, as well as domains
// on subfolders, but we also need to look for the `domain` as a last resort
// to cover mapped domains for regular WP.com sites.
return site.ID === siteID || site.slug === siteID || site.domain === siteID || site.wpcom_url === siteID;
return siteMatchesSiteId( site, siteID );
}, this ).shift();
};

Expand Down Expand Up @@ -577,4 +573,22 @@ SitesList.prototype.onUpdatedPlugin = function( site ) {
}
};

SitesList.prototype.withSelectedFirst = function( sites ) {
if ( ! this.selected ) {
// we return a copy, so that we are consistent with returning a copy in the other case
return sites.slice();
}

return sites.filter( site => siteMatchesSiteId( site, this.selected ) )
.concat( sites.filter( site => ! siteMatchesSiteId( site, this.selected ) ) );
}

function siteMatchesSiteId( site, siteID ) {
// We need to check `slug` before `domain` to grab the correct site on certain
// clashes between a domain redirect and a Jetpack site, as well as domains
// on subfolders, but we also need to look for the `domain` as a last resort
// to cover mapped domains for regular WP.com sites.
return site.ID === siteID || site.slug === siteID || site.domain === siteID || site.wpcom_url === siteID;
Copy link
Contributor

Choose a reason for hiding this comment

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

I know you didn't write this, but... 😢

}

module.exports = SitesList;
36 changes: 36 additions & 0 deletions client/lib/sites-list/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,40 @@ describe( 'SitesList', function() {
} );
} );

describe( 'withSelectedFirst', function() {
it( 'should return empty array when given empty array', function() {
var sitesList = SitesList();
assert.deepEqual( [], sitesList.withSelectedFirst( [] ) );
} );
it( 'should return an equal array if there is no selected element', function() {
var sitesList = SitesList();
sitesList.selected = null;
assert.deepEqual( original, sitesList.withSelectedFirst( original ) );
} );
it( 'should keep the first element if it is selected', function() {
var sitesList = SitesList();
sitesList.initialize( original.slice() );
sitesList.selected = original[0].ID;
assert.deepEqual( original, sitesList.withSelectedFirst( original ) );
} );
it( 'should move the selected site to the front', function() {
var sitesList = SitesList(),
sites = [ {ID: 1}, {ID: 2}, {ID: 3} ];
sitesList.initialize( sites.slice() );
sitesList.selected = 2;
assert.deepEqual( [ {ID: 2}, {ID: 1}, {ID: 3}], sitesList.withSelectedFirst( sites ) );
} );
it( 'should not mutate original array', function() {
var sitesList = SitesList(),
sites = [ {ID: 1}, {ID: 2}, {ID: 3} ],
originalSites = sites.slice(),
after;
sitesList.initialize( sites.slice() );
sitesList.selected = 2;
sitesList.withSelectedFirst( sites );
after = assert.deepEqual( originalSites, sites );
assert( after !== sites );
} );
} );

} );