diff --git a/app/Http/Livewire/Form/CardForm.php b/app/Http/Livewire/Form/CardForm.php index 535cbcd..186db8d 100644 --- a/app/Http/Livewire/Form/CardForm.php +++ b/app/Http/Livewire/Form/CardForm.php @@ -7,19 +7,8 @@ use App\Models\Character; use App\Models\Raid; use App\Models\Wow; -use App\Http\Livewire\Traits\Alert; - -use Livewire\Component; - -class CardForm extends Component +class CardForm extends ModelForm { - use Alert; - - /** - * The card record - */ - public $card; - /** * Array of available characters */ @@ -31,13 +20,13 @@ class CardForm extends Component public $raids; /** - * True if the record already exists in the database. + * Redirect after this route after record was created. */ - public bool $exist; + public string $redirect_route = 'admin.card.index'; public function mount(Card $card) { - $this->card = $card; + $this->record = $card; $this->characters = Character::all()->pluck('name', 'id'); $this->raids = Raid::all()->pluck('name', 'id'); $this->classes = Wow::$classes; @@ -50,17 +39,17 @@ class CardForm extends Component protected function rules() { return [ - 'card.body' => 'required|string|min:3|max:200', - 'card.character_id' => 'exists:' . Character::class . ',id|nullable', - 'card.raid_id' => 'exists:' . Raid::class . ',id|nullable', - 'card.class' => 'in:' . collect($this->classes)->keys() . '|nullable', + 'record.body' => 'required|string|min:3|max:200', + 'record.character_id' => 'exists:' . Character::class . ',id|nullable', + 'record.raid_id' => 'exists:' . Raid::class . ',id|nullable', + 'record.class' => 'in:' . collect($this->classes)->keys() . '|nullable', ]; } public function updated($property, $value) { // Hack to force empty value to null. - if (in_array($property, ['card.character_id', 'card.raid_id', 'card.class'])) { + if (in_array($property, ['record.character_id', 'record.raid_id', 'record.class'])) { if (empty($value)) { $this->{$property} = null; } @@ -68,37 +57,4 @@ class CardForm extends Component $this->validateOnly($property); } - - /** - * Returns true if this card has not been stored in the database. - */ - public function isNew() : bool - { - return !$this->exist; - } - - /** - * Submit the form, create/update card. - */ - public function submit() - { - $this->validate(); - $this->card->save(); - - if ($this->isNew()) { - session()->flash('info', 'Card was successfully created.'); - return redirect()->route('admin.card.index'); - } - - $this->info('Card was successfully updated.'); - } - - /** - * Render the setup page - */ - public function render() - { - return view('form.card') - ->layout('layouts.admin'); - } } diff --git a/app/Http/Livewire/Form/CharacterForm.php b/app/Http/Livewire/Form/CharacterForm.php index 136efa0..8358107 100644 --- a/app/Http/Livewire/Form/CharacterForm.php +++ b/app/Http/Livewire/Form/CharacterForm.php @@ -2,32 +2,18 @@ namespace App\Http\Livewire\Form; -use App\Models\Card; use App\Models\Character; -use App\Models\Raid; -use App\Models\Wow; -use App\Http\Livewire\Traits\Alert; - -use Livewire\Component; - -class CharacterForm extends Component +class CharacterForm extends ModelForm { - use Alert; - /** - * The character record + * Redirect after this route after record was created. */ - public $character; - - /** - * True if the record already exists in the database. - */ - public bool $exist; + public string $redirect_route = 'admin.character.index'; public function mount(Character $character) { - $this->character = $character; + $this->record = $character; $this->exist = $character->exists; } @@ -37,45 +23,7 @@ class CharacterForm extends Component protected function rules() { return [ - 'character.name' => 'required|string|min:3|max:14', + 'record.name' => 'required|string|min:3|max:14', ]; } - - public function updated($property, $value) - { - $this->validateOnly($property); - } - - /** - * Returns true if this card has not been stored in the database. - */ - public function isNew() : bool - { - return !$this->exist; - } - - /** - * Submit the form, create/update card. - */ - public function submit() - { - $this->validate(); - $this->character->save(); - - if ($this->isNew()) { - session()->flash('info', 'Character was successfully created.'); - return redirect()->route('admin.character.index'); - } - - $this->info('Character was successfully updated.'); - } - - /** - * Render the setup page - */ - public function render() - { - return view('form.character') - ->layout('layouts.admin'); - } } diff --git a/app/Http/Livewire/Form/RaidForm.php b/app/Http/Livewire/Form/RaidForm.php index 92e387b..831e2dc 100644 --- a/app/Http/Livewire/Form/RaidForm.php +++ b/app/Http/Livewire/Form/RaidForm.php @@ -4,27 +4,16 @@ namespace App\Http\Livewire\Form; use App\Models\Raid; -use App\Http\Livewire\Traits\Alert; - -use Livewire\Component; - -class RaidForm extends Component +class RaidForm extends ModelForm { - use Alert; - /** - * The raid record + * Redirect after this route after record was created. */ - public $raid; - - /** - * True if the record already exists in the database. - */ - public bool $exist; + public string $redirect_route = 'admin.raid.index'; public function mount(Raid $raid) { - $this->raid = $raid; + $this->record = $raid; $this->exist = $raid->exists; } @@ -34,45 +23,7 @@ class RaidForm extends Component protected function rules() { return [ - 'raid.name' => 'required|string|min:2|max:20', + 'record.name' => 'required|string|min:2|max:20', ]; } - - public function updated($property, $value) - { - $this->validateOnly($property); - } - - /** - * Returns true if this card has not been stored in the database. - */ - public function isNew() : bool - { - return !$this->exist; - } - - /** - * Submit the form, create/update card. - */ - public function submit() - { - $this->validate(); - $this->raid->save(); - - if ($this->isNew()) { - session()->flash('info', 'Raid was successfully created.'); - return redirect()->route('admin.character.index'); - } - - $this->info('Raid was successfully updated.'); - } - - /** - * Render the setup page - */ - public function render() - { - return view('form.raid') - ->layout('layouts.admin'); - } } diff --git a/resources/views/form/card.blade.php b/resources/views/form/card.blade.php index f1ade82..c53df83 100644 --- a/resources/views/form/card.blade.php +++ b/resources/views/form/card.blade.php @@ -6,22 +6,22 @@