Skip to content

Commit

Permalink
Split selectors.js into individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham authored and ehg committed Dec 16, 2015
1 parent 85d2411 commit 77e4cc5
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 101 deletions.
6 changes: 5 additions & 1 deletion client/components/data/activating-theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import omit from 'lodash/object/omit';
/**
* Internal dependencies
*/
import { isActivating, hasActivated, getCurrentTheme } from 'lib/themes/selectors';
import {
isActivating,
hasActivated,
getCurrentTheme
} from 'lib/themes/selectors/current-theme';

/**
* Passes the activating state of themes to the supplied child component.
Expand Down
2 changes: 1 addition & 1 deletion client/components/data/current-theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import omit from 'lodash/object/omit';
* Internal dependencies
*/
import { fetchCurrentTheme } from 'lib/themes/actions';
import { getCurrentTheme } from 'lib/themes/selectors';
import { getCurrentTheme } from 'lib/themes/selectors/current-theme';

/**
* Fetches the currently active theme of the supplied site
Expand Down
36 changes: 29 additions & 7 deletions shared/components/data/themes-list-fetcher/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import React from 'react';
import omit from 'lodash/object/omit';
import once from 'lodash/function/once';
import filter from 'lodash/collection/filter';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

Expand All @@ -12,13 +13,9 @@ import { connect } from 'react-redux';
*/
import Constants from 'lib/themes/constants';
import { query, fetchNextPage } from 'lib/themes/actions';
import {
getFilteredThemes,
hasSiteChanged,
isJetpack,
isLastPage,
isFetchingNextPage
} from 'lib/themes/selectors';
import { hasSiteChanged, isJetpack } from 'lib/themes/selectors/themes-last-query';
import { isLastPage, isFetchingNextPage, getThemesList } from 'lib/themes/selectors/themes-list';
import { getThemeById } from 'lib/themes/selectors/themes';

const ThemesListFetcher = React.createClass( {
propTypes: {
Expand Down Expand Up @@ -118,6 +115,31 @@ const ThemesListFetcher = React.createClass( {

} );

function getFilteredThemes( state, search ) {
const allThemes = getThemesList( state )
.map( getThemeById.bind( null, state ) );

if ( ! isJetpack( state ) || ! search ) {
return allThemes;
}

return filter( allThemes, theme => matches( theme, search ) );
}

function matches( theme, rawSearch ) {
const search = rawSearch.toLowerCase().trim();

return [ 'name', 'tags', 'description', 'author' ].some( field => (
theme[ field ] && join( theme[ field ] )
.toLowerCase().replace( '-', ' ' )
.indexOf( search ) >= 0
) );
}

function join( value ) {
return Array.isArray( value ) ? value.join( ' ' ) : value;
}

export default connect(
( state, props ) => Object.assign( {},
props,
Expand Down
10 changes: 4 additions & 6 deletions shared/lib/themes/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ const debug = debugFactory( 'calypso:themes:actions' ); //eslint-disable-line no
*/
import ThemeConstants from 'lib/themes/constants';
import ThemeHelpers from './helpers';
import {
getThemeById,
getCurrentTheme,
getQueryParams,
isJetpack
} from './selectors';
import { getCurrentTheme } from 'lib/themes/selectors/current-theme';
import { isJetpack } from 'lib/themes/selectors/themes-last-query';
import { getQueryParams } from 'lib/themes/selectors/themes-list';
import { getThemeById } from 'lib/themes/selectors/themes';
import wpcom from 'lib/wp';

export function fetchThemes( site ) {
Expand Down
83 changes: 0 additions & 83 deletions shared/lib/themes/selectors.js

This file was deleted.

11 changes: 11 additions & 0 deletions shared/lib/themes/selectors/current-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function getCurrentTheme( state, siteId ) {
return state.themes.currentTheme.get( 'currentThemes' ).get( siteId );
}

export function isActivating( state ) {
return state.themes.currentTheme.get( 'isActivating' );
}

export function hasActivated( state ) {
return state.themes.currentTheme.get( 'hasActivated' );
}
16 changes: 16 additions & 0 deletions shared/lib/themes/selectors/themes-last-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function isJetpack( state ) {
return !! state.themes.themesLastQuery.get( 'isJetpack' );
}

export function getParams( state ) {
return state.themes.themesLastQuery.get( 'lastParams' ) || {};
}

export function hasSiteChanged( state ) {
return state.themes.themesLastQuery.get( 'previousSiteId' ) !==
state.themes.themesLastQuery.get( 'currentSiteId' );
};

export function hasParams( state ) {
return !! state.themes.themesLastQuery.get( 'lastParams' );
}
15 changes: 15 additions & 0 deletions shared/lib/themes/selectors/themes-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function isFetchingNextPage( state ) {
return state.themes.themesList.getIn( [ 'queryState', 'isFetchingNextPage' ] );
}

export function isLastPage( state ) {
return state.themes.themesList.getIn( [ 'queryState', 'isLastPage' ] );
}

export function getThemesList( state ) {
return state.themes.themesList.get( 'list' );
}

export function getQueryParams( state ) {
return state.themes.themesList.get( 'query' ).toObject();
}
8 changes: 8 additions & 0 deletions shared/lib/themes/selectors/themes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getThemes( state ) {
return state.themes.themes.get( 'themes' ).toJS();
}

export function getThemeById( state, id ) {
const theme = state.themes.themes.getIn( [ 'themes', id ] );
return theme ? theme.toJS() : undefined;
}
6 changes: 3 additions & 3 deletions shared/my-sites/themes/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var Main = require( 'components/main' ),
ThemesSelection = require( './themes-selection' ),
ThemeHelpers = require( 'lib/themes/helpers' ),
getButtonOptions = require( './theme-options' ),
ThemeSelectors = require( 'lib/themes/selectors' );
ThemesListSelectors = require( 'lib/themes/selectors/themes-list' );

var Themes = React.createClass( {
mixins: [ observe( 'sites' ) ],
Expand Down Expand Up @@ -172,8 +172,8 @@ export default connect(
( state, props ) => Object.assign( {},
props,
{
queryParams: ThemeSelectors.getQueryParams( state ),
themesList: ThemeSelectors.getThemesList( state )
queryParams: ThemesListSelectors.getQueryParams( state ),
themesList: ThemesListSelectors.getThemesList( state )
}
)
)( Themes );

0 comments on commit 77e4cc5

Please sign in to comment.