Initial commit
This commit is contained in:
184
app/Livewire/GameSelector.php
Normal file
184
app/Livewire/GameSelector.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Game;
|
||||
use App\Models\Genre;
|
||||
use App\Models\Platform;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Game Selector for existing games and new games.
|
||||
*/
|
||||
class GameSelector extends Component
|
||||
{
|
||||
|
||||
public const int REQUIRED_CHARS = 3;
|
||||
|
||||
/**
|
||||
* If we are in new game mode.
|
||||
* @var bool
|
||||
*/
|
||||
public bool $newGame = false;
|
||||
|
||||
/**
|
||||
* Current user game search.
|
||||
* @var string
|
||||
*/
|
||||
public string $search = '';
|
||||
|
||||
/**
|
||||
* Existing game ID selected.
|
||||
* @var int|null
|
||||
*/
|
||||
public ?int $gameId = null;
|
||||
|
||||
/**
|
||||
* Existing game name selected or new game name.
|
||||
* @var string|null
|
||||
*/
|
||||
public ?string $gameName = null;
|
||||
|
||||
/**
|
||||
* New game platform ID.
|
||||
* @var int|null
|
||||
*/
|
||||
public ?int $gamePlatformId = null;
|
||||
|
||||
/**
|
||||
* New game genre ID.
|
||||
* @var int|null
|
||||
*/
|
||||
public ?int $gameGenreId = null;
|
||||
|
||||
/**
|
||||
* Existing game platform name or new game platform name.
|
||||
* @var string|null
|
||||
*/
|
||||
public ?string $platformName = null;
|
||||
|
||||
/**
|
||||
* Existing game genre name or new game genre name.
|
||||
* @var string|null
|
||||
*/
|
||||
public ?string $genreName = null;
|
||||
|
||||
/**
|
||||
* If dropdown must be rendered or not.
|
||||
* @var bool
|
||||
*/
|
||||
public bool $dropdown = false;
|
||||
|
||||
public function mount( ?int $gameId = null, ?string $newGameTitle = null, ?int $newGamePlatform = null, ?int $newGameGenre = null ): void
|
||||
{
|
||||
|
||||
// If we selected an existent game.
|
||||
if( $gameId ){
|
||||
$game = Game::with(['platform','genre'])->find($gameId);
|
||||
if( $game ){
|
||||
$this->gameId = $game->id;
|
||||
$this->gameName = $game->name;
|
||||
$this->platformName = $game->platform->name;
|
||||
$this->genreName = $game->genre->name;
|
||||
$this->search = $game->name;
|
||||
$this->newGame = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if( $newGameTitle || $newGamePlatform || $newGameGenre ){
|
||||
$this->newGame = true;
|
||||
$this->gameName = $newGameTitle;
|
||||
$this->gamePlatformId = is_numeric($newGamePlatform) ? (int) $newGamePlatform : null;
|
||||
$this->gameGenreId = is_numeric($newGameGenre) ? (int) $newGameGenre : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If we update search bar.
|
||||
* @return void
|
||||
*/
|
||||
public function updatedSearch(): void
|
||||
{
|
||||
if( $this->gameId ){
|
||||
$this->gameId = null;
|
||||
$this->gameName = null;
|
||||
$this->platformName = null;
|
||||
$this->genreName = null;
|
||||
}
|
||||
$this->dropdown = strlen($this->search) >= self::REQUIRED_CHARS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select an existent game.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $name
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function selectGame( int $id, string $name ): void
|
||||
{
|
||||
$game = Game::with(['platform','genre'])->find($id);
|
||||
if( $game ){
|
||||
$this->gameId = $game->id;
|
||||
$this->gameName = $game->name;
|
||||
$this->platformName = $game->platform->name;
|
||||
$this->genreName = $game->genre->name;
|
||||
$this->search = $game->name;
|
||||
$this->dropdown = false;
|
||||
|
||||
$this->dispatch( 'game-selected', id: $id ); // Send an event to the JS part.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear existent game selection.
|
||||
* @return void
|
||||
*/
|
||||
public function clearGame(): void
|
||||
{
|
||||
$this->gameId = null;
|
||||
$this->gameName = null;
|
||||
$this->platformName = null;
|
||||
$this->genreName = null;
|
||||
$this->search = '';
|
||||
|
||||
$this->dispatch( 'game-selected', id: null ); // Send an event to the JS part.
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch mode.
|
||||
* @return void
|
||||
*/
|
||||
public function switchNewGame(): void
|
||||
{
|
||||
$this->clearGame();
|
||||
$this->newGame = !$this->newGame;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
$games = collect();
|
||||
|
||||
// Need to search games for dropdown.
|
||||
if( $this->dropdown && strlen($this->search) >= self::REQUIRED_CHARS && $this->newGame === false ){
|
||||
$games = Game::with(['platform','genre'])
|
||||
->where('name', 'like', '%'.$this->search.'%')
|
||||
->orderBy('name')
|
||||
->limit(20)
|
||||
->get();
|
||||
}
|
||||
|
||||
$data = [ 'games' => $games, 'required_chars' => self::REQUIRED_CHARS ];
|
||||
if( $this->newGame === true ){ // If we want a new game, get platforms and genres.
|
||||
$data['platforms'] = Platform::orderBy('name')->get();
|
||||
$data['genres'] = Genre::orderBy('name')->get();
|
||||
}
|
||||
$data['hasOldNewGame'] = old('new-game-title') || old('new-game-platform') || old('new-game-genre');
|
||||
|
||||
return view('livewire.game-selector', $data );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user