Skip to content

Commit

Permalink
Global styles: leave screen if current shadow entry gets deleted (Wor…
Browse files Browse the repository at this point in the history
…dPress#65935)

* Global styles: leave screen if current shadow entry gets deleted

* Refine approach

* Apply feedback

---

Co-authored-by: ciampo <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: nith53 <[email protected]>
Co-authored-by: benniledl <[email protected]>
  • Loading branch information
6 people authored and karthick-murugan committed Nov 13, 2024
1 parent 45a90f6 commit be48daa
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
reset,
moreVertical,
} from '@wordpress/icons';
import { useState, useMemo } from '@wordpress/element';
import { useState, useMemo, useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -73,12 +73,30 @@ const presetShadowMenuItems = [

export default function ShadowsEditPanel() {
const {
goBack,
params: { category, slug },
goTo,
} = useNavigator();
const [ shadows, setShadows ] = useGlobalSetting(
`shadow.presets.${ category }`
);

useEffect( () => {
const hasCurrentShadow = shadows?.some(
( shadow ) => shadow.slug === slug
);
// If the shadow being edited doesn't exist anymore in the global styles setting, navigate back
// to prevent the user from editing a non-existent shadow entry.
// This can happen, for example:
// - when the user deletes the shadow
// - when the user resets the styles while editing a custom shadow
//
// The check on the slug is necessary to prevent a double back navigation when the user triggers
// a backward navigation by interacting with the screen's UI.
if ( !! slug && ! hasCurrentShadow ) {
goBack();
}
}, [ shadows, slug, goBack ] );

const [ baseShadows ] = useGlobalSetting(
`shadow.presets.${ category }`,
undefined,
Expand Down Expand Up @@ -123,9 +141,7 @@ export default function ShadowsEditPanel() {
};

const handleShadowDelete = () => {
const updatedShadows = shadows.filter( ( s ) => s.slug !== slug );
setShadows( updatedShadows );
goTo( `/shadows` );
setShadows( shadows.filter( ( s ) => s.slug !== slug ) );
};

const handleShadowRename = ( newName ) => {
Expand Down

0 comments on commit be48daa

Please sign in to comment.