Skip to content

Commit

Permalink
Only allow editing of nav-links
Browse files Browse the repository at this point in the history
  • Loading branch information
oakesjosh committed Mar 1, 2024
1 parent a0e7ede commit 6eed986
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/blocks/navigation/components/menu-editor/edit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useState } from '@wordpress/element';
import { TextControl, Button } from '@wordpress/components';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { get } from 'lodash';
import { createBlock } from '@wordpress/blocks';
import { serialize } from '@wordpress/blocks';

Check failure on line 7 in src/blocks/navigation/components/menu-editor/edit.js

View workflow job for this annotation

GitHub Actions / lint

'@wordpress/blocks' import is duplicated

function NavigationItem({ thisBlock, allBlocks, index, updateLocalBlocks, maxIndex }) {
const [isEditing, setIsEditing] = useState(false);
Expand Down Expand Up @@ -66,8 +68,12 @@ function NavigationItem({ thisBlock, allBlocks, index, updateLocalBlocks, maxInd
<>
<p>
<strong>Block:</strong> {thisBlock.name} <br />
<strong>Label:</strong> {thisBlock.attributes.label} <br />
<button onClick={() => setIsEditing(!isEditing)}>Edit</button>
{thisBlock.name === 'kadence/navigation-link' && (
<>
<strong>Label:</strong> {thisBlock.attributes.label} <br />
<button onClick={() => setIsEditing(!isEditing)}>Edit</button>
</>
)}
</p>
{isEditing && (
<>
Expand Down Expand Up @@ -131,6 +137,7 @@ const renderBlocks = (allBlocks, innerBlocks, updateLocalBlocks) => {
};

export default function MenuEdit({ selectedPostId }) {
const { saveEntityRecord } = useDispatch('core');
const [blocks, , onChange] = useEntityBlockEditor('postType', 'kadence_navigation', { id: selectedPostId });
const initialBlocks = get(blocks, [0, 'innerBlocks'], []);

Expand All @@ -145,7 +152,7 @@ export default function MenuEdit({ selectedPostId }) {
updateLocalBlocks([...initialBlocks]); // Reset to initial state
};

const saveChanges = () => {
const saveChanges = async () => {
console.log('Saving changes');
onChange(
[
Expand All @@ -156,6 +163,16 @@ export default function MenuEdit({ selectedPostId }) {
],
blocks[0].clientId
);
try {
const newBlock = createBlock('kadence/navigation', {}, innerBlocks);
await saveEntityRecord('postType', 'kadence_navigation', {
id: selectedPostId,
content: serialize(newBlock),
});
console.log('Post saved successfully');
} catch (error) {
console.error('Error saving post:', error);
}
};

return (
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/navigation/components/menu-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import { __ } from '@wordpress/i18n';
import { Flex, FlexBlock, FlexItem } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { EntityProvider } from '@wordpress/core-data';

import MenuEdit from './edit';

// Register the blocks for use outside the block editor
import '../../../navigation/index';
import '../../../navigation-link/index';
import '../../../rowlayout/index';
import '../../../column/index';
import '../../../advancedheading/block';

export default function MenuEditor() {
const [selectedPostId, setSelectedPostId] = useState(0);
Expand Down

0 comments on commit 6eed986

Please sign in to comment.