Skip to content

Commit

Permalink
fix: prevent duplication after insertion in Posts Inserted block (#191)
Browse files Browse the repository at this point in the history
Closes #185
  • Loading branch information
adekbadek authored May 5, 2020
1 parent 5415704 commit d4ac6d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
34 changes: 32 additions & 2 deletions src/editor/blocks/posts-inserter/deduplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { POSTS_INSERTER_BLOCK_NAME, POSTS_INSERTER_STORE_NAME } from './consts';
const DEFAULT_STATE = {
postIdsByBlocks: {},
existingBlockIdsInOrder: [],
insertedPostIds: [],
};

const actions = {
Expand All @@ -26,6 +27,17 @@ const actions = {
props,
};
},
/**
* After insertion, save the inserted post ids.
*
* @param {Array} insertedPostIds post ids
*/
setInsertedPostsIds( insertedPostIds ) {
return {
type: 'SET_INSERTED_POST_IDS',
insertedPostIds,
};
},
removeBlock( clientId ) {
return {
type: 'REMOVE_BLOCK',
Expand All @@ -47,6 +59,7 @@ registerStore( POSTS_INSERTER_STORE_NAME, {
const { clientId, existingBlocks } = action.props;
const existingBlockIdsInOrder = getAllPostsInserterBlocksIds( existingBlocks );
return {
...state,
existingBlockIdsInOrder,
postIdsByBlocks: pick(
{
Expand All @@ -56,6 +69,11 @@ registerStore( POSTS_INSERTER_STORE_NAME, {
existingBlockIdsInOrder
),
};
case 'SET_INSERTED_POST_IDS':
return {
...state,
insertedPostIds: uniq( [ ...state.insertedPostIds, ...action.insertedPostIds ] ),
};
case 'REMOVE_BLOCK':
return {
...state,
Expand All @@ -70,10 +88,22 @@ registerStore( POSTS_INSERTER_STORE_NAME, {
actions,

selectors: {
getHandledPostIds( { postIdsByBlocks, existingBlockIdsInOrder }, blockClientId ) {
getHandledPostIds(
{ postIdsByBlocks, existingBlockIdsInOrder, insertedPostIds },
blockClientId
) {
const blockIndex = existingBlockIdsInOrder.indexOf( blockClientId );
const blocksBeforeIds = slice( existingBlockIdsInOrder, 0, blockIndex );
return uniq( flatten( values( pick( postIdsByBlocks, blocksBeforeIds ) ) ) );
return [
/**
* Ids of posts handled by the existing blocks.
*/
...uniq( flatten( values( pick( postIdsByBlocks, blocksBeforeIds ) ) ) ),
/**
* Ids of posts that were inserted.
*/
...insertedPostIds,
];
},
},
} );
14 changes: 10 additions & 4 deletions src/editor/blocks/posts-inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const PostsInserterBlock = ( {
postList,
replaceBlocks,
setHandledPostsIds,
setInsertedPostsIds,
removeBlock,
} ) => {
const templateBlocks = getTemplateBlocks( postList, attributes );
Expand All @@ -44,19 +45,21 @@ const PostsInserterBlock = ( {
setAttributes( { innerBlocksToInsert } );
}, [ JSON.stringify( innerBlocksToInsert ) ]);

const handledPostIds = postList.map( post => post.id );

useEffect(() => {
if ( attributes.areBlocksInserted ) {
replaceBlocks( templateBlocks );
setInsertedPostsIds( handledPostIds );
}
}, [ attributes.areBlocksInserted ]);

const ids = postList.map( post => post.id );
useEffect(() => {
if ( ! attributes.preventDeduplication ) {
setHandledPostsIds( ids );
setHandledPostsIds( handledPostIds );
return removeBlock;
}
}, [ ids.join() ]);
}, [ handledPostIds.join() ]);

const blockControlsImages = [
{
Expand Down Expand Up @@ -199,12 +202,15 @@ const PostsInserterBlockWithSelect = compose( [
} ),
withDispatch( ( dispatch, props ) => {
const { replaceBlocks } = dispatch( 'core/block-editor' );
const { setHandledPostsIds, removeBlock } = dispatch( POSTS_INSERTER_STORE_NAME );
const { setHandledPostsIds, setInsertedPostsIds, removeBlock } = dispatch(
POSTS_INSERTER_STORE_NAME
);
return {
replaceBlocks: blocks => {
replaceBlocks( props.selectedBlock.clientId, blocks );
},
setHandledPostsIds: ids => setHandledPostsIds( ids, props ),
setInsertedPostsIds,
removeBlock: () => removeBlock( props.clientId ),
};
} ),
Expand Down

0 comments on commit d4ac6d8

Please sign in to comment.