Skip to content

Commit 1651f79

Browse files
committed
Save re-ordering
1 parent 0b39c16 commit 1651f79

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

resources/views/filament/resources/menu-resource/pages/manage-menu-items.blade.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

resources/views/livewire/menu-tree-item.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="menu-tree-item" data-item-id="{{ $item->id }}">
1+
<div class="menu-tree-item" data-item-id="{{ $item->id }}" wire:key="tree-item-{{ $item->id }}">
22
<div class="flex items-center p-4 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 group hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors {{ $depth > 0 ? 'ml-' . ($depth * 8) : '' }}">
33

44
@if($showActions)

src/Resources/MenuResource/Pages/ManageMenuItems.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ public function reorderMenuItems(array $orderedItems): void
294294

295295
// Validate that all items belong to this menu
296296
$itemIds = array_column($orderedItems, 'id');
297+
298+
// Filter out null IDs
299+
$itemIds = array_filter($itemIds, function($id) {
300+
return $id !== null && is_numeric($id);
301+
});
302+
303+
if (empty($itemIds)) {
304+
Notification::make()
305+
->title(flexiblePagesTrans('menu_items.errors.no_items_to_reorder'))
306+
->danger()
307+
->send();
308+
309+
return;
310+
}
311+
297312
$validItems = $menuItemModel::whereIn('id', $itemIds)
298313
->where('menu_id', $this->record->id)
299314
->count();

0 commit comments

Comments
 (0)