Skip to content

Commit

Permalink
save theme location on button click (#30340)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzim authored Mar 30, 2021
1 parent 169a0ca commit 2a1c395
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
10 changes: 9 additions & 1 deletion packages/edit-navigation/src/components/name-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useEffect, useRef, useContext } from '@wordpress/element';
import { TextControl } from '@wordpress/components';
import {
IsMenuNameControlFocusedContext,
untitledMenu,
useMenuEntity,
useSelectedMenuData,
} from '../../hooks';
Expand All @@ -19,7 +20,14 @@ export function NameEditor() {
);

const { menuId } = useSelectedMenuData();
const { editMenuName, editedMenuName } = useMenuEntity( menuId );
const { editedMenu, editMenuEntityRecord, menuEntityData } = useMenuEntity(
menuId
);
const editedMenuName = menuId && editedMenu.name;

const editMenuName = ( name = untitledMenu ) =>
editMenuEntityRecord( ...menuEntityData, { name } );

const inputRef = useRef();
useEffect( () => {
if ( isMenuNameEditFocused ) inputRef.current.focus();
Expand Down
12 changes: 3 additions & 9 deletions packages/edit-navigation/src/hooks/use-menu-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { useSelect, useDispatch } from '@wordpress/data';
*/
import { MENU_KIND, MENU_POST_TYPE } from '../constants';

import { untitledMenu } from './index';

export default function useMenuEntity( menuId ) {
const { editEntityRecord } = useDispatch( 'core' );

Expand All @@ -20,13 +18,9 @@ export default function useMenuEntity( menuId ) {
[ menuId ]
);

const editedMenuName = menuId && editedMenu.name;

const editMenuName = ( name = untitledMenu ) =>
editEntityRecord( ...menuEntityData, { name } );

return {
editedMenuName,
editMenuName,
editedMenu,
menuEntityData,
editMenuEntityRecord: editEntityRecord,
};
}
25 changes: 13 additions & 12 deletions packages/edit-navigation/src/hooks/use-menu-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
*/
import { useState, useEffect, useMemo, useCallback } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { useDispatch } from '@wordpress/data';

/**
* External dependencies
*/
import { merge } from 'lodash';
/**
* Internal dependencies
*/
import { useMenuEntity, useSelectedMenuData } from './index';

const locationsForMenuId = ( menuLocationsByName, id ) =>
Object.values( menuLocationsByName )
.filter( ( { menu } ) => menu === id )
.map( ( { name } ) => name );

const withLocations = ( menuLocationsByName ) => ( id ) => ( {
id,
locations: locationsForMenuId( menuLocationsByName, id ),
} );

export default function useMenuLocations() {
const [ menuLocationsByName, setMenuLocationsByName ] = useState( null );

const { menuId } = useSelectedMenuData();
const { editMenuEntityRecord, menuEntityData } = useMenuEntity( menuId );
useEffect( () => {
let isMounted = true;

Expand All @@ -41,8 +41,6 @@ export default function useMenuLocations() {
return () => ( isMounted = false );
}, [] );

const { saveMenu } = useDispatch( 'core' );

const assignMenuToLocation = useCallback(
async ( locationName, newMenuId ) => {
const oldMenuId = menuLocationsByName[ locationName ].menu;
Expand All @@ -53,10 +51,13 @@ export default function useMenuLocations() {

setMenuLocationsByName( newMenuLocationsByName );

[ oldMenuId, newMenuId ]
.filter( Boolean )
.map( withLocations( newMenuLocationsByName ) )
.forEach( saveMenu );
const activeMenuId = oldMenuId || newMenuId;
editMenuEntityRecord( ...menuEntityData, {
locations: locationsForMenuId(
newMenuLocationsByName,
activeMenuId
),
} );
},
[ menuLocationsByName ]
);
Expand Down

0 comments on commit 2a1c395

Please sign in to comment.