diff --git a/app/Livewire/Datatable/Studio/Loyalty/TiersTable.php b/app/Livewire/Datatable/Studio/Loyalty/TiersTable.php deleted file mode 100644 index 3d99313..0000000 --- a/app/Livewire/Datatable/Studio/Loyalty/TiersTable.php +++ /dev/null @@ -1,57 +0,0 @@ -searchable(), - - Column::make('Min. Poin', 'min_points')->searchable()->sortable(), - - Column::make('Maks. Poin', 'max_points')->searchable()->sortable(), - - Column::make('Aksi') - ->label(function ($row) { - $actions = ''; - - if (auth()->user()->can('update tier')) { - $actions .= view('components.datatables.edit-modal', [ - 'id' => $row->hash, - 'method' => 'update', - 'modalTitle' => 'Ubah Tier', - ])->render(); - } - - if (auth()->user()->can('delete tier')) { - $actions .= view('components.datatables.delete', [ - 'id' => $row->hash, - 'deleteRoute' => route('studio.loyalty.tier.delete', $row->hash), - ])->render(); - } - - return $actions; - }) - ->html(), - ]; - } - - public function builder(): Builder - { - return Tier::select('id', 'name', 'min_points', 'max_points'); - } -} diff --git a/app/Livewire/Forms/Studio/Loyalty/TierForm.php b/app/Livewire/Forms/Studio/Loyalty/TierForm.php index c26fa76..2b032f1 100644 --- a/app/Livewire/Forms/Studio/Loyalty/TierForm.php +++ b/app/Livewire/Forms/Studio/Loyalty/TierForm.php @@ -3,6 +3,7 @@ namespace App\Livewire\Forms\Studio\Loyalty; use App\Models\Tier; +use App\Rules\UnsignedInteger; use Livewire\Form; class TierForm extends Form @@ -19,8 +20,8 @@ public function rules(): array { return [ 'name' => ['required', 'string', 'max:50'], - 'min_points' => ['required', 'numeric'], - 'max_points' => ['nullable', 'numeric'], + 'min_points' => ['required', new UnsignedInteger], + 'max_points' => ['nullable', new UnsignedInteger], ]; } @@ -46,13 +47,24 @@ public function store() { $this->validate(); - Tier::create($this->pull()); + return Tier::create($this->prepareSavedData()); } public function update() { $this->validate(); - $this->tier->update($this->pull()); + $this->tier->update($this->prepareSavedData()); + + return $this->tier; + } + + private function prepareSavedData() + { + return [ + 'name' => $this->name, + 'min_points' => replaceCurrency($this->min_points), + 'max_points' => replaceCurrency($this->max_points), + ]; } } diff --git a/app/Livewire/Studio/Loyalty/Tier.php b/app/Livewire/Studio/Loyalty/Tier.php index f238dd5..58c2403 100644 --- a/app/Livewire/Studio/Loyalty/Tier.php +++ b/app/Livewire/Studio/Loyalty/Tier.php @@ -19,12 +19,27 @@ class Tier extends Component { use WithAuthorization, WithCloseModal, WithConfirmation, WithToast, WithUpdatedData; + public array $tiers = []; + public TierForm $form; public string $method = 'create'; public string $modalTitle = ''; + public function mount() + { + $this->tiers = TierModel::orderBy('min_points', 'asc') + ->get() + ->map(fn($tier) => [ + 'hash' => $tier->hash, + 'name' => $tier->name, + 'min_points' => currency($tier->min_points, ''), + 'max_points' => currency($tier->max_points, ''), + ]) + ->toArray(); + } + #[On('modal:open')] public function openModal(string $method, string $modalTitle, ?string $id = null) { @@ -43,9 +58,19 @@ public function create() { $this->canOrAbort('create tier'); - $this->form->store(); + $tier = $this->form->store(); - $this->dispatch('refreshDatatable'); + $tiers = $this->tiers; + $tiers[] = [ + 'hash' => $tier->hash, + 'name' => $tier->name, + 'min_points' => currency($tier->min_points, ''), + 'max_points' => currency($tier->max_points, ''), + ]; + + usort($tiers, fn($a, $b) => replaceCurrency($a['min_points']) <=> replaceCurrency($b['min_points'])); + + $this->tiers = $tiers; $this->toast('Tier berhasil ditambahkan.'); @@ -56,9 +81,22 @@ public function update() { $this->canOrAbort('update tier'); - $this->form->update(); + $tier = $this->form->update(); - $this->dispatch('refreshDatatable'); + // update array tiers + $tiers = $this->tiers; + foreach ($tiers as $index => $item) { + if ($item['hash'] === $tier->hash) { + $tiers[$index] = [ + 'hash' => $tier->hash, + 'name' => $tier->name, + 'min_points' => currency($tier->min_points, ''), + 'max_points' => currency($tier->max_points, ''), + ]; + break; + } + } + $this->tiers = $tiers; $this->toast('Tier berhasil diperbarui.'); @@ -69,7 +107,9 @@ public function delete(TierModel $tier) { $tier->delete(); - $this->dispatch('refreshDatatable'); + // update array tiers + $tiers = array_values(array_filter($this->tiers, fn($item) => $item['hash'] !== $tier->hash)); + $this->tiers = $tiers; $this->toast('Tier berhasil dihapus.'); diff --git a/resources/views/livewire/studio/loyalty/tiers.blade.php b/resources/views/livewire/studio/loyalty/tiers.blade.php index 2493bc8..5fab82a 100644 --- a/resources/views/livewire/studio/loyalty/tiers.blade.php +++ b/resources/views/livewire/studio/loyalty/tiers.blade.php @@ -3,7 +3,7 @@