Skip to content

Commit

Permalink
Openverse: prevent multiple insertions during upload (#65719)
Browse files Browse the repository at this point in the history
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: youknowriad <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 22e19b4 commit 76a389f
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export function MediaPreview( { media, onClick, category } ) {
);
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );
const { getSettings } = useSelect( blockEditorStore );
const { getSettings, getBlock } = useSelect( blockEditorStore );
const { updateBlockAttributes } = useDispatch( blockEditorStore );

const onMediaInsert = useCallback(
( previewBlock ) => {
Expand Down Expand Up @@ -180,18 +181,31 @@ export function MediaPreview( { media, onClick, category } ) {
if ( isBlobURL( img.url ) ) {
return;
}
onClick( {
...clonedBlock,
attributes: {

if ( ! getBlock( clonedBlock.clientId ) ) {
// Ensure the block is only inserted once.
onClick( {
...clonedBlock,
attributes: {
...clonedBlock.attributes,
id: img.id,
url: img.url,
},
} );

createSuccessNotice(
__( 'Image uploaded and inserted.' ),
{ type: 'snackbar', id: 'inserter-notice' }
);
} else {
// For subsequent calls, update the existing block.
updateBlockAttributes( clonedBlock.clientId, {
...clonedBlock.attributes,
id: img.id,
url: img.url,
},
} );
createSuccessNotice(
__( 'Image uploaded and inserted.' ),
{ type: 'snackbar', id: 'inserter-notice' }
);
} );
}

setIsInserting( false );
},
allowedTypes: ALLOWED_MEDIA_TYPES,
Expand All @@ -214,7 +228,9 @@ export function MediaPreview( { media, onClick, category } ) {
getSettings,
onClick,
createSuccessNotice,
updateBlockAttributes,
createErrorNotice,
getBlock,
]
);

Expand Down

1 comment on commit 76a389f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 76a389f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11105210943
📝 Reported issues:

Please sign in to comment.