Skip to content

Commit

Permalink
Remove usage stats for reusable blocks that are removed
Browse files Browse the repository at this point in the history
When a reusable block is deleted, remove that block from recentInserts
and frequentInserts.
  • Loading branch information
noisysocks committed Jan 7, 2018
1 parent 1c2db95 commit fb2669f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
first,
last,
omit,
omitBy,
without,
mapValues,
findIndex,
Expand Down Expand Up @@ -541,6 +542,16 @@ export function preferences( state = PREFERENCES_DEFAULTS, action ) {
},
};
}, state );
case 'REMOVE_REUSABLE_BLOCK':
const { id } = action;
return {
...state,
recentInserts: reject( state.recentInserts, ( { ref } ) => ref === id ),
insertFrequency: omitBy( state.insertFrequency, ( frequency, key ) => {
const { ref } = JSON.parse( key );
return ref === id;
} ),
};
case 'TOGGLE_FEATURE':
return {
...state,
Expand Down
31 changes: 31 additions & 0 deletions editor/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,37 @@ describe( 'state', () => {
} );
} );

it( 'should remove usage stats for reusable blocks that are removed', () => {
const initialState = {
recentInserts: [
{ name: 'core/paragraph' },
{ name: 'core/block', ref: 123 },
{ name: 'core/block', ref: 456 },
],
insertFrequency: {
[ JSON.stringify( { name: 'core/paragraph' } ) ]: 4,
[ JSON.stringify( { name: 'core/block', ref: 123 } ) ]: 2,
[ JSON.stringify( { name: 'core/block', ref: 456 } ) ]: 2,
},
};

const state = preferences( deepFreeze( initialState ), {
type: 'REMOVE_REUSABLE_BLOCK',
id: 123,
} );

expect( state ).toEqual( {
recentInserts: [
{ name: 'core/paragraph' },
{ name: 'core/block', ref: 456 },
],
insertFrequency: {
[ JSON.stringify( { name: 'core/paragraph' } ) ]: 4,
[ JSON.stringify( { name: 'core/block', ref: 456 } ) ]: 2,
},
} );
} );

it( 'should toggle a feature flag', () => {
const state = preferences( deepFreeze( { features: { chicken: true } } ), {
type: 'TOGGLE_FEATURE',
Expand Down

0 comments on commit fb2669f

Please sign in to comment.