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 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;
|
||||||
@ -25,7 +24,7 @@ class Formula extends Component
|
|||||||
|
|
||||||
public FormulaForm $form;
|
public FormulaForm $form;
|
||||||
|
|
||||||
public Collection $formulas;
|
public array $formulas = [];
|
||||||
|
|
||||||
public string $method = 'create';
|
public string $method = 'create';
|
||||||
|
|
||||||
@ -40,33 +39,22 @@ public function mount(): void
|
|||||||
$this->formulas = FormulaModel::orderBy('size')
|
$this->formulas = FormulaModel::orderBy('size')
|
||||||
->get()
|
->get()
|
||||||
->groupBy('size')
|
->groupBy('size')
|
||||||
->map(function ($items) {
|
->map(
|
||||||
return $items
|
fn ($items) => $items
|
||||||
->map(fn ($item) => [
|
->map(fn ($item) => [
|
||||||
'hash' => $item->hash,
|
'hash' => $item->hash,
|
||||||
'quality' => $item->quality,
|
'quality' => $item->quality,
|
||||||
'volume' => $item->volume,
|
'volume' => $item->volume,
|
||||||
])
|
])
|
||||||
->sortBy('volume');
|
->sortBy('volume')
|
||||||
});
|
->values()
|
||||||
|
->all()
|
||||||
|
)
|
||||||
|
->all();
|
||||||
|
|
||||||
$this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
|
$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
|
public function create(): void
|
||||||
{
|
{
|
||||||
$this->canOrAbort('create formula');
|
$this->canOrAbort('create formula');
|
||||||
@ -87,26 +75,14 @@ public function create(): void
|
|||||||
'volume' => $formula->volume,
|
'volume' => $formula->volume,
|
||||||
];
|
];
|
||||||
|
|
||||||
// push new formula to collection
|
if (! isset($this->formulas[$size])) {
|
||||||
$size = $formula->size;
|
$this->formulas[$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] = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$newFormulas[$size][] = $newFormula;
|
$this->formulas[$size][] = $newFormula;
|
||||||
// Sort by volume
|
|
||||||
usort($newFormulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
|
||||||
|
|
||||||
$this->formulas = collect($newFormulas);
|
// Sort by volume
|
||||||
|
usort($this->formulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||||
|
|
||||||
$this->toast('Rumus berhasil ditambahkan.');
|
$this->toast('Rumus berhasil ditambahkan.');
|
||||||
|
|
||||||
@ -130,26 +106,19 @@ public function update(): void
|
|||||||
|
|
||||||
$formula = $this->form->update();
|
$formula = $this->form->update();
|
||||||
|
|
||||||
// update collection formulas
|
// update array formulas
|
||||||
$newFormulas = $this->formulas->toArray();
|
foreach ($this->formulas as &$group) {
|
||||||
|
foreach ($group as &$item) {
|
||||||
foreach ($newFormulas as $size => &$items) {
|
|
||||||
foreach ($items as &$item) {
|
|
||||||
if ($item['hash'] === $formula->hash) {
|
if ($item['hash'] === $formula->hash) {
|
||||||
$item = [
|
$item['quality'] = $formula->quality;
|
||||||
'hash' => $formula->hash,
|
$item['volume'] = $formula->volume;
|
||||||
'quality' => $formula->quality,
|
// Hash doesn't change
|
||||||
'volume' => $formula->volume,
|
|
||||||
];
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Sort by volume
|
// re-sort
|
||||||
usort($items, fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
usort($group, fn ($a, $b) => $a['volume'] <=> $b['volume']);
|
||||||
}
|
}
|
||||||
unset($items);
|
unset($group, $item); // Break references
|
||||||
|
|
||||||
$this->formulas = collect($newFormulas);
|
|
||||||
|
|
||||||
$this->toast('Rumus berhasil diperbarui.');
|
$this->toast('Rumus berhasil diperbarui.');
|
||||||
|
|
||||||
@ -158,27 +127,40 @@ public function update(): void
|
|||||||
|
|
||||||
public function delete(FormulaModel $formula): void
|
public function delete(FormulaModel $formula): void
|
||||||
{
|
{
|
||||||
|
$this->canOrAbort('delete formula');
|
||||||
|
|
||||||
$formula->delete();
|
$formula->delete();
|
||||||
|
|
||||||
// update collection formulas
|
// update array formulas
|
||||||
$size = $formula->size;
|
$size = $formula->size;
|
||||||
$newFormulas = $this->formulas->toArray();
|
|
||||||
|
|
||||||
if (isset($newFormulas[$size])) {
|
if (isset($this->formulas[$size])) {
|
||||||
$newFormulas[$size] = array_values(array_filter($newFormulas[$size], fn ($item) => $item['hash'] !== $formula->hash));
|
$this->formulas[$size] = array_values(array_filter($this->formulas[$size], fn ($item) => $item['hash'] !== $formula->hash));
|
||||||
|
|
||||||
if (empty($newFormulas[$size])) {
|
if (empty($this->formulas[$size])) {
|
||||||
unset($newFormulas[$size]);
|
unset($this->formulas[$size]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->formulas = collect($newFormulas);
|
|
||||||
|
|
||||||
$this->toast('Rumus berhasil dihapus.');
|
$this->toast('Rumus berhasil dihapus.');
|
||||||
|
|
||||||
Flux::modals()->close();
|
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
|
public function render(): View
|
||||||
{
|
{
|
||||||
return view('livewire.studio.master.formulas', [
|
return view('livewire.studio.master.formulas', [
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user