@@ -9,26 +9,33 @@ import { useSidebarContext } from '../common/context/SidebarContext';
99import {
1010 useContainer ,
1111 useRemoveContainerChildren ,
12- useAddItemsToContainer ,
1312 useLibraryBlockMetadata ,
13+ useContainerChildren ,
14+ useUpdateContainerChildren ,
1415} from '../data/apiHooks' ;
1516import messages from './messages' ;
17+ import { LibraryBlockMetadata } from '../data/api' ;
1618
1719interface Props {
1820 usageKey : string ;
21+ index ?: number ;
1922 close : ( ) => void ;
2023}
2124
22- const ComponentRemover = ( { usageKey, close } : Props ) => {
25+ const ComponentRemover = ( { usageKey, index , close } : Props ) => {
2326 const intl = useIntl ( ) ;
2427 const { sidebarItemInfo, closeLibrarySidebar } = useSidebarContext ( ) ;
25- const { containerId } = useLibraryContext ( ) ;
28+ const { containerId, showOnlyPublished } = useLibraryContext ( ) ;
2629 const { showToast } = useContext ( ToastContext ) ;
2730
2831 const removeContainerItemMutation = useRemoveContainerChildren ( containerId ) ;
29- const addItemToContainerMutation = useAddItemsToContainer ( containerId ) ;
32+ const updateContainerChildrenMutation = useUpdateContainerChildren ( containerId ) ;
3033 const { data : container , isPending : isPendingParentContainer } = useContainer ( containerId ) ;
3134 const { data : component , isPending } = useLibraryBlockMetadata ( usageKey ) ;
35+ // Use update api for children if duplicates are present to avoid removing all instances of the child
36+ const { data : children } = useContainerChildren < LibraryBlockMetadata > ( containerId , showOnlyPublished ) ;
37+ const childrenUsageIds = children ?. map ( ( child ) => child . id ) ;
38+ const hasDuplicates = ( childrenUsageIds ?. filter ( ( child ) => child === usageKey ) . length || 0 ) > 1 ;
3239
3340 // istanbul ignore if: loading state
3441 if ( isPending || isPendingParentContainer ) {
@@ -37,8 +44,11 @@ const ComponentRemover = ({ usageKey, close }: Props) => {
3744 }
3845
3946 const removeFromContainer = ( ) => {
47+ if ( ! childrenUsageIds ) {
48+ return ;
49+ }
4050 const restoreComponent = ( ) => {
41- addItemToContainerMutation . mutateAsync ( [ usageKey ] ) . then ( ( ) => {
51+ updateContainerChildrenMutation . mutateAsync ( childrenUsageIds ) . then ( ( ) => {
4252 showToast ( intl . formatMessage ( messages . undoRemoveComponentFromContainerToastSuccess ) ) ;
4353 } ) . catch ( ( ) => {
4454 showToast ( intl . formatMessage ( messages . undoRemoveComponentFromContainerToastFailed ) ) ;
@@ -63,6 +73,37 @@ const ComponentRemover = ({ usageKey, close }: Props) => {
6373 close ( ) ;
6474 } ;
6575
76+ const excludeOneInstance = ( ) => {
77+ if ( ! childrenUsageIds || typeof index === 'undefined' ) {
78+ return ;
79+ }
80+ const updatedKeys = childrenUsageIds . filter ( ( childId , idx ) => childId !== usageKey || idx !== index ) ;
81+ const restoreComponent = ( ) => {
82+ updateContainerChildrenMutation . mutateAsync ( childrenUsageIds ) . then ( ( ) => {
83+ showToast ( intl . formatMessage ( messages . undoRemoveComponentFromContainerToastSuccess ) ) ;
84+ } ) . catch ( ( ) => {
85+ showToast ( intl . formatMessage ( messages . undoRemoveComponentFromContainerToastFailed ) ) ;
86+ } ) ;
87+ } ;
88+ updateContainerChildrenMutation . mutateAsync ( updatedKeys ) . then ( ( ) => {
89+ if ( sidebarItemInfo ?. id === usageKey && sidebarItemInfo ?. index === index ) {
90+ // Close sidebar if current component is open
91+ closeLibrarySidebar ( ) ;
92+ }
93+ showToast (
94+ intl . formatMessage ( messages . removeComponentFromContainerSuccess ) ,
95+ {
96+ label : intl . formatMessage ( messages . undoRemoveComponentFromContainerToastAction ) ,
97+ onClick : restoreComponent ,
98+ } ,
99+ ) ;
100+ } ) . catch ( ( ) => {
101+ showToast ( intl . formatMessage ( messages . removeComponentFromContainerFailure ) ) ;
102+ } ) ;
103+
104+ close ( ) ;
105+ }
106+
66107 const removeText = intl . formatMessage ( messages . removeComponentConfirm , {
67108 componentName : < b > { component ?. displayName } </ b > ,
68109 parentContainerName : < b > { container ?. displayName } </ b > ,
@@ -76,7 +117,7 @@ const ComponentRemover = ({ usageKey, close }: Props) => {
76117 title = { intl . formatMessage ( messages . removeComponentWarningTitle ) }
77118 icon = { Warning }
78119 description = { removeText }
79- onDeleteSubmit = { removeFromContainer }
120+ onDeleteSubmit = { hasDuplicates ? excludeOneInstance : removeFromContainer }
80121 btnLabel = { intl . formatMessage ( messages . componentRemoveButtonLabel ) }
81122 buttonVariant = "primary"
82123 />
0 commit comments