Skip to content

Commit

Permalink
edit and creation without modal
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzodibenede committed Dec 17, 2024
1 parent 8598234 commit 8f547a5
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 343 deletions.
9 changes: 7 additions & 2 deletions app/Http/Controllers/PokemonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\Team;
use Illuminate\Http\Request;
use App\Models\Pokemon;
use App\Models\Type;
Expand All @@ -11,7 +12,6 @@ class PokemonController extends Controller
public function index(Request $request): \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application
{
$query = Pokemon::query();

if (isset($request->name) && !empty($request->name)) {
$query->where('name', 'LIKE', '%' . $request->name . '%');
}
Expand Down Expand Up @@ -51,10 +51,15 @@ public function index(Request $request): \Illuminate\Contracts\View\View|\Illumi
}
}
}
$team = null;
if (!empty($request->team)) {
$team = Team::getTeamById($request->team);
}
//dd($team);

$pokemons = $query->paginate(20);
$types = Type::all();
return view('pokemons.index', compact('pokemons', 'types'));
return view('pokemons.index', compact('pokemons', 'types', 'team'));
}

public function show(Pokemon $pokemon): \Illuminate\Contracts\View\View
Expand Down
34 changes: 1 addition & 33 deletions app/Http/Controllers/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public function store(TeamRequest $request): \Illuminate\Http\RedirectResponse
$team->save();

$selectedPokemons = $this->createSelectedPokemonsArray($request);

$team->pokemons()->attach($selectedPokemons);
$team->pokemons()->sync($selectedPokemons);

return redirect()->route('teams.index')
->with('success', 'Team created successfully.');
Expand Down Expand Up @@ -102,37 +101,6 @@ public function destroy($id): \Illuminate\Http\RedirectResponse
->with('success', 'Team deleted successfully.');
}

public function addPokemon(Pokemon $pokemon, Team $team)
{
if ($this->isNotTheCurrentUser($team->user_id)) {
return redirect()->back()
->with('error', 'You do not have permission to update this team.');
}

if ($team->isTeamFull()) {
return redirect()->back()
->with('error', 'The team already has 6 Pokémon.');
}

if ($team->isPokemonInTheTeam($pokemon)) {
return redirect()->back()
->with('error', 'This Pokémon is already in the team.');
}

$team->addPokemon($pokemon);

return redirect()->back()
->with('success', 'Pokémon added to the team successfully!');
}



public function addPokemonForm(Pokemon $pokemonToAdd): \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application
{
$teamsOfUser = auth()->user()->teamsNotFull;
return view('teams.add-pokemon', compact('teamsOfUser', 'pokemonToAdd'));
}

private function createSelectedPokemonsArray(TeamRequest $request): array
{
$selectedPokemons = $request->input('selected_pokemons', '');
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ public function categorizedResistances()
}
return $categorized;
}

public static function getTeamById($id)
{
return self::find($id);
}
}

1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function run(): void
$this->call([
TypeSeeder::class,
PokemonSeeder::class,
TypeRatioSeeder::class,
]);
}
}
4 changes: 2 additions & 2 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { initializePokemonSelection } from './initializePokemonSelection.js';

document.addEventListener('DOMContentLoaded', () => {
if (window.location.pathname === '/teams/create') {
initializePokemonSelection('.slot', '.add-pokemon-btn', '#pokemonModal', '#selectedPokemons');
initializePokemonSelection('#selectedPokemons');

}
});

document.addEventListener('DOMContentLoaded', () => {
initializePokemonSelection('.slot', '.add-pokemon-btn', '#pokemonModal', '#selectedPokemons');
initializePokemonSelection( '#selectedPokemons');
});
101 changes: 1 addition & 100 deletions resources/js/initializePokemonSelection.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
export function initializePokemonSelection(slotSelector, addPokemonBtnSelector, modalSelector, hiddenInputSelector) {
export function initializePokemonSelection(hiddenInputSelector) {
const selectedPokemons = {};
let currentSlotId = null;

function getUrlParams() {
return new URLSearchParams(window.location.search);
}

function updateUrlParams() {
const params = new URLSearchParams(window.location.search);

Object.keys(selectedPokemons).forEach(slotId => {
params.set(`pokemon_${slotId}`, selectedPokemons[slotId]);
});

window.history.replaceState({}, '', `${window.location.pathname}?${params.toString()}`);
}

function loadSelectedPokemonsFromUrl() {
const params = getUrlParams();
const pairs = params.toString().split("&");
Expand All @@ -36,98 +25,10 @@ export function initializePokemonSelection(slotSelector, addPokemonBtnSelector,
updateHiddenInput();
}

function updateSlotDisplay(slotId, pokemonName, pokemonSprite) {
const slotElement = document.getElementById(`slot-${slotId}`);
if (pokemonName) {
slotElement.innerHTML = `
<div class="card text-center">
<img class="card-img-top" src="${pokemonSprite}" alt="Sprite of ${pokemonName}" style="object-fit: contain; aspect-ratio: 1/1" height="200px">
<div class="card-body p-2">
<h5 class="card-title mb-0">${pokemonName}</h5>
</div>
</div>
`;
} else {
slotElement.innerHTML = "";
}
}

function updateHiddenInput() {
const hiddenInput = document.querySelector(hiddenInputSelector);
hiddenInput.value = Object.values(selectedPokemons).join(',');
}

function updateCardVisuals() {
document.querySelectorAll('.pokemon-card').forEach(card => {
card.classList.remove('selected-green', 'selected-yellow');
const pokemonId = card.querySelector('.add-pokemon-btn').dataset.pokemonId;
const addButton = card.querySelector('.add-pokemon-btn');

if (selectedPokemons[currentSlotId] === pokemonId) {
updateCardIfSelected(card, addButton);
} else if (Object.values(selectedPokemons).some(id => id === pokemonId)) {
updateCardIfSelectedOnAnotherSlot(card, addButton);
} else {
updateCardIfNotSelected(card, addButton);
}
});
}

function updateCardIfSelected(card, addButton) {
card.classList.add('selected-green');
addButton.classList.remove('btn-success');
addButton.classList.add('btn-danger');
addButton.innerHTML = '<i class="bi bi-dash"></i>';
}

function updateCardIfSelectedOnAnotherSlot(card, addButton) {
card.classList.add('selected-yellow');
addButton.classList.remove('btn-danger');
addButton.classList.add('btn-success');
addButton.innerHTML = '<i class="bi bi-plus"></i>';
}

function updateCardIfNotSelected(card, addButton) {
addButton.classList.remove('btn-danger');
addButton.classList.add('btn-success');
addButton.innerHTML = '<i class="bi bi-plus"></i>';
}

function handleSlotClick(event) {
currentSlotId = event.currentTarget.dataset.slotId;
updateCardVisuals();
$(modalSelector).modal('show');
}

function handleAddButtonClick(event) {
const button = event.currentTarget;
const pokemonId = button.dataset.pokemonId;
const pokemonName = button.dataset.pokemonName;
const pokemonSprite = button.dataset.pokemonSprite;

if (selectedPokemons[currentSlotId] === pokemonId) {
delete selectedPokemons[currentSlotId];
updateSlotDisplay(currentSlotId);
} else if (!Object.values(selectedPokemons).some(id => id === pokemonId)) {
selectedPokemons[currentSlotId] = pokemonId;
updateSlotDisplay(currentSlotId, pokemonName, pokemonSprite);
} else {
alert("This Pokemon is already selected in another slot.");
return;
}

updateHiddenInput();
updateCardVisuals();
updateUrlParams();
}

document.querySelectorAll(slotSelector).forEach(slot => {
slot.addEventListener('click', handleSlotClick);
});

document.querySelectorAll(addPokemonBtnSelector).forEach(button => {
button.addEventListener('click', handleAddButtonClick);
});

loadSelectedPokemonsFromUrl();
}
3 changes: 2 additions & 1 deletion resources/views/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
New to PokeTeams? <a href="{{ route('register') }}">Create an account</a>
<br>

@include("partials.errors-form")

<button class="btn btn-primary mt-2">Sign in</button>
</form>
</div>
Expand Down
44 changes: 0 additions & 44 deletions resources/views/partials/pokemon-modal.blade.php

This file was deleted.

Loading

0 comments on commit 8f547a5

Please sign in to comment.