diff --git a/resources/js/pages/Scenarios/Show.tsx b/resources/js/pages/Scenarios/Show.tsx index bf3deff..6eef5d3 100644 --- a/resources/js/pages/Scenarios/Show.tsx +++ b/resources/js/pages/Scenarios/Show.tsx @@ -30,19 +30,59 @@ interface Props { export default function Show({ scenario, buckets }: Props) { const [isModalOpen, setIsModalOpen] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); + const [editingBucket, setEditingBucket] = useState(null); const [formData, setFormData] = useState({ name: '', allocation_type: 'fixed_limit', allocation_value: '' }); + const handleEdit = (bucket: Bucket) => { + setEditingBucket(bucket); + setFormData({ + name: bucket.name, + allocation_type: bucket.allocation_type, + allocation_value: bucket.allocation_value ? bucket.allocation_value.toString() : '' + }); + setIsModalOpen(true); + }; + + const handleDelete = async (bucket: Bucket) => { + if (!confirm(`Are you sure you want to delete "${bucket.name}"?`)) { + return; + } + + try { + const response = await fetch(`/buckets/${bucket.id}`, { + method: 'DELETE', + headers: { + 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '', + }, + }); + + if (response.ok) { + router.reload({ only: ['buckets'] }); + } else { + console.error('Failed to delete bucket'); + } + } catch (error) { + console.error('Error deleting bucket:', error); + } + }; + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); + const url = editingBucket + ? `/buckets/${editingBucket.id}` + : `/scenarios/${scenario.id}/buckets`; + + const method = editingBucket ? 'PATCH' : 'POST'; + try { - const response = await fetch(`/scenarios/${scenario.id}/buckets`, { - method: 'POST', + const response = await fetch(url, { + method: method, headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '', @@ -50,20 +90,22 @@ export default function Show({ scenario, buckets }: Props) { body: JSON.stringify({ name: formData.name, allocation_type: formData.allocation_type, - allocation_value: formData.allocation_value ? parseFloat(formData.allocation_value) : null + allocation_value: formData.allocation_value ? parseFloat(formData.allocation_value) : null, + priority: editingBucket ? editingBucket.priority : undefined }), }); if (response.ok) { setIsModalOpen(false); setFormData({ name: '', allocation_type: 'fixed_limit', allocation_value: '' }); + setEditingBucket(null); router.reload({ only: ['buckets'] }); } else { const errorData = await response.json(); - console.error('Failed to create bucket:', errorData); + console.error(`Failed to ${editingBucket ? 'update' : 'create'} bucket:`, errorData); } } catch (error) { - console.error('Error creating bucket:', error); + console.error(`Error ${editingBucket ? 'updating' : 'creating'} bucket:`, error); } finally { setIsSubmitting(false); } @@ -135,7 +177,11 @@ export default function Show({ scenario, buckets }: Props) {

Buckets

- -
@@ -268,7 +320,7 @@ export default function Show({ scenario, buckets }: Props) {
-

Add New Bucket

+

{editingBucket ? 'Edit Bucket' : 'Add New Bucket'}