Skip to content

Commit

Permalink
Themes: Removes all the remaining traces of Flux stores
Browse files Browse the repository at this point in the history
* `reducers/themes-last-query`: Remove Flux dispatcher `waitFor`
* `reducers/themes-list`: Use `isJetpack()` from selectors instead of
  `ThemeLastQueryStore`'s
* Kill `shared/lib/themes/stores`
* Remove obsolete helper functions from reducers, now that they are in `selectors/`
* Default-export reducers, don't export initialStates anymore
* Update README.md
* Rewrite tests in `shared/lib/themes` to directly test reducers instead of stores
  Note that in test/themes-list-store, the Jetpack test is removed since filtering
	is no longer done in the reducer but in the themes-list-fetcher data component
  (using the getFilteredThemes()) now.
  • Loading branch information
ockham committed Dec 16, 2015
1 parent 79f41f4 commit 51b1aef
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 374 deletions.
6 changes: 2 additions & 4 deletions shared/lib/themes/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Theme Data
==========

Contains stores and action creators for themes, and the theme showcase.
Contains reducers and action creators for themes, and the theme showcase.

## Stores

We're transitioning to a more `redux`-like architecture, so our Flux `./stores` are created from `./reducers`, using our own `createReducerStore()`.
## Reducers

### current-theme

Expand Down
8 changes: 2 additions & 6 deletions shared/lib/themes/reducers/current-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { fromJS } from 'immutable';
*/
import ThemeConstants from 'lib/themes/constants';

export const initialState = fromJS( {
const initialState = fromJS( {
isActivating: false,
hasActivated: false,
currentThemes: {}
} );

export const reducer = ( state = initialState, action ) => {
export default ( state = initialState, action ) => {
switch ( action.type ) {
case ThemeConstants.RECEIVE_CURRENT_THEME:
return state.setIn( [ 'currentThemes', action.site.ID ], {
Expand All @@ -33,7 +33,3 @@ export const reducer = ( state = initialState, action ) => {
}
return state;
};

export function getCurrentTheme( state, siteId ) {
return state.get( 'currentThemes' ).get( siteId );
}
8 changes: 4 additions & 4 deletions shared/lib/themes/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { combineReducers } from 'redux';
/**
* Internal dependencies
*/
import { reducer as themes } from './themes';
import { reducer as themesList } from './themes-list';
import { reducer as themesLastQuery } from './themes-last-query';
import { reducer as currentTheme } from './current-theme';
import themes from './themes';
import themesList from './themes-list';
import themesLastQuery from './themes-last-query';
import currentTheme from './current-theme';

export default combineReducers( {
themes,
Expand Down
12 changes: 2 additions & 10 deletions shared/lib/themes/reducers/themes-last-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { fromJS } from 'immutable';
*/
import ThemeConstants from '../constants';

export const initialState = fromJS( {
const initialState = fromJS( {
previousSiteId: 0,
currentSiteId: null,
isJetpack: null,
lastParams: null,
} );

export const reducer = ( state = initialState, action ) => {
export default ( state = initialState, action ) => {
switch ( action.type ) {
case ThemeConstants.QUERY_THEMES:
return state.set( 'lastParams', action.params );
Expand All @@ -29,11 +29,3 @@ export const reducer = ( state = initialState, action ) => {

return state;
};

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

export function hasParams( state ) {
return !! state.get( 'lastParams' );
}
4 changes: 2 additions & 2 deletions shared/lib/themes/reducers/themes-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const defaultQueryState = fromJS( {
isFetchingNextPage: false
} );

export const initialState = query( fromJS( {
const initialState = query( fromJS( {
list: [],
nextId: 0,
query: {},
Expand Down Expand Up @@ -54,7 +54,7 @@ function isActionForLastPage( list, action ) {
action.themes.length === 0;
}

export const reducer = ( state = initialState, action ) => {
export default ( state = initialState, action ) => {
switch ( action.type ) {
case ThemeConstants.QUERY_THEMES:
return query( state, action.params );
Expand Down
9 changes: 2 additions & 7 deletions shared/lib/themes/reducers/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import transform from 'lodash/object/transform';
*/
import ThemeConstants from 'lib/themes/constants';

export const initialState = fromJS( {
const initialState = fromJS( {
themes: {},
currentSiteId: 0
} );
Expand All @@ -26,7 +26,7 @@ function setActiveTheme( themeId, themes ) {
.setIn( [ themeId, 'active' ], true );
}

export const reducer = ( state = initialState, action ) => {
export default( state = initialState, action ) => {
switch ( action.type ) {
case ThemeConstants.RECEIVE_THEMES:
const isNewSite = action.isJetpack && ( action.siteId !== state.get( 'currentSiteId' ) );
Expand All @@ -40,8 +40,3 @@ export const reducer = ( state = initialState, action ) => {
}
return state;
};

export function getThemeById( state, id ) {
const theme = state.getIn( [ 'themes', id ] );
return theme ? theme.toJS() : undefined;
}
14 changes: 0 additions & 14 deletions shared/lib/themes/stores/current-theme.js

This file was deleted.

15 changes: 0 additions & 15 deletions shared/lib/themes/stores/themes-last-query.js

This file was deleted.

15 changes: 0 additions & 15 deletions shared/lib/themes/stores/themes-list.js

This file was deleted.

12 changes: 0 additions & 12 deletions shared/lib/themes/stores/themes.js

This file was deleted.

38 changes: 0 additions & 38 deletions shared/lib/themes/test/current-theme-store.js

This file was deleted.

40 changes: 40 additions & 0 deletions shared/lib/themes/test/current-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'chai';

import { createStore } from 'redux';

import ThemeConstants from '../constants';
import reducer from '../reducers/current-theme';

describe( 'current-theme', () => {
const SITE = { ID: 77203074 }; // dummy id

const actionReceiveCurrentTheme = {
type: ThemeConstants.RECEIVE_CURRENT_THEME,
themeId: 'twentyfifteen',
themeName: 'Twenty Fifteen',
site: SITE
};

let store;

function getCurrentTheme( siteId ) {
return store.getState().get( 'currentThemes' ).get( siteId );
}

beforeEach( () => {
store = createStore( reducer );
} );

describe( 'get()', () => {
beforeEach( () => {
store.dispatch( actionReceiveCurrentTheme );
} );

it( 'returns the current theme for the supplied site id', () => {
const currentTheme = getCurrentTheme( SITE.ID );

expect( currentTheme.id ).to.equal( 'twentyfifteen' );
expect( currentTheme.name ).to.equal( 'Twenty Fifteen' );
} );
} );
} );
Loading

0 comments on commit 51b1aef

Please sign in to comment.