@@ -27,10 +27,12 @@ class="space-y-6"
27
27
<!-- Tree Items -->
28
28
<div class =" space-y-2" id =" menu-items-container" wire:key =" tree-{{ $refreshKey } }" >
29
29
@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 >
34
36
@endforeach
35
37
</div >
36
38
@endif
@@ -94,14 +96,44 @@ function menuItemsManager(config) {
94
96
handle: ' .drag-handle' ,
95
97
onEnd : (evt ) => {
96
98
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);
99
100
}
100
101
}
101
102
});
102
103
}
103
104
}
104
105
},
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
+ },
105
137
}
106
138
}
107
139
</script >
0 commit comments