Update alpine handling

This commit is contained in:
DariusIII
2026-02-20 09:58:35 +01:00
parent a372ffc0bb
commit 0823bef407
5 changed files with 55 additions and 10 deletions
@@ -135,4 +135,38 @@ class AdminCategoryController extends BasePageController
return view('admin.categories.edit', $this->viewData);
}
/**
* Delete a category.
*
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
*/
public function destroy(Request $request)
{
$this->setAdminPrefs();
$id = $request->input('id');
if (! $id) {
return redirect()->to('admin/category-list')->with('error', 'No category ID provided.');
}
$category = Category::find($id);
if (! $category) {
return redirect()->to('admin/category-list')->with('error', 'Category not found.');
}
// Check for child categories
if (Category::where('root_categories_id', $id)->exists()) {
return redirect()->to('admin/category-list')->with('error', 'Cannot delete category "'.$category->title.'" because it has child categories. Remove or reassign them first.');
}
$title = $category->title;
$category->delete();
return redirect()->to('admin/category-list')->with('success', 'Category "'.$title.'" deleted successfully.');
}
}
+17 -1
View File
@@ -23,13 +23,21 @@ Alpine.data('commentDeleteModal', () => ({
init() {
this._baseUrl = this.$el.dataset.deleteUrl || '';
// Listen for delete button clicks via delegation (CSP-safe, no $dispatch with inline objects)
var self = this;
document.addEventListener('click', function(e) {
var btn = e.target.closest('.comment-delete-btn');
if (btn) {
self.openModal(btn.dataset.commentId, btn.dataset.commentText);
}
});
},
openModal(id, text) {
this.commentId = id;
this.commentText = text;
this.open = true;
const form = this.$refs.deleteForm;
var form = this.$refs.deleteForm;
if (form) {
form.action = this._baseUrl + '?id=' + id;
}
@@ -49,6 +57,14 @@ Alpine.data('categoryDeleteModal', () => ({
init() {
this._baseUrl = this.$el.dataset.deleteUrl || '';
// Listen for delete button clicks via delegation (CSP-safe, no $dispatch with inline objects)
var self = this;
document.addEventListener('click', function(e) {
var btn = e.target.closest('.category-delete-btn');
if (btn) {
self.openModal(btn.dataset.categoryId);
}
});
},
openModal(id) {
@@ -118,9 +118,8 @@
<i class="fa fa-edit"></i>
</a>
<button type="button"
class="text-red-600 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300"
class="category-delete-btn text-red-600 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300"
data-category-id="{{ $category->id }}"
x-on:click="$dispatch('open-category-delete-modal', { id: $el.dataset.categoryId })"
title="Delete Category">
<i class="fa fa-trash"></i>
</button>
@@ -159,7 +158,6 @@
<!-- Delete Confirmation Modal -->
<div x-data="categoryDeleteModal"
data-delete-url="{{ url('/admin/category-delete') }}"
x-on:open-category-delete-modal.window="openModal($event.detail.id)"
x-show="open"
x-cloak
class="fixed inset-0 bg-gray-600/50 overflow-y-auto h-full w-full z-50">
@@ -190,7 +188,5 @@
</div>
</div>
</div>
{{-- Scripts moved to resources/js/csp-safe.js --}}
@endsection
@@ -121,11 +121,10 @@
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<button type="button"
class="text-red-600 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300 transition"
class="comment-delete-btn text-red-600 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300 transition"
title="Delete"
data-comment-id="{{ $comment->id }}"
data-comment-text="{{ addslashes(Str::limit($comment->text, 50)) }}"
x-on:click="$dispatch('open-delete-modal', { id: $el.dataset.commentId, text: $el.dataset.commentText })">
data-comment-text="{{ addslashes(Str::limit($comment->text, 50)) }}">
<i class="fas fa-trash"></i>
</button>
</td>
@@ -151,7 +150,6 @@
<!-- Delete Confirmation Modal -->
<div x-data="commentDeleteModal"
data-delete-url="{{ url('admin/comment-delete') }}"
x-on:open-delete-modal.window="openModal($event.detail.id, $event.detail.text)"
x-show="open"
x-cloak
class="fixed inset-0 bg-gray-900/50 flex items-center justify-center z-50 transition-opacity">
+1
View File
@@ -203,6 +203,7 @@ Route::middleware(['role:Admin', '2fa'])->prefix('admin')->group(function () {
Route::get('category-list', [AdminCategoryController::class, 'index'])->name('admin.category-list');
Route::match(['GET', 'POST'], 'category-add', [AdminCategoryController::class, 'create'])->name('admin.category-add');
Route::match(['GET', 'POST'], 'category-edit', [AdminCategoryController::class, 'edit'])->name('admin.category-edit');
Route::get('category-delete', [AdminCategoryController::class, 'destroy'])->name('admin.category-delete');
Route::get('user-list', [AdminUserController::class, 'index'])->name('admin.user-list');
Route::match(['GET', 'POST'], 'user-edit', [AdminUserController::class, 'edit'])->name('admin.user-edit');
Route::post('user-delete', [AdminUserController::class, 'destroy'])->name('admin.user-delete');