Skip to content

Commit

Permalink
Added Division CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
troccoli committed Aug 29, 2024
1 parent 2f43919 commit 0116b23
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 2 deletions.
37 changes: 37 additions & 0 deletions app/Livewire/Divisions/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Livewire\Divisions;

use App\Livewire\Forms\DivisionForm;
use App\Models\Competition;
use App\Models\Division;
use App\Models\Season;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Create extends Component
{
public DivisionForm $form;

public function mount(Division $division): void
{
$this->form->setDivisionModel($division);
}

public function save(): void
{
$this->form->store();

$this->redirectRoute('divisions.index', navigate: true);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.division.create', [
'seasons' => Season::latest('year')->get(),
'competitions' => Competition::all(),
]);
}
}
32 changes: 32 additions & 0 deletions app/Livewire/Divisions/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Livewire\Divisions;

use App\Livewire\Forms\DivisionForm;
use App\Models\Division;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Edit extends Component
{
public DivisionForm $form;

public function mount(Division $division): void
{
$this->form->setDivisionModel($division);
}

public function save(): void
{
$this->form->update();

$this->redirectRoute('divisions.index', navigate: true);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.division.edit');
}
}
32 changes: 32 additions & 0 deletions app/Livewire/Divisions/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Livewire\Divisions;

use App\Models\Division;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
use WithPagination;

#[Layout('layouts.app')]
public function render(): View
{
$divisions = Division::with(['competition', 'competition.season'])
->oldest('display_order')
->simplePaginate(10);

return view('livewire.division.index', compact('divisions'))
->with('i', $this->getPage() * $divisions->perPage());
}

public function delete(Division $division): void
{
$division->delete();

$this->redirectRoute('divisions.index', navigate: true);
}
}
25 changes: 25 additions & 0 deletions app/Livewire/Divisions/Show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Livewire\Divisions;

use App\Livewire\Forms\DivisionForm;
use App\Models\Division;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Show extends Component
{
public DivisionForm $form;

public function mount(Division $division): void
{
$this->form->setDivisionModel($division);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.division.show', ['division' => $this->form->divisionModel]);
}
}
65 changes: 65 additions & 0 deletions app/Livewire/Forms/DivisionForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Livewire\Forms;

use App\Models\Division;
use Livewire\Form;

class DivisionForm extends Form
{
public ?Division $divisionModel;

public ?string $competition_id;

public ?string $name;

public ?int $display_order;

public ?string $seasonName;

public ?string $competitionName;

public function rules(): array
{
return [
'competition_id' => 'required|uuid|exists:competitions,id',
'name' => 'required|string',
'display_order' => 'required|int|min:1',
];
}

public function messages(): array
{
return [
'competition_id.required' => 'Please choose a competition.',
'name.required' => 'The division\'s name is required.',
'display_order.required' => 'Please choose a positive number.',
];
}

public function setDivisionModel(Division $divisionModel): void
{
$this->divisionModel = $divisionModel;

$this->competition_id = $this->divisionModel->competition_id;
$this->name = $this->divisionModel->name;
$this->display_order = $this->divisionModel->display_order;
$this->seasonName = $this->divisionModel?->competition?->season?->name;
$this->competitionName = $this->divisionModel?->competition?->name;

}

public function store(): void
{
$this->divisionModel->create($this->validate());

$this->reset();
}

public function update(): void
{
$this->divisionModel->update($this->validate());

$this->reset();
}
}
2 changes: 1 addition & 1 deletion resources/views/components/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<x-responsive-nav-link :href="route('competitions.index')" :active="request()->routeIs('competitions.*')" wire:navigate>
Competitions
</x-responsive-nav-link>
<x-responsive-nav-link :href="route('divisions')" :active="request()->routeIs('divisions')" wire:navigate>
<x-responsive-nav-link :href="route('divisions.index')" :active="request()->routeIs('divisions.*')" wire:navigate>
Divisions
</x-responsive-nav-link>
<hr class="hidden md:block"/>
Expand Down
12 changes: 12 additions & 0 deletions resources/views/livewire/division/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-crud.header>Divisions</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="divisions.index">Create a new division</x-crud.subheader>

<x-crud.content class="mt-8 max-w-xl">
<form method="POST" wire:submit="save" role="form" enctype="multipart/form-data">
@csrf
@include('livewire.division.form')
</form>
</x-crud.content>
</div>
13 changes: 13 additions & 0 deletions resources/views/livewire/division/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<x-crud.header>Division</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="divisions.index">Update the {{ $form->name }} division</x-crud.subheader>

<x-crud.content class="mt-8 max-w-xl">
<form method="POST" wire:submit="save" role="form" enctype="multipart/form-data">
{{ method_field('PATCH') }}
@csrf
@include('livewire.division.form')
</form>
</x-crud.content>
</div>
49 changes: 49 additions & 0 deletions resources/views/livewire/division/form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div class="space-y-6">
<div>
<x-input-label for="season_id" value="Season" />
@empty($form->competition_id)
<x-select-input wire:model="form.season_id" id="season_id" name="season_id" class="mt-1 block w-full"
:options="$seasons" placeholder="Choose a season" />
@else
<x-text-input wire:model="form.seasonName" id="season_id" name="season_id" type="text"
class="mt-1 block w-full" disabled />
@endempty
@error('form.season_id')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>
<div>
<x-input-label for="competitions_id" value="Competition" />
@empty($form->competition_id)
<x-select-input wire:model="form.competition_id" id="competition_id" name="competition_id"
class="mt-1 block w-full"
:options="$competitions" placeholder="Choose a competition" />
@else
<x-text-input wire:model="form.competitionName" id="competition_id" name="competition_id" type="text"
class="mt-1 block w-full" disabled />
@endempty
@error('form.competition_id')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>
<div>
<x-input-label for="name" value="Name" />
<x-text-input wire:model="form.name" id="name" name="name" type="text" class="mt-1 block w-full"
autocomplete="name" placeholder="Name" />
@error('form.name')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>
<div>
<x-input-label for="display_order" value="Display Order" />
<x-text-input wire:model="form.display_order" id="display_order" name="display_order" type="text"
class="mt-1 block w-full" autocomplete="display_order" placeholder="Display Order" />
@error('form.display_order')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>

<div class="flex items-center gap-4">
<x-primary-button>Save</x-primary-button>
</div>
</div>
31 changes: 31 additions & 0 deletions resources/views/livewire/division/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<x-crud.header>Divisions</x-crud.header>

<div class="w-full">
<x-crud.subheader add-route="divisions.create" class="mb-4">
A list of all the divisions in the system
</x-crud.subheader>
<x-crud.content>
<x-crud.index.table columns="season,competition,name,display order">
@foreach ($divisions as $division)
<x-crud.index.row row-key="{{ $division->getKey() }}">
<x-crud.index.cell>{{ $division->competition->season->name }}</x-crud.index.cell>
<x-crud.index.cell>{{ $division->competition->name }}</x-crud.index.cell>
<x-crud.index.cell>{{ $division->name }}</x-crud.index.cell>
<x-crud.index.cell>{{ $division->display_order }}</x-crud.index.cell>
<x-crud.index.cell class="flex gap-1 pl-2 pr-0 font-medium">
<x-crud.index.show-button route="divisions.show" :model="$division" />
<x-crud.index.edit-button route="divisions.edit" :model="$division" />
<x-crud.index.delete-button :model="$division">
Are you sure you want to delete the {{ $division->name }} divisions in the {{ $division->competition->name }}
competition in the {{ $division->competition->season->name }} season?
</x-crud.index.delete-button>
</x-crud.index.cell>
</x-crud.index.row>
@endforeach
</x-crud.index.table>

<div class="mt-4 px-4">
{!! $divisions->withQueryString()->links() !!}
</div>
</x-crud.content>
</div>
13 changes: 13 additions & 0 deletions resources/views/livewire/division/show.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<x-crud.header>Divisions</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="divisions.index">Details of the {{ $division->name }} division</x-crud.subheader>
<x-crud.content>
<x-crud.show.table>
<x-crud.show.model-field label="Season">{{ $division->competition->season->name }}</x-crud.show.model-field>
<x-crud.show.model-field label="Competition">{{ $division->competition->name }}</x-crud.show.model-field>
<x-crud.show.model-field label="Name">{{ $division->name }}</x-crud.show.model-field>
<x-crud.show.model-field label="Display order">{{ $division->display_order }}</x-crud.show.model-field>
</x-crud.show.table>
</x-crud.content>
</div>
6 changes: 5 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
Route::get('/competitions/show/{competition}', \App\Livewire\Competitions\Show::class)->name('competitions.show');
Route::get('/competitions/update/{competition}', \App\Livewire\Competitions\Edit::class)->name('competitions.edit');

Route::view('divisions', 'divisions')->name('divisions');
Route::get('/divisions', \App\Livewire\Divisions\Index::class)->name('divisions.index');
Route::get('/divisions/create', \App\Livewire\Divisions\Create::class)->name('divisions.create');
Route::get('/divisions/show/{division}', \App\Livewire\Divisions\Show::class)->name('divisions.show');
Route::get('/divisions/update/{division}', \App\Livewire\Divisions\Edit::class)->name('divisions.edit');

Route::view('fixtures', 'fixtures')->name('fixtures');
Route::view('clubs', 'clubs')->name('clubs');
Route::view('teams', 'teams')->name('teams');
Expand Down

0 comments on commit 0116b23

Please sign in to comment.