@@ -27,10 +27,12 @@ class="space-y-6"
2727 <!-- Tree Items -->
2828 <div class =" space-y-2" id =" menu-items-container" wire:key =" tree-{{ $refreshKey } }" >
2929 @foreach ($this -> record -> menuItems ()-> with ([' children' , ' linkable' ])-> whereNull (' parent_id' )-> orderBy (' _lft' )-> get () as $item )
30- @livewire (' filament-flexible-content-block-pages::menu-tree-item' , [
31- ' item' => $item ,
32- ' maxDepth' => $this -> getMaxDepth ()
33- ], key (" item-{$item -> id }-{$refreshKey }" ) )
30+ <div data-item-id =" {{ $item -> id } }" >
31+ @livewire (' filament-flexible-content-block-pages::menu-tree-item' , [
32+ ' item' => $item ,
33+ ' maxDepth' => $this -> getMaxDepth ()
34+ ], key (" item-{$item -> id }-{$refreshKey }" ) )
35+ </div >
3436 @endforeach
3537 </div >
3638 @endif
@@ -94,14 +96,44 @@ function menuItemsManager(config) {
9496 handle: ' .drag-handle' ,
9597 onEnd : (evt ) => {
9698 if (evt .oldIndex !== evt .newIndex ) {
97- // TODO: Implement reordering with proper nested set handling
98- console .log (' Reorder from' , evt .oldIndex , ' to' , evt .newIndex );
99+ this .saveReorder (evt);
99100 }
100101 }
101102 });
102103 }
103104 }
104105 },
106+
107+ saveReorder (evt ) {
108+ this .loading = true ;
109+
110+ // Get all items in their new order
111+ const container = document .getElementById (' menu-items-container' );
112+ const items = Array .from (container .children ).map ((element , index ) => {
113+ // Extract item ID from the wire:key attribute or data attribute
114+ const itemId = this .extractItemId (element);
115+ return {
116+ id: itemId,
117+ position: index,
118+ parent_id: null // Root level items for now
119+ };
120+ });
121+
122+ // Call the Livewire method to save the new order
123+ this .$wire .reorderMenuItems (items).then (() => {
124+ this .loading = false ;
125+ }).catch ((error ) => {
126+ console .error (' Reorder failed:' , error);
127+ this .loading = false ;
128+ // Revert the visual change by refreshing
129+ this .refreshMenuItems ();
130+ });
131+ },
132+
133+ extractItemId (element ) {
134+ const itemId = element .dataset .itemId ;
135+ return itemId ? parseInt (itemId) : null ;
136+ },
105137 }
106138 }
107139 </script >
0 commit comments