From c907f405083b27de8cfd62570284ab5e301ac1f7 Mon Sep 17 00:00:00 2001
From: myrmidex
Date: Mon, 29 Dec 2025 23:35:57 +0100
Subject: [PATCH] Arrange priority
---
app/Http/Controllers/BucketController.php | 37 +++++++++-----
resources/js/pages/Scenarios/Show.tsx | 62 +++++++++++++++++++++--
2 files changed, 82 insertions(+), 17 deletions(-)
diff --git a/app/Http/Controllers/BucketController.php b/app/Http/Controllers/BucketController.php
index 6142a05..80e9742 100644
--- a/app/Http/Controllers/BucketController.php
+++ b/app/Http/Controllers/BucketController.php
@@ -7,6 +7,7 @@
use App\Models\Scenario;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
class BucketController extends Controller
@@ -198,20 +199,28 @@ private function updateBucketPriority(Bucket $bucket, int $newPriority): void
return;
}
- if ($newPriority < $oldPriority) {
- // Moving up - shift others down
- $scenario->buckets()
- ->where('id', '!=', $bucket->id)
- ->whereBetween('priority', [$newPriority, $oldPriority - 1])
- ->increment('priority');
- } else {
- // Moving down - shift others up
- $scenario->buckets()
- ->where('id', '!=', $bucket->id)
- ->whereBetween('priority', [$oldPriority + 1, $newPriority])
- ->decrement('priority');
- }
+ // Use database transaction to handle constraint conflicts
+ DB::transaction(function () use ($bucket, $scenario, $oldPriority, $newPriority) {
+ // Temporarily set the moving bucket to a high priority to avoid conflicts
+ $tempPriority = $scenario->buckets()->max('priority') + 100;
+ $bucket->update(['priority' => $tempPriority]);
- $bucket->update(['priority' => $newPriority]);
+ if ($newPriority < $oldPriority) {
+ // Moving up - shift others down
+ $scenario->buckets()
+ ->where('id', '!=', $bucket->id)
+ ->whereBetween('priority', [$newPriority, $oldPriority - 1])
+ ->increment('priority');
+ } else {
+ // Moving down - shift others up
+ $scenario->buckets()
+ ->where('id', '!=', $bucket->id)
+ ->whereBetween('priority', [$oldPriority + 1, $newPriority])
+ ->decrement('priority');
+ }
+
+ // Finally, set the bucket to its new priority
+ $bucket->update(['priority' => $newPriority]);
+ });
}
}
diff --git a/resources/js/pages/Scenarios/Show.tsx b/resources/js/pages/Scenarios/Show.tsx
index bd61c21..bf3deff 100644
--- a/resources/js/pages/Scenarios/Show.tsx
+++ b/resources/js/pages/Scenarios/Show.tsx
@@ -69,6 +69,40 @@ export default function Show({ scenario, buckets }: Props) {
}
};
+ const handlePriorityChange = async (bucketId: number, direction: 'up' | 'down') => {
+ const bucket = buckets.find(b => b.id === bucketId);
+ if (!bucket) return;
+
+ const newPriority = direction === 'up' ? bucket.priority - 1 : bucket.priority + 1;
+
+ // Don't allow moving beyond bounds
+ if (newPriority < 1 || newPriority > buckets.length) return;
+
+ try {
+ const response = await fetch(`/buckets/${bucketId}`, {
+ method: 'PATCH',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '',
+ },
+ body: JSON.stringify({
+ name: bucket.name,
+ allocation_type: bucket.allocation_type,
+ allocation_value: bucket.allocation_value,
+ priority: newPriority
+ }),
+ });
+
+ if (response.ok) {
+ router.reload({ only: ['buckets'] });
+ } else {
+ console.error('Failed to update bucket priority');
+ }
+ } catch (error) {
+ console.error('Error updating bucket priority:', error);
+ }
+ };
+
return (
<>
@@ -123,9 +157,31 @@ export default function Show({ scenario, buckets }: Props) {
Priority {bucket.priority} • {bucket.allocation_type_label}
-
- #{bucket.priority}
-
+
+
+
+
+ #{bucket.priority}
+
+