wip: feedback about booked and assigned rooms
This commit is contained in:
@@ -4,11 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Booking;
|
||||
|
||||
use App\Controller\Traits\HtmxControllerTrait;
|
||||
use App\Form\BookingCreateStep2Type;
|
||||
use App\Form\Model\BookingCreateDto;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Service\BookingService;
|
||||
use App\Validator\Constraints\Participant;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -23,6 +23,7 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
class CreateStep2Controller extends AbstractController
|
||||
{
|
||||
use BookingCreateTrait;
|
||||
use HtmxControllerTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly BookingService $bookingService,
|
||||
@@ -61,9 +62,12 @@ class CreateStep2Controller extends AbstractController
|
||||
return $this->redirectToRoute('app_booking_create_step_3');
|
||||
}
|
||||
|
||||
$roomAssignmentCounts = $this->bookingService->getRoomAssignmentCounts($bookingCreateDto);
|
||||
|
||||
return $this->render('booking/create_step_2.html.twig', [
|
||||
'bookingCreateDto' => $bookingCreateDto,
|
||||
'participantsCount' => $participantsCount,
|
||||
'assignmentCounts' => $roomAssignmentCounts,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
@@ -76,6 +80,7 @@ class CreateStep2Controller extends AbstractController
|
||||
public function refreshParticipantForm(Request $request): Response
|
||||
{
|
||||
$bookingCreateDto = $this->bookingService->getOrCreateBookingCreateDto($request);
|
||||
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
|
||||
|
||||
// Process form data without validation to capture current state
|
||||
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
|
||||
@@ -85,9 +90,20 @@ class CreateStep2Controller extends AbstractController
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
return $this->renderBlock('booking/create_step_2.html.twig', 'participants_form', [
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
$roomAssignmentCounts = $this->bookingService->getRoomAssignmentCounts($bookingCreateDto);
|
||||
|
||||
// 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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -102,4 +102,22 @@ class BookingService
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user