Skip to content

Commit

Permalink
Added Team CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
troccoli committed Sep 3, 2024
1 parent f5f35aa commit 6a12f11
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 2 deletions.
52 changes: 52 additions & 0 deletions app/Livewire/Forms/TeamForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Livewire\Forms;

use App\Models\Team;
use Livewire\Form;

class TeamForm extends Form
{
public ?Team $teamModel;

public ?string $club_id;

public ?string $clubName;

public ?string $name;

public ?string $venue_id;

public function rules(): array
{
return [
'club_id' => 'required|uuid|exists:clubs,id',
'name' => 'required|string',
'venue_id' => 'string|uuid|exists:venues,id',
];
}

public function setTeamModel(Team $teamModel): void
{
$this->teamModel = $teamModel;

$this->club_id = $this->teamModel->club_id;
$this->clubName = $this->teamModel->club?->name;
$this->name = $this->teamModel->name;
$this->venue_id = $this->teamModel->venue_id;
}

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

$this->reset();
}

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

$this->reset();
}
}
37 changes: 37 additions & 0 deletions app/Livewire/Teams/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Livewire\Teams;

use App\Livewire\Forms\TeamForm;
use App\Models\Club;
use App\Models\Team;
use App\Models\Venue;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Create extends Component
{
public TeamForm $form;

public function mount(Team $team): void
{
$this->form->setTeamModel($team);
}

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

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

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.team.create', [
'clubs' => Club::query()->orderBy('name')->get(),
'venues' => Venue::query()->orderBy('name')->get(),
]);
}
}
37 changes: 37 additions & 0 deletions app/Livewire/Teams/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Livewire\Teams;

use App\Livewire\Forms\TeamForm;
use App\Models\Club;
use App\Models\Team;
use App\Models\Venue;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Edit extends Component
{
public TeamForm $form;

public function mount(Team $team): void
{
$this->form->setTeamModel($team);
}

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

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

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.team.edit', [
'clubs' => Club::query()->orderBy('name')->get(),
'venues' => Venue::query()->orderBy('name')->get(),
]);
}
}
61 changes: 61 additions & 0 deletions app/Livewire/Teams/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Livewire\Teams;

use App\Models\Club;
use App\Models\Team;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
use WithPagination;

public string $clubId;

public array $filters;

public function mount(): void
{
$clubs = Club::query()->orderBy('name')->get();
$this->clubId = $clubs->first()->getKey();

$this->filters = [
'clubs' => [
'label' => 'Clubs',
'options' => $clubs,
'currentOption' => $this->clubId,
'event' => 'club-selected',
],
];
}

#[Layout('layouts.app')]
public function render(): View
{
$teams = Team::query()
->where('club_id', $this->clubId)
->with(['club', 'venue'])
->orderBy('name')
->simplePaginate(10);

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

public function delete(Team $team): void
{
$team->delete();

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

#[On('club-selected')]
public function setCurrentClub($clubId): void
{
$this->clubId = $clubId;
}
}
25 changes: 25 additions & 0 deletions app/Livewire/Teams/Show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Livewire\Teams;

use App\Livewire\Forms\TeamForm;
use App\Models\Team;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Show extends Component
{
public TeamForm $form;

public function mount(Team $team): void
{
$this->form->setTeamModel($team);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.team.show', ['team' => $this->form->teamModel]);
}
}
2 changes: 1 addition & 1 deletion resources/views/components/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<x-responsive-nav-link :href="route('clubs.index')" :active="request()->routeIs('clubs.*')" wire:navigate>
Clubs
</x-responsive-nav-link>
<x-responsive-nav-link :href="route('teams')" :active="request()->routeIs('teams')" wire:navigate>
<x-responsive-nav-link :href="route('teams.index')" :active="request()->routeIs('teams.*')" wire:navigate>
Teams
</x-responsive-nav-link>
<hr class="hidden md:block"/>
Expand Down
12 changes: 12 additions & 0 deletions resources/views/livewire/team/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-crud.header>Teams</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="teams.index">Create a new team</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.team.form')
</form>
</x-crud.content>
</div>
13 changes: 13 additions & 0 deletions resources/views/livewire/team/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<x-crud.header>Teams</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="teams.index">Update the {{ $this->form->teamModel->name }} team</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.team.form')
</form>
</x-crud.content>
</div>
35 changes: 35 additions & 0 deletions resources/views/livewire/team/form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div class="space-y-6">
<div>
<x-input-label for="club_id" value="Club" />
@empty($form->club_id)
<x-select-input wire:model="form.club_id" id="club_id" name="club_id" class="mt-1 block w-full"
:options="$clubs" placeholder="Choose a club" />
@else
<x-text-input wire:model="form.clubName" id="club_id" name="club_id" type="text"
class="mt-1 block w-full" disabled />
@endempty
@error('form.club_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="venue_id" value="Venue" />
<x-select-input wire:model="form.venue_id" id="venue_id" name="venue_id" class="mt-1 block w-full"
:options="$venues" placeholder="Choose a venue" />
@error('form.venue_id')
<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>
27 changes: 27 additions & 0 deletions resources/views/livewire/team/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<x-crud.header>Teams</x-crud.header>

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

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

<div class="w-full">
<x-crud.subheader back-route="teams.index">Details of the {{ $team->name }} team</x-crud.subheader>
<x-crud.content>
<x-crud.show.table>
<x-crud.show.model-field label="Club">{{ $team->club->name }}</x-crud.show.model-field>
<x-crud.show.model-field label="Name">{{ $team->name }}</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 @@ -32,7 +32,11 @@
Route::get('/clubs/show/{club}', \App\Livewire\Clubs\Show::class)->name('clubs.show');
Route::get('/clubs/update/{club}', \App\Livewire\Clubs\Edit::class)->name('clubs.edit');

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

Route::view('venues', 'venues')->name('venues');
});

Expand Down

0 comments on commit 6a12f11

Please sign in to comment.