refactor: Add return type hints, implement authorization checks, and optimize formula data management across Livewire components.
This commit is contained in:
parent
99ca332f1c
commit
acc107bbfb
@ -13,7 +13,6 @@
|
||||
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;
|
||||
@ -25,7 +24,7 @@ class Formula extends Component
|
||||
|
||||
public FormulaForm $form;
|
||||
|
||||
public Collection $formulas;
|
||||
public array $formulas = [];
|
||||
|
||||
public string $method = 'create';
|
||||
|
||||
@ -40,33 +39,22 @@ public function mount(): void
|
||||
$this->formulas = FormulaModel::orderBy('size')
|
||||
->get()
|
||||
->groupBy('size')
|
||||
->map(function ($items) {
|
||||
return $items
|
||||
->map(
|
||||
fn ($items) => $items
|
||||
->map(fn ($item) => [
|
||||
'hash' => $item->hash,
|
||||
'quality' => $item->quality,
|
||||
'volume' => $item->volume,
|
||||
])
|
||||
->sortBy('volume');
|
||||
});
|
||||
->sortBy('volume')
|
||||
->values()
|
||||
->all()
|
||||
)
|
||||
->all();
|
||||
|
||||
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
||||
}
|
||||
|
||||
#[On('modal:open')]
|
||||
public function openModal(string $method, string $modalTitle, ?string $id = null): void
|
||||
{
|
||||
$this->resetValidation();
|
||||
$this->resetErrorBag();
|
||||
|
||||
$this->method = $method;
|
||||
$this->modalTitle = $modalTitle;
|
||||
|
||||
if ($id) {
|
||||
$this->form->setFormula(FormulaModel::byHashOrFail($id));
|
||||
}
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
$this->canOrAbort('create formula');
|
||||
@ -87,26 +75,14 @@ public function create(): void
|
||||
'volume' => $formula->volume,
|
||||
];
|
||||
|
||||
// push new formula to collection
|
||||
$size = $formula->size;
|
||||
$newFormula = [
|
||||
'hash' => $formula->hash,
|
||||
'quality' => $formula->quality,
|
||||
'volume' => $formula->volume,
|
||||
];
|
||||
|
||||
// Recreate the collection instead of modifying in place
|
||||
$newFormulas = $this->formulas->toArray();
|
||||
|
||||
if (! isset($newFormulas[$size])) {
|
||||
$newFormulas[$size] = [];
|
||||
if (! isset($this->formulas[$size])) {
|
||||
$this->formulas[$size] = [];
|
||||
}
|
||||
|
||||
$newFormulas[$size][] = $newFormula;
|
||||
// Sort by volume
|
||||
usort($newFormulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||
$this->formulas[$size][] = $newFormula;
|
||||
|
||||
$this->formulas = collect($newFormulas);
|
||||
// Sort by volume
|
||||
usort($this->formulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||
|
||||
$this->toast('Rumus berhasil ditambahkan.');
|
||||
|
||||
@ -130,26 +106,19 @@ public function update(): void
|
||||
|
||||
$formula = $this->form->update();
|
||||
|
||||
// update collection formulas
|
||||
$newFormulas = $this->formulas->toArray();
|
||||
|
||||
foreach ($newFormulas as $size => &$items) {
|
||||
foreach ($items as &$item) {
|
||||
// update array formulas
|
||||
foreach ($this->formulas as &$group) {
|
||||
foreach ($group as &$item) {
|
||||
if ($item['hash'] === $formula->hash) {
|
||||
$item = [
|
||||
'hash' => $formula->hash,
|
||||
'quality' => $formula->quality,
|
||||
'volume' => $formula->volume,
|
||||
];
|
||||
break;
|
||||
$item['quality'] = $formula->quality;
|
||||
$item['volume'] = $formula->volume;
|
||||
// Hash doesn't change
|
||||
}
|
||||
}
|
||||
// Sort by volume
|
||||
usort($items, fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||
// re-sort
|
||||
usort($group, fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||
}
|
||||
unset($items);
|
||||
|
||||
$this->formulas = collect($newFormulas);
|
||||
unset($group, $item); // Break references
|
||||
|
||||
$this->toast('Rumus berhasil diperbarui.');
|
||||
|
||||
@ -158,27 +127,40 @@ public function update(): void
|
||||
|
||||
public function delete(FormulaModel $formula): void
|
||||
{
|
||||
$this->canOrAbort('delete formula');
|
||||
|
||||
$formula->delete();
|
||||
|
||||
// update collection formulas
|
||||
// update array formulas
|
||||
$size = $formula->size;
|
||||
$newFormulas = $this->formulas->toArray();
|
||||
|
||||
if (isset($newFormulas[$size])) {
|
||||
$newFormulas[$size] = array_values(array_filter($newFormulas[$size], fn ($item) => $item['hash'] !== $formula->hash));
|
||||
if (isset($this->formulas[$size])) {
|
||||
$this->formulas[$size] = array_values(array_filter($this->formulas[$size], fn ($item) => $item['hash'] !== $formula->hash));
|
||||
|
||||
if (empty($newFormulas[$size])) {
|
||||
unset($newFormulas[$size]);
|
||||
if (empty($this->formulas[$size])) {
|
||||
unset($this->formulas[$size]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->formulas = collect($newFormulas);
|
||||
|
||||
$this->toast('Rumus berhasil dihapus.');
|
||||
|
||||
Flux::modals()->close();
|
||||
}
|
||||
|
||||
#[On('modal:open')]
|
||||
public function openModal(string $method, string $modalTitle, ?string $id = null): void
|
||||
{
|
||||
$this->resetValidation();
|
||||
$this->resetErrorBag();
|
||||
|
||||
$this->method = $method;
|
||||
$this->modalTitle = $modalTitle;
|
||||
|
||||
if ($id) {
|
||||
$this->form->setFormula(FormulaModel::byHashOrFail($id));
|
||||
}
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.studio.master.formulas', [
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user