parfum/app/Livewire/Studio/Loyalty/Tier/Reward.php
2025-11-12 19:33:48 +07:00

101 lines
2.5 KiB
PHP

<?php
namespace App\Livewire\Studio\Loyalty\Tier;
use App\Livewire\Forms\Studio\Loyalty\Tier\RewardForm;
use App\Livewire\Studio\Loyalty\Tier;
use App\Models\TierReward;
use App\Traits\WithAuthorization;
use App\Traits\WithCloseModal;
use App\Traits\WithConfirmation;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
use Flux\Flux;
use Livewire\Attributes\On;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Hadiah')]
class Reward extends Component
{
use WithAuthorization, WithCloseModal, WithConfirmation, WithToast, WithUpdatedData;
public RewardForm $form;
public string $modalTitle = '';
public string $method = '';
public array $rewards = [];
#[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)
->get()
->map(fn (TierReward $reward) => [
'hash' => $reward->hash,
'tier_id' => $reward->tier_id,
'name' => $reward->name,
'value' => currency($reward->value, 'Rp'),
])
->toArray();
if ($id) {
$this->form->setRewards(TierReward::byHashOrFail($id));
}
}
public function create()
{
$this->canOrAbort('create tier reward');
$this->form->store();
$this->dispatch('data:tiersUpdated')->to(Tier::class);
$this->toast('Hadiah berhasil ditambahkan.');
Flux::modal('reward-form-modal')->close();
}
public function update()
{
$this->canOrAbort('update tier reward');
$this->form->update();
$this->toast('Hadiah berhasil diperbarui.');
Flux::modal('reward-form-modal')->close();
}
public function delete(TierReward $reward)
{
$reward->delete();
$this->toast('Hadiah berhasil dihapus.');
$this->dispatch('data:tiersUpdated')->to(Tier::class);
Flux::modal('delete')->close();
$this->rewards = array_values(array_filter($this->rewards, fn ($item) => $item['hash'] !== $reward->hash));
}
public function render()
{
return view('livewire.studio.loyalty.tier.reward', [
'pageTitle' => 'Hadiah',
]);
}
}