Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplication after insertion in Posts Inserted block #191

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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