canOrAbort('create tier reward'); $reward = $this->form->store(); $this->rewards = collect($this->rewards) ->push( $reward->only(['tier_id', 'name', 'value']) + ['hash' => $reward->hash] ) ->toArray(); $this->dispatch('data:tiersUpdated')->to(Tier::class); $this->toast('Hadiah berhasil ditambahkan.'); Flux::modal('reward-form-modal')->close(); } public function update(): void { $this->canOrAbort('update tier reward'); $reward = $this->form->update(); $updatedReward = $reward->only(['tier_id', 'name', 'value']) + [ 'hash' => $reward->hash, ]; $this->rewards = collect($this->rewards) ->map( fn ($item) => $item['hash'] === $updatedReward['hash'] ? $updatedReward : $item ) ->toArray(); $this->toast('Hadiah berhasil diperbarui.'); Flux::modal('reward-form-modal')->close(); } public function delete(TierReward $reward): void { $this->canOrAbort('delete tier reward'); $reward->delete(); $this->rewards = collect($this->rewards) ->reject(fn ($item) => $item['hash'] === $reward->hash) ->values() ->toArray(); $this->toast('Hadiah berhasil dihapus.'); $this->dispatch('data:tiersUpdated')->to(Tier::class); Flux::modal('delete-reward-confirmation')->close(); } #[On('modal:open:reward')] public function openModal(string $method, string $modalTitle, ?string $tier_id = null, ?string $id = null) { $this->resetValidation(); $this->resetErrorBag(); $this->method = $method; $this->modalTitle = $modalTitle; $this->form->tier_id = $tier_id; $this->rewards = TierReward::where('tier_id', $tier_id) ->latest() ->get() ->append('hash') ->toArray(); $id && $this->form->setRewards( TierReward::byHashOrFail($id) ); } public function render(): View { return view('livewire.studio.loyalty.tier.reward', [ 'pageTitle' => 'Hadiah', ]); } }