diff --git a/app/Livewire/Datatable/Studio/Master/FormulasTable.php b/app/Livewire/Datatable/Studio/Master/FormulasTable.php deleted file mode 100644 index d0257e8..0000000 --- a/app/Livewire/Datatable/Studio/Master/FormulasTable.php +++ /dev/null @@ -1,63 +0,0 @@ -searchable(), - - Column::make('Ukuran', 'size') - ->format(fn ($row, $column) => $row.' ml') - ->searchable() - ->sortable(), - - Column::make('Volume', 'volume') - ->format(fn ($row, $column) => $row.' ml') - ->searchable() - ->sortable(), - - Column::make('Aksi') - ->label(function ($row) { - $actions = ''; - - if (auth()->user()->can('update formula')) { - $actions .= view('components.datatables.edit-modal', [ - 'id' => $row->hash, - 'method' => 'update', - 'modalTitle' => 'Ubah Rumus', - ])->render(); - } - - if (auth()->user()->can('delete formula')) { - $actions .= view('components.datatables.delete', [ - 'id' => $row->hash, - 'deleteRoute' => route('studio.master.formula.delete', $row->hash), - ])->render(); - } - - return $actions; - }) - ->html(), - ]; - } - - public function builder(): Builder - { - return Formula::select('id', 'quality', 'size', 'volume'); - } -} diff --git a/app/Livewire/Forms/Studio/Master/FormulaForm.php b/app/Livewire/Forms/Studio/Master/FormulaForm.php index 4a270ed..de1f45b 100644 --- a/app/Livewire/Forms/Studio/Master/FormulaForm.php +++ b/app/Livewire/Forms/Studio/Master/FormulaForm.php @@ -20,8 +20,8 @@ public function rules(): array { return [ 'quality' => ['required', 'string', 'max:20'], - 'size' => ['required', 'numeric', new UnsignedInteger], - 'volume' => ['required', 'numeric', new UnsignedInteger], + 'size' => ['required', new UnsignedInteger], + 'volume' => ['required', new UnsignedInteger], ]; } @@ -39,14 +39,14 @@ public function setFormula(Formula $formula) $this->quality = $formula->quality; $this->size = $formula->size; - $this->volume = $formula->volume; + $this->volume = replaceCurrency($formula->volume); } public function store() { $this->validate(); - Formula::create($this->prepareSavedData()); + return Formula::create($this->prepareSavedData()); } public function update() @@ -54,6 +54,8 @@ public function update() $this->validate(); $this->formula->update($this->prepareSavedData()); + + return $this->formula; } private function prepareSavedData() @@ -61,7 +63,7 @@ private function prepareSavedData() return [ 'quality' => $this->quality, 'size' => $this->size, - 'volume' => $this->volume, + 'volume' => replaceCurrency($this->volume), ]; } } diff --git a/app/Livewire/Studio/Master/Formula.php b/app/Livewire/Studio/Master/Formula.php index 6fd91e9..bc5dd15 100644 --- a/app/Livewire/Studio/Master/Formula.php +++ b/app/Livewire/Studio/Master/Formula.php @@ -22,6 +22,8 @@ class Formula extends Component public FormulaForm $form; + public array $formulas = []; + public string $method = 'create'; public string $modalTitle = ''; @@ -32,6 +34,16 @@ class Formula extends Component public function mount() { + $this->formulas = FormulaModel::orderBy('size')->get() + ->groupBy('size') + ->map(function ($items) { + return $items->map(fn ($item) => [ + 'hash' => $item->hash, + 'quality' => $item->quality, + 'volume' => $item->volume, + ]); + })->toArray(); + $this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray(); } @@ -53,9 +65,16 @@ public function create() { $this->canOrAbort('create formula'); - $this->form->store(); + $formula = $this->form->store(); - $this->dispatch('refreshDatatable'); + // push new formula to array + $size = $formula->size; + $this->formulas[$size] ??= []; + $this->formulas[$size][] = [ + 'hash' => $formula->hash, + 'quality' => $formula->quality, + 'volume' => $formula->volume, + ]; $this->toast('Rumus berhasil ditambahkan.'); @@ -66,9 +85,21 @@ public function update() { $this->canOrAbort('update formula'); - $this->form->update(); + $formula = $this->form->update(); - $this->dispatch('refreshDatatable'); + // update array formulas + foreach ($this->formulas as $key => &$items) { + $index = array_search($formula->hash, array_column($items, 'hash')); + if ($index !== false) { + $this->formulas[$key][$index] = [ + 'hash' => $formula->hash, + 'quality' => $formula->quality, + 'volume' => $formula->volume, + ]; + break; + } + } + unset($items); $this->toast('Rumus berhasil diperbarui.'); @@ -79,7 +110,18 @@ public function delete(FormulaModel $formula) { $formula->delete(); - $this->dispatch('refreshDatatable'); + // 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 (empty($this->formulas[$size])) { + unset($this->formulas[$size]); + } + } $this->toast('Rumus berhasil dihapus.'); diff --git a/resources/views/livewire/studio/master/formulas.blade.php b/resources/views/livewire/studio/master/formulas.blade.php index d603dd0..097bdbb 100644 --- a/resources/views/livewire/studio/master/formulas.blade.php +++ b/resources/views/livewire/studio/master/formulas.blade.php @@ -15,7 +15,39 @@
- +
+ @foreach ($formulas as $key => $formula) + + Ukuran {{ $key }} ml + @foreach ($formula as $item) + +
+ {{ $item['quality'] }} + ({{ $item['volume'] }} ml) +
+ + + + + + + + + + + + + + + +
+ @endforeach +
+ @endforeach +
@include('components.confirmation.delete') @@ -49,8 +81,11 @@ Volume * - + + + ml +