refactor(formula): Enhance formula data management by switching to Collection for formulas and adding volume validation rule.

This commit is contained in:
Yoga Pangestu 2025-12-11 22:53:27 +07:00
parent 92b069eef5
commit a097f7ee15
2 changed files with 35 additions and 27 deletions

View File

@ -22,7 +22,7 @@ public function rules(): array
return [ return [
'quality' => ['required', 'string', 'max:20'], 'quality' => ['required', 'string', 'max:20'],
'size' => ['required', 'integer', 'min:1'], 'size' => ['required', 'integer', 'min:1'],
'volume' => ['required', new UnsignedInteger], 'volume' => ['required', new UnsignedInteger, 'lte:size'],
]; ];
} }

View File

@ -13,6 +13,7 @@
use App\Traits\WithUpdatedData; use App\Traits\WithUpdatedData;
use Flux\Flux; use Flux\Flux;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Livewire\Attributes\On; use Livewire\Attributes\On;
use Livewire\Attributes\Title; use Livewire\Attributes\Title;
use Livewire\Component; use Livewire\Component;
@ -24,7 +25,7 @@ class Formula extends Component
public FormulaForm $form; public FormulaForm $form;
public array $formulas = []; public Collection $formulas;
public string $method = 'create'; public string $method = 'create';
@ -49,8 +50,7 @@ public function mount(): void
->sortBy('volume') ->sortBy('volume')
->values() ->values()
->all() ->all()
) );
->all();
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray(); $this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
} }
@ -67,22 +67,24 @@ public function create(): void
$formula = $this->form->store(); $formula = $this->form->store();
// push new formula to collection
$size = $formula->size; $size = $formula->size;
$newFormula = [ $newFormula = [
'hash' => $formula->hash, 'hash' => $formula->hash,
'quality' => $formula->quality, 'quality' => $formula->quality,
'volume' => $formula->volume, 'volume' => $formula->volume,
]; ];
if (! isset($this->formulas[$size])) { // Get the group for this size, or an empty Collection
$this->formulas[$size] = []; $group = $this->formulas->get($size, collect());
}
$this->formulas[$size][] = $newFormula; // Add, then re-sort
$group = $group->push($newFormula)
->sortBy('volume')
->values();
// Sort by volume // Put it back
usort($this->formulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']); $this->formulas->put($size, $group);
$this->toast('Rumus berhasil ditambahkan.'); $this->toast('Rumus berhasil ditambahkan.');
@ -107,18 +109,19 @@ public function update(): void
$formula = $this->form->update(); $formula = $this->form->update();
// update array formulas // update array formulas
foreach ($this->formulas as &$group) { $this->formulas = $this->formulas->map(function ($group) use ($formula) {
foreach ($group as &$item) { return collect($group)
if ($item['hash'] === $formula->hash) { ->map(function ($item) use ($formula) {
$item['quality'] = $formula->quality; if ($item['hash'] === $formula->hash) {
$item['volume'] = $formula->volume; $item['quality'] = $formula->quality;
// Hash doesn't change $item['volume'] = $formula->volume;
} }
}
// re-sort return $item;
usort($group, fn ($a, $b) => $a['volume'] <=> $b['volume']); })
} ->sortBy('volume')
unset($group, $item); // Break references ->values();
});
$this->toast('Rumus berhasil diperbarui.'); $this->toast('Rumus berhasil diperbarui.');
@ -134,11 +137,16 @@ public function delete(FormulaModel $formula): void
// update array formulas // update array formulas
$size = $formula->size; $size = $formula->size;
if (isset($this->formulas[$size])) { if ($this->formulas->has($size)) {
$this->formulas[$size] = array_values(array_filter($this->formulas[$size], fn ($item) => $item['hash'] !== $formula->hash)); $group = collect($this->formulas->get($size))
->reject(fn ($item) => $item['hash'] === $formula->hash)
->values()
->all();
if (empty($this->formulas[$size])) { if (empty($group)) {
unset($this->formulas[$size]); $this->formulas->forget($size);
} else {
$this->formulas->put($size, $group);
} }
} }