refactor: Consistently handle formula data as a PHP array in Livewire component and view.

This commit is contained in:
Yoga Pangestu 2025-12-18 10:17:05 +07:00
parent 9cf985e8f9
commit 4aad6aa730
2 changed files with 40 additions and 28 deletions

View File

@ -24,7 +24,7 @@ class Formula extends Component
public FormulaForm $form;
public $formulas;
public array $formulas = [];
public string $method = 'create';
@ -48,8 +48,9 @@ public function mount(): void
])
->sortBy('volume')
->values()
->all()
);
->toArray()
)
->toArray();
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
}
@ -74,16 +75,20 @@ public function create(): void
'volume' => $formula->volume,
];
// Get the group for this size, or an empty Collection
$group = $this->formulas->get($size, collect());
// Convert to collection for manipulation
$formulas = collect($this->formulas);
// Get the group for this size, or an empty collection
$group = collect($formulas->get($size, []));
// Add, then re-sort
$group = $group->push($newFormula)
->sortBy('volume')
->values();
->values()
->toArray();
// Put it back
$this->formulas->put($size, $group);
// Put it back and convert to array
$this->formulas = $formulas->put($size, $group)->toArray();
$this->toast('Rumus berhasil ditambahkan.');
@ -107,20 +112,23 @@ public function update(): void
$formula = $this->form->update();
// update array formulas
$this->formulas = $this->formulas->map(function ($group) use ($formula) {
return collect($group)
->map(function ($item) use ($formula) {
if ($item['hash'] === $formula->hash) {
$item['quality'] = $formula->quality;
$item['volume'] = $formula->volume;
}
// Convert to collection for manipulation
$this->formulas = collect($this->formulas)
->map(function ($group) use ($formula) {
return collect($group)
->map(function ($item) use ($formula) {
if ($item['hash'] === $formula->hash) {
$item['quality'] = $formula->quality;
$item['volume'] = $formula->volume;
}
return $item;
})
->sortBy('volume')
->values();
});
return $item;
})
->sortBy('volume')
->values()
->toArray();
})
->toArray();
$this->toast('Rumus berhasil diperbarui.');
@ -133,22 +141,26 @@ public function delete(FormulaModel $formula): void
$formula->delete();
// update array formulas
// Convert to collection for manipulation
$size = $formula->size;
$formulas = collect($this->formulas);
if ($this->formulas->has($size)) {
$group = collect($this->formulas->get($size))
if ($formulas->has($size)) {
$group = collect($formulas->get($size))
->reject(fn ($item) => $item['hash'] === $formula->hash)
->values()
->all();
->toArray();
if (empty($group)) {
$this->formulas->forget($size);
$formulas->forget($size);
} else {
$this->formulas->put($size, $group);
$formulas->put($size, $group);
}
}
// Convert back to array
$this->formulas = $formulas->toArray();
$this->toast('Rumus berhasil dihapus.');
Flux::modals()->close();

View File

@ -22,7 +22,7 @@
</div>
<div class="mt-6">
@if ($formulas->isNotEmpty())
@if (!empty($formulas))
<div class="mt-6 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 items-start">
@foreach ($formulas as $key => $formula)
<flux:card class="space-y-2">