122 lines
3.2 KiB
PHP
122 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Studio\Loyalty\Tier;
|
|
|
|
use App\Livewire\Forms\Studio\Loyalty\Tier\RewardForm;
|
|
use App\Livewire\Studio\Loyalty\Tier\Index as Tier;
|
|
use App\Models\TierReward;
|
|
use App\Traits\Authorization\WithAuthorization;
|
|
use App\Traits\Components\WithCloseModal;
|
|
use App\Traits\Components\WithConfirmation;
|
|
use App\Traits\Components\WithToast;
|
|
use App\Traits\Notification\WithSubscribeNotification;
|
|
use App\Traits\Utilities\WithUpdatedData;
|
|
use Flux\Flux;
|
|
use Illuminate\Contracts\View\View;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
|
|
#[Title('Hadiah')]
|
|
class Reward extends Component
|
|
{
|
|
use WithAuthorization, WithCloseModal, WithConfirmation, WithSubscribeNotification, WithToast, WithUpdatedData;
|
|
|
|
public RewardForm $form;
|
|
|
|
public string $modalTitle = '';
|
|
|
|
public string $method = '';
|
|
|
|
public array $rewards = [];
|
|
|
|
public function create(): void
|
|
{
|
|
$this->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',
|
|
]);
|
|
}
|
|
}
|