wip: feedback about booked and assigned rooms

This commit is contained in:
Björn Fromme
2025-07-23 11:39:03 +02:00
parent e8c27cb51b
commit 8746397d83
4 changed files with 126 additions and 46 deletions
@@ -4,11 +4,11 @@ declare(strict_types=1);
namespace App\Controller\Booking; namespace App\Controller\Booking;
use App\Controller\Traits\HtmxControllerTrait;
use App\Form\BookingCreateStep2Type; use App\Form\BookingCreateStep2Type;
use App\Form\Model\BookingCreateDto; use App\Form\Model\BookingCreateDto;
use App\Form\Model\ParticipantDto; use App\Form\Model\ParticipantDto;
use App\Service\BookingService; use App\Service\BookingService;
use App\Validator\Constraints\Participant;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -23,6 +23,7 @@ use Symfony\Component\Routing\Attribute\Route;
class CreateStep2Controller extends AbstractController class CreateStep2Controller extends AbstractController
{ {
use BookingCreateTrait; use BookingCreateTrait;
use HtmxControllerTrait;
public function __construct( public function __construct(
private readonly BookingService $bookingService, private readonly BookingService $bookingService,
@@ -61,9 +62,12 @@ class CreateStep2Controller extends AbstractController
return $this->redirectToRoute('app_booking_create_step_3'); return $this->redirectToRoute('app_booking_create_step_3');
} }
$roomAssignmentCounts = $this->bookingService->getRoomAssignmentCounts($bookingCreateDto);
return $this->render('booking/create_step_2.html.twig', [ return $this->render('booking/create_step_2.html.twig', [
'bookingCreateDto' => $bookingCreateDto, 'bookingCreateDto' => $bookingCreateDto,
'participantsCount' => $participantsCount, 'participantsCount' => $participantsCount,
'assignmentCounts' => $roomAssignmentCounts,
'form' => $form->createView(), 'form' => $form->createView(),
]); ]);
} }
@@ -76,6 +80,7 @@ class CreateStep2Controller extends AbstractController
public function refreshParticipantForm(Request $request): Response public function refreshParticipantForm(Request $request): Response
{ {
$bookingCreateDto = $this->bookingService->getOrCreateBookingCreateDto($request); $bookingCreateDto = $this->bookingService->getOrCreateBookingCreateDto($request);
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
// Process form data without validation to capture current state // Process form data without validation to capture current state
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [ $form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
@@ -85,9 +90,20 @@ class CreateStep2Controller extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
return $this->renderBlock('booking/create_step_2.html.twig', 'participants_form', [ $roomAssignmentCounts = $this->bookingService->getRoomAssignmentCounts($bookingCreateDto);
'form' => $form->createView(),
]); // The DTO is now updated with the latest selection.
// We can now render the blocks with the fresh data.
return $this->htmxOobResponse(
'booking/create_step_2.html.twig',
['participants_form', 'booking_summary'],
[
'form' => $form->createView(),
'bookingCreateDto' => $bookingCreateDto,
'participantsCount' => $participantsCount,
'assignmentCounts' => $roomAssignmentCounts,
]
);
} }
private function getParticipantsCount(BookingCreateDto $bookingCreateDto): int private function getParticipantsCount(BookingCreateDto $bookingCreateDto): int
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Controller\Traits;
use Symfony\Component\HttpFoundation\Response;
/**
* Provides helper methods for handling HTMX responses.
*
* This trait should be used in controllers that extend Symfony's AbstractController,
* as it relies on the renderBlockView() method.
*/
trait HtmxControllerTrait
{
/**
* Renders multiple Twig blocks for an HTMX Out-of-Band swap response.
*
* @param string $templateName the name of the Twig template
* @param string[] $blockNames an array of block names to render
* @param array<string, mixed> $context the context to pass to the template
*/
protected function htmxOobResponse(string $templateName, array $blockNames, array $context = []): Response
{
$html = '';
// Add a flag to the context so templates can conditionally add the hx-swap-oob attribute.
$oobContext = $context + ['htmx_oob_swap' => true];
foreach ($blockNames as $blockName) {
// Use renderBlockView() to get the raw HTML string for each block.
$html .= $this->renderBlockView($templateName, $blockName, $oobContext);
}
return new Response($html);
}
}
+18
View File
@@ -102,4 +102,22 @@ class BookingService
return $participantsCount; return $participantsCount;
} }
/**
* Calculates the number of participants assigned to each room ID.
*
* @return array<int, int> an array where the key is the room ID and the value is the count of assigned participants
*/
public function getRoomAssignmentCounts(BookingCreateDto $bookingCreateDto): array
{
$counts = [];
foreach ($bookingCreateDto->participants as $participant) {
if (null !== $participant->assignedRoomId) {
$counts[$participant->assignedRoomId] = ($counts[$participant->assignedRoomId] ?? 0) + 1;
}
}
return $counts;
}
} }
+50 -41
View File
@@ -8,8 +8,9 @@
<h2>Teilnehmer</h2> <h2>Teilnehmer</h2>
{{ form_start(form) }} {{ form_start(form) }}
{% do form.participants.setRendered %} {% do form.participants.setRendered %}
{# This block contains the participant form fields #}
{% block participants_form %} {% block participants_form %}
<div id="participants-form" class="space-y-8 pb-8"> <div id="participants-form" class="space-y-8 pb-8"{% if htmx_oob_swap is defined and htmx_oob_swap %} hx-swap-oob="true"{% endif %}>
{% for participant in form.participants %} {% for participant in form.participants %}
<fieldset class="border rounded-md p-8 pt-4"> <fieldset class="border rounded-md p-8 pt-4">
<legend class="font-bold text-xl px-2"> <legend class="font-bold text-xl px-2">
@@ -27,12 +28,11 @@
{{ form_row(participant.mobile) }} {{ form_row(participant.mobile) }}
</div> </div>
<div class="grid grid-cols-1 gap-4 pt-4"> <div class="grid grid-cols-1 gap-4 pt-4">
{# hx-swap="none" tells HTMX not to do a normal swap, as OOB will handle it #}
{{ form_row(participant.assignedRoomId, { {{ form_row(participant.assignedRoomId, {
'attr': { 'attr': {
'hx-post': path('app_booking_create_step_2_refresh'), 'hx-post': path('app_booking_create_step_2_refresh'),
'hx-target': '#participants-form', 'hx-swap': 'none'
'hx-select': '#participants-form',
'hx-swap': 'outerHTML'
} }
}) }} }) }}
</div> </div>
@@ -48,43 +48,52 @@
{{ form_end(form) }} {{ form_end(form) }}
</div> </div>
<div> <div>
<h3>Zusammenfassung</h3> {# This block contains the booking summary #}
<h4> {% block booking_summary %}
Reise <div id="booking-summary"{% if htmx_oob_swap is defined and htmx_oob_swap %} hx-swap-oob="true"{% endif %}>
</h4> <h3>Zusammenfassung</h3>
<p> <h4>
{{ bookingCreateDto.travelData.label }} Reise
</p> </h4>
<h4> <p>
Datum {{ bookingCreateDto.travelData.label }}
</h4> </p>
<p> <h4>
{{ bookingCreateDto.travelData.dateFrom | date('d.m.Y') }} - {{ bookingCreateDto.travelData.dateTo | date('d.m.Y') }} Datum
</p> </h4>
<h4> <p>
Unterkunft {{ bookingCreateDto.travelData.dateFrom | date('d.m.Y') }} - {{ bookingCreateDto.travelData.dateTo | date('d.m.Y') }}
</h4> </p>
<p> <h4>
{{ bookingCreateDto.travelData.hotel.name }} Unterkunft
</p> </h4>
{% if participantsCount > 0 %} <p>
<h4> {{ bookingCreateDto.travelData.hotel.name }}
Anzahl Teilnehmer </p>
</h4> {% if participantsCount > 0 %}
<p> <h4>
{{ participantsCount }} Anzahl Teilnehmer
</p> </h4>
{% endif %} <p>
<h4> {{ participantsCount }}
Zimmer </p>
</h4> {% endif %}
<ul> <h4>
{% for roomSelection in bookingCreateDto.selectedRooms %} Zimmer
<li> </h4>
{{ roomSelection.quantity }}x {{ roomSelection.roomLabel }} <ul>
</li> {% for roomSelection in bookingCreateDto.selectedRooms %}
{% endfor %} {% set assignedCount = assignmentCounts[roomSelection.roomId] ?? 0 %}
</ul> <li>
{{ roomSelection.roomLabel }}
<span class="text-sm pl-2 {% if assignedCount == roomSelection.quantity %}text-green-600{% else %}text-gray-500{% endif %}">
({{ assignedCount }}/{{ roomSelection.quantity }})
</span>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}