Skip to content

Commit

Permalink
Reducer for user specific data (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
notnownikki authored Jul 19, 2017
1 parent 43d4a2f commit 659c64f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 40 deletions.
4 changes: 4 additions & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ function preparePostState( store, post ) {
export function createEditorInstance( id, post ) {
const store = createReduxStore();

store.dispatch( {
type: 'LOAD_USER_DATA',
} );

preparePostState( store, post );

render(
Expand Down
2 changes: 1 addition & 1 deletion editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,5 +676,5 @@ export function getNotices( state ) {
*/
export function getRecentlyUsedBlocks( state ) {
// resolves the block names in the state to the block type settings
return state.editor.recentlyUsedBlocks.map( blockType => getBlockType( blockType ) );
return state.userData.recentlyUsedBlocks.map( blockType => getBlockType( blockType ) );
}
18 changes: 15 additions & 3 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,24 @@ export const editor = combineUndoableReducers( {

return state;
},
}, { resetTypes: [ 'RESET_BLOCKS' ] } );

/**
* Reducer loading and saving user specific data, such as preferences and
* block usage.
*
* @param {Object} state Current state
* @param {Object} action Dispatched action
* @return {Object} Updated state
*/
export const userData = combineReducers( {
recentlyUsedBlocks( state = [], action ) {
const maxRecent = 8;
switch ( action.type ) {
case 'SETUP_NEW_POST':
case 'LOAD_USER_DATA':
// This is where we initially populate the recently used blocks,
// for now this inserts blocks from the common category.
// for now this inserts blocks from the common category, but will
// load this from an API in the future.
return getBlockTypes()
.filter( ( blockType ) => 'common' === blockType.category )
.slice( 0, maxRecent )
Expand All @@ -247,7 +258,7 @@ export const editor = combineUndoableReducers( {
}
return state;
},
}, { resetTypes: [ 'RESET_BLOCKS' ] } );
} );

/**
* Reducer returning the last-known state of the current post, in the format
Expand Down Expand Up @@ -528,6 +539,7 @@ export function createReduxStore() {
panel,
saving,
notices,
userData,
} ) );

const enhancers = [ applyMiddleware( refx( effects ) ) ];
Expand Down
83 changes: 47 additions & 36 deletions editor/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
notices,
showInsertionPoint,
createReduxStore,
userData,
} from '../state';

describe( 'state', () => {
Expand Down Expand Up @@ -83,42 +84,6 @@ describe( 'state', () => {
expect( state.blockOrder ).toEqual( [ 'chicken', 'ribs' ] );
} );

it( 'should record recently used blocks', () => {
const original = editor( undefined, {} );
const state = editor( original, {
type: 'INSERT_BLOCKS',
blocks: [ {
uid: 'bacon',
name: 'core-embed/twitter',
} ],
} );

expect( state.recentlyUsedBlocks[ 0 ] ).toEqual( 'core-embed/twitter' );

const twoRecentBlocks = editor( state, {
type: 'INSERT_BLOCKS',
blocks: [ {
uid: 'eggs',
name: 'core-embed/youtube',
} ],
} );

expect( twoRecentBlocks.recentlyUsedBlocks[ 0 ] ).toEqual( 'core-embed/youtube' );
expect( twoRecentBlocks.recentlyUsedBlocks[ 1 ] ).toEqual( 'core-embed/twitter' );
} );

it( 'should populate recently used blocks with the common category', () => {
const initial = editor( undefined, {
type: 'SETUP_NEW_POST',
edits: {
status: 'draft',
title: 'post title',
},
} );

expect( initial.recentlyUsedBlocks ).toEqual( expect.arrayContaining( [ 'core/test-block', 'core/text' ] ) );
} );

it( 'should replace the block', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
Expand Down Expand Up @@ -1052,4 +1017,50 @@ describe( 'state', () => {
] ) );
} );
} );

describe( 'userData()', () => {
beforeAll( () => {
registerBlockType( 'core/test-block', {
save: noop,
edit: noop,
category: 'common',
} );
} );

afterAll( () => {
unregisterBlockType( 'core/test-block' );
} );

it( 'should record recently used blocks', () => {
const original = userData( undefined, {} );
const state = userData( original, {
type: 'INSERT_BLOCKS',
blocks: [ {
uid: 'bacon',
name: 'core-embed/twitter',
} ],
} );

expect( state.recentlyUsedBlocks[ 0 ] ).toEqual( 'core-embed/twitter' );

const twoRecentBlocks = userData( state, {
type: 'INSERT_BLOCKS',
blocks: [ {
uid: 'eggs',
name: 'core-embed/youtube',
} ],
} );

expect( twoRecentBlocks.recentlyUsedBlocks[ 0 ] ).toEqual( 'core-embed/youtube' );
expect( twoRecentBlocks.recentlyUsedBlocks[ 1 ] ).toEqual( 'core-embed/twitter' );
} );

it( 'should populate recently used blocks with the common category', () => {
const initial = userData( undefined, {
type: 'LOAD_USER_DATA',
} );

expect( initial.recentlyUsedBlocks ).toEqual( expect.arrayContaining( [ 'core/test-block', 'core/text' ] ) );
} );
} );
} );

0 comments on commit 659c64f

Please sign in to comment.