refactor(formula): Enhance formula data management by switching to Collection for formulas and adding volume validation rule.
This commit is contained in:
parent
92b069eef5
commit
a097f7ee15
@ -22,7 +22,7 @@ public function rules(): array
|
||||
return [
|
||||
'quality' => ['required', 'string', 'max:20'],
|
||||
'size' => ['required', 'integer', 'min:1'],
|
||||
'volume' => ['required', new UnsignedInteger],
|
||||
'volume' => ['required', new UnsignedInteger, 'lte:size'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
use App\Traits\WithUpdatedData;
|
||||
use Flux\Flux;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Collection;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
@ -24,7 +25,7 @@ class Formula extends Component
|
||||
|
||||
public FormulaForm $form;
|
||||
|
||||
public array $formulas = [];
|
||||
public Collection $formulas;
|
||||
|
||||
public string $method = 'create';
|
||||
|
||||
@ -49,8 +50,7 @@ public function mount(): void
|
||||
->sortBy('volume')
|
||||
->values()
|
||||
->all()
|
||||
)
|
||||
->all();
|
||||
);
|
||||
|
||||
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
||||
}
|
||||
@ -67,22 +67,24 @@ public function create(): void
|
||||
|
||||
$formula = $this->form->store();
|
||||
|
||||
// push new formula to collection
|
||||
$size = $formula->size;
|
||||
|
||||
$newFormula = [
|
||||
'hash' => $formula->hash,
|
||||
'quality' => $formula->quality,
|
||||
'volume' => $formula->volume,
|
||||
];
|
||||
|
||||
if (! isset($this->formulas[$size])) {
|
||||
$this->formulas[$size] = [];
|
||||
}
|
||||
// Get the group for this size, or an empty Collection
|
||||
$group = $this->formulas->get($size, collect());
|
||||
|
||||
$this->formulas[$size][] = $newFormula;
|
||||
// Add, then re-sort
|
||||
$group = $group->push($newFormula)
|
||||
->sortBy('volume')
|
||||
->values();
|
||||
|
||||
// Sort by volume
|
||||
usort($this->formulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||
// Put it back
|
||||
$this->formulas->put($size, $group);
|
||||
|
||||
$this->toast('Rumus berhasil ditambahkan.');
|
||||
|
||||
@ -107,18 +109,19 @@ public function update(): void
|
||||
$formula = $this->form->update();
|
||||
|
||||
// update array formulas
|
||||
foreach ($this->formulas as &$group) {
|
||||
foreach ($group as &$item) {
|
||||
if ($item['hash'] === $formula->hash) {
|
||||
$item['quality'] = $formula->quality;
|
||||
$item['volume'] = $formula->volume;
|
||||
// Hash doesn't change
|
||||
}
|
||||
}
|
||||
// re-sort
|
||||
usort($group, fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||
}
|
||||
unset($group, $item); // Break references
|
||||
$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;
|
||||
}
|
||||
|
||||
return $item;
|
||||
})
|
||||
->sortBy('volume')
|
||||
->values();
|
||||
});
|
||||
|
||||
$this->toast('Rumus berhasil diperbarui.');
|
||||
|
||||
@ -134,11 +137,16 @@ public function delete(FormulaModel $formula): void
|
||||
// update array formulas
|
||||
$size = $formula->size;
|
||||
|
||||
if (isset($this->formulas[$size])) {
|
||||
$this->formulas[$size] = array_values(array_filter($this->formulas[$size], fn ($item) => $item['hash'] !== $formula->hash));
|
||||
if ($this->formulas->has($size)) {
|
||||
$group = collect($this->formulas->get($size))
|
||||
->reject(fn ($item) => $item['hash'] === $formula->hash)
|
||||
->values()
|
||||
->all();
|
||||
|
||||
if (empty($this->formulas[$size])) {
|
||||
unset($this->formulas[$size]);
|
||||
if (empty($group)) {
|
||||
$this->formulas->forget($size);
|
||||
} else {
|
||||
$this->formulas->put($size, $group);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user