refactor: Consistently handle formula data as a PHP array in Livewire component and view.
This commit is contained in:
parent
9cf985e8f9
commit
4aad6aa730
@ -24,7 +24,7 @@ class Formula extends Component
|
|||||||
|
|
||||||
public FormulaForm $form;
|
public FormulaForm $form;
|
||||||
|
|
||||||
public $formulas;
|
public array $formulas = [];
|
||||||
|
|
||||||
public string $method = 'create';
|
public string $method = 'create';
|
||||||
|
|
||||||
@ -48,8 +48,9 @@ public function mount(): void
|
|||||||
])
|
])
|
||||||
->sortBy('volume')
|
->sortBy('volume')
|
||||||
->values()
|
->values()
|
||||||
->all()
|
->toArray()
|
||||||
);
|
)
|
||||||
|
->toArray();
|
||||||
|
|
||||||
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
||||||
}
|
}
|
||||||
@ -74,16 +75,20 @@ public function create(): void
|
|||||||
'volume' => $formula->volume,
|
'volume' => $formula->volume,
|
||||||
];
|
];
|
||||||
|
|
||||||
// Get the group for this size, or an empty Collection
|
// Convert to collection for manipulation
|
||||||
$group = $this->formulas->get($size, collect());
|
$formulas = collect($this->formulas);
|
||||||
|
|
||||||
|
// Get the group for this size, or an empty collection
|
||||||
|
$group = collect($formulas->get($size, []));
|
||||||
|
|
||||||
// Add, then re-sort
|
// Add, then re-sort
|
||||||
$group = $group->push($newFormula)
|
$group = $group->push($newFormula)
|
||||||
->sortBy('volume')
|
->sortBy('volume')
|
||||||
->values();
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
// Put it back
|
// Put it back and convert to array
|
||||||
$this->formulas->put($size, $group);
|
$this->formulas = $formulas->put($size, $group)->toArray();
|
||||||
|
|
||||||
$this->toast('Rumus berhasil ditambahkan.');
|
$this->toast('Rumus berhasil ditambahkan.');
|
||||||
|
|
||||||
@ -107,20 +112,23 @@ public function update(): void
|
|||||||
|
|
||||||
$formula = $this->form->update();
|
$formula = $this->form->update();
|
||||||
|
|
||||||
// update array formulas
|
// Convert to collection for manipulation
|
||||||
$this->formulas = $this->formulas->map(function ($group) use ($formula) {
|
$this->formulas = collect($this->formulas)
|
||||||
return collect($group)
|
->map(function ($group) use ($formula) {
|
||||||
->map(function ($item) use ($formula) {
|
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;
|
||||||
}
|
$item['volume'] = $formula->volume;
|
||||||
|
}
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
})
|
})
|
||||||
->sortBy('volume')
|
->sortBy('volume')
|
||||||
->values();
|
->values()
|
||||||
});
|
->toArray();
|
||||||
|
})
|
||||||
|
->toArray();
|
||||||
|
|
||||||
$this->toast('Rumus berhasil diperbarui.');
|
$this->toast('Rumus berhasil diperbarui.');
|
||||||
|
|
||||||
@ -133,22 +141,26 @@ public function delete(FormulaModel $formula): void
|
|||||||
|
|
||||||
$formula->delete();
|
$formula->delete();
|
||||||
|
|
||||||
// update array formulas
|
// Convert to collection for manipulation
|
||||||
$size = $formula->size;
|
$size = $formula->size;
|
||||||
|
$formulas = collect($this->formulas);
|
||||||
|
|
||||||
if ($this->formulas->has($size)) {
|
if ($formulas->has($size)) {
|
||||||
$group = collect($this->formulas->get($size))
|
$group = collect($formulas->get($size))
|
||||||
->reject(fn ($item) => $item['hash'] === $formula->hash)
|
->reject(fn ($item) => $item['hash'] === $formula->hash)
|
||||||
->values()
|
->values()
|
||||||
->all();
|
->toArray();
|
||||||
|
|
||||||
if (empty($group)) {
|
if (empty($group)) {
|
||||||
$this->formulas->forget($size);
|
$formulas->forget($size);
|
||||||
} else {
|
} else {
|
||||||
$this->formulas->put($size, $group);
|
$formulas->put($size, $group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert back to array
|
||||||
|
$this->formulas = $formulas->toArray();
|
||||||
|
|
||||||
$this->toast('Rumus berhasil dihapus.');
|
$this->toast('Rumus berhasil dihapus.');
|
||||||
|
|
||||||
Flux::modals()->close();
|
Flux::modals()->close();
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-6">
|
<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">
|
<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)
|
@foreach ($formulas as $key => $formula)
|
||||||
<flux:card class="space-y-2">
|
<flux:card class="space-y-2">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user