Skip to content

Commit

Permalink
back via given route
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzodibenede committed Nov 14, 2024
1 parent 83e9e92 commit 264a5c5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 64 deletions.
7 changes: 2 additions & 5 deletions app/Http/Controllers/PokemonController.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

namespace App\Http\Controllers;


use App\Models\Pokemon;
use Illuminate\Http\Client\Request;
use Symfony\Component\Console\Input\Input;

class PokemonController extends Controller
{
Expand Down
25 changes: 16 additions & 9 deletions app/Http/Controllers/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public function store(TeamRequest $request): \Illuminate\Http\RedirectResponse
$team->user_id = auth()->id();
$team->save();

$selectedPokemons = $request->input('selected_pokemons')
? explode(',', $request->input('selected_pokemons'))
: [];
$selectedPokemons = $this->createSelectedPokemonsArray($request);

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

return redirect()->route('teams.index')
Expand All @@ -43,7 +42,8 @@ public function edit($id)
$team = Team::findOrFail($id);

if ($team->user_id !== auth()->id()) {
return redirect()->route('teams.index')->with('error', 'You do not have permission to edit this team.');
return redirect()->route('teams.index')
->with('error', 'You do not have permission to edit this team.');
}

$pokemons = Pokemon::all();
Expand All @@ -55,15 +55,14 @@ public function update(TeamRequest $request, $id): \Illuminate\Http\RedirectResp
$team = Team::findOrFail($id);

if ($team->user_id !== auth()->id()) {
return redirect()->route('teams.index')->with('error', 'You do not have permission to update this team.');
return redirect()->route('teams.index')
->with('error', 'You do not have permission to update this team.');
}

$team->name = $request->name;
$team->save();

$selectedPokemons = $request->input('selected_pokemons')
? explode(',', $request->input('selected_pokemons'))
: [];
$selectedPokemons = $this->createSelectedPokemonsArray($request);
$team->pokemons()->sync($selectedPokemons);

return redirect()->route('teams.index')
Expand All @@ -81,12 +80,20 @@ public function destroy($id): \Illuminate\Http\RedirectResponse
$team = Team::findOrFail($id);

if ($team->user_id !== auth()->id()) {
return redirect()->route('teams.index')->with('error', 'You do not have permission to delete this team.');
return redirect()->route('teams.index')
->with('error', 'You do not have permission to delete this team.');
}

$team->delete();

return redirect()->route('teams.index')
->with('success', 'Team deleted successfully.');
}

private function createSelectedPokemonsArray(TeamRequest $request): array
{
return $request->input('selected_pokemons')
? explode(',', $request->input('selected_pokemons'))
: [];
}
}
93 changes: 48 additions & 45 deletions resources/js/initializePokemonSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,60 @@ export function initializePokemonSelection(slotSelector, pokemonCardSelector, mo
const selectedPokemons = {};
let currentSlotId = null;

document.querySelectorAll(slotSelector).forEach(slot => {
slot.addEventListener('click', function () {
currentSlotId = this.dataset.slotId;

document.querySelectorAll(pokemonCardSelector).forEach(card => {
card.classList.remove('selected-green', 'selected-blue');
function updateSlotDisplay(slotId, pokemonName) {
document.getElementById(`slot-${slotId}`).textContent = pokemonName || "Empty";
}

if (Object.values(selectedPokemons).includes(card.dataset.pokemonId)) {
card.classList.add('selected-blue');
}
function updateHiddenInput() {
document.querySelector(hiddenInputSelector).value = Object.values(selectedPokemons).join(',');
}

if (selectedPokemons[currentSlotId] === card.dataset.pokemonId) {
card.classList.remove('selected-blue');
card.classList.add('selected-green');
}
});
function updateCardVisuals() {
document.querySelectorAll(pokemonCardSelector).forEach(card => {
card.classList.remove('selected-green', 'selected-blue');

$(modalSelector).modal('show');
if (Object.values(selectedPokemons).includes(card.dataset.pokemonId)) {
card.classList.add('selected-blue');
}
if (selectedPokemons[currentSlotId] === card.dataset.pokemonId) {
card.classList.remove('selected-blue');
card.classList.add('selected-green');
}
});
}

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

function handleCardClick(event) {
const card = event.currentTarget;
const pokemonId = card.dataset.pokemonId;
const pokemonName = card.dataset.pokemonName;

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

updateHiddenInput();
updateCardVisuals();
$(modalSelector).modal('hide');
}

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

document.querySelectorAll(pokemonCardSelector).forEach(card => {
card.addEventListener('click', function () {
const pokemonId = this.dataset.pokemonId;
const pokemonName = this.dataset.pokemonName;

if (selectedPokemons[currentSlotId] === pokemonId) {
delete selectedPokemons[currentSlotId];
document.getElementById(`slot-${currentSlotId}`).textContent = "Empty";
document.querySelector(hiddenInputSelector).value = Object.values(selectedPokemons).join(',');

this.classList.remove('selected-green');
} else if (!Object.values(selectedPokemons).includes(pokemonId)) {
document.getElementById(`slot-${currentSlotId}`).textContent = pokemonName;
selectedPokemons[currentSlotId] = pokemonId;
document.querySelector(hiddenInputSelector).value = Object.values(selectedPokemons).join(',');

document.querySelectorAll(pokemonCardSelector).forEach(card => {
card.classList.remove('selected-green', 'selected-blue');
if (Object.values(selectedPokemons).includes(card.dataset.pokemonId)) {
card.classList.add('selected-blue');
}
if (selectedPokemons[currentSlotId] === card.dataset.pokemonId) {
card.classList.add('selected-green');
}
});
} else {
alert("This Pokemon is already selected in another slot.");
}

$(modalSelector).modal('hide');
});
card.addEventListener('click', handleCardClick);
});
}
1 change: 0 additions & 1 deletion resources/views/partials/team-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

<input type="hidden" name="selected_pokemons" id="selectedPokemons">


<style>
.slot {
height: 100px;
Expand Down
9 changes: 8 additions & 1 deletion resources/views/pokemons/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
@foreach($pokemons as $pokemon)
<tr>
<td>
<a href="{{ route('pokemons.show', $pokemon) }}">{{ $pokemon->name }}</a>
<a href="{{ route('pokemons.show', [
'pokemon' => $pokemon,
'backRoute' => 'pokemons.index'
]
)
}}">
{{ $pokemon->name }}
</a>
</td>
<td class="w-25">
<div class="type-wrapper">
Expand Down
5 changes: 3 additions & 2 deletions resources/views/pokemons/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
@section("content")

<div class="container mt-5 w-75">
<a href="{{ route('pokemons.index') }}" class="btn btn-primary mb-3"><i class="bi bi-arrow-return-left"></i>
Back to Pokemons</a>
<a href="{{ route($_GET['backRoute']) }}" class="btn btn-primary mb-3"><i class="bi bi-arrow-return-left"></i>
Back
</a>

<h1>{{ $pokemon->name }}</h1>

Expand Down
1 change: 0 additions & 1 deletion resources/views/teams/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@
</div>

@include("partials.pokemon-modal")

@endsection

0 comments on commit 264a5c5

Please sign in to comment.