feat: refactor to cards
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Booking\Create;
|
||||
|
||||
use App\Controller\Booking\Traits\BookingCreateTrait;
|
||||
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
|
||||
use App\Form\BookingCreateStep1Type;
|
||||
use App\Htmx\HxTrait;
|
||||
use App\Service\BookingService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
* Handles the first step of the booking creation process.
|
||||
*
|
||||
* This controller manages room selection functionality where users
|
||||
* choose the types and quantities of rooms for their booking.
|
||||
*/
|
||||
class Step1Controller extends AbstractController
|
||||
{
|
||||
use BookingCreateTrait;
|
||||
use BookingExceptionHandlerTrait;
|
||||
use HxTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly BookingService $bookingService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays and processes the room selection form.
|
||||
*/
|
||||
#[Route('/bookings/create', name: 'app_booking_create_step_1')]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$result = $this->getOrCreateBookingCreateDto($this->bookingService, $request);
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
$bookingCreateDto = $result;
|
||||
|
||||
// Get or create baseline snapshot for change detection
|
||||
$oldRoomSelectionSnapshot = $this->bookingService->getOrCreateBaselineSnapshot($request, $bookingCreateDto);
|
||||
|
||||
$summary = $this->bookingService->getRoomSummaryAndParticipantCount($bookingCreateDto);
|
||||
|
||||
// Validate step access - allow step 1 or redirect to current step
|
||||
if ($redirect = $this->validateStepAccess($bookingCreateDto, 1)) {
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
$form = $this->createForm(BookingCreateStep1Type::class, $bookingCreateDto, [
|
||||
'attr' => [
|
||||
'hx-post' => $this->generateUrl('app_booking_create_step_1'),
|
||||
'hx-target' => '#form-wrapper',
|
||||
'hx-select' => '#form-wrapper',
|
||||
'hx-swap' => 'outerHTML',
|
||||
],
|
||||
'validation_groups' => ['booking_create_step_1'],
|
||||
]);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
if ($this->bookingService->hasRoomSelectionChanged($oldRoomSelectionSnapshot, $bookingCreateDto)) {
|
||||
$this->bookingService->resetParticipantAssignments($bookingCreateDto);
|
||||
}
|
||||
|
||||
$bookingCreateDto->currentStep = 2;
|
||||
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
|
||||
|
||||
// Clear baseline snapshot when moving to step 2
|
||||
$this->bookingService->clearBaselineSnapshot($request);
|
||||
|
||||
return $this->hxRedirect($request, $this->generateUrl('app_booking_create_step_2'));
|
||||
}
|
||||
|
||||
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
|
||||
$groupedRooms = $this->bookingService->groupRoomsBySelectionType($availableRooms);
|
||||
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($summary['selectedRooms'], $availableRooms);
|
||||
|
||||
return $this->render('booking/create/step_1.html.twig', [
|
||||
'bookingCreateDto' => $bookingCreateDto,
|
||||
'roomSummary' => $summary['selectedRooms'],
|
||||
'participantCount' => $summary['participantCount'],
|
||||
'pricingData' => $summary['pricing'],
|
||||
'form' => $form->createView(),
|
||||
'groupedRooms' => $groupedRooms,
|
||||
'groupedSelectedRooms' => $groupedSelectedRooms,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles HTMX requests for dynamic form updates when room selections change by submitting the form
|
||||
* without validation and returning freshly rendered blocks.
|
||||
*/
|
||||
#[Route('/bookings/create/refresh', name: 'app_booking_create_step_1_refresh', methods: ['POST'])]
|
||||
public function refreshRoomSelection(Request $request): Response
|
||||
{
|
||||
$result = $this->getOrCreateBookingCreateDtoForHtmx($this->bookingService, $request);
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
$bookingCreateDto = $result;
|
||||
|
||||
// Process form data without validation to capture current state
|
||||
$form = $this->createForm(BookingCreateStep1Type::class, $bookingCreateDto, [
|
||||
'validation_groups' => false,
|
||||
]);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
|
||||
|
||||
$summary = $this->bookingService->getRoomSummaryAndParticipantCount($bookingCreateDto);
|
||||
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
|
||||
$groupedRooms = $this->bookingService->groupRoomsBySelectionType($availableRooms);
|
||||
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($summary['selectedRooms'], $availableRooms);
|
||||
|
||||
// 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_1.html.twig',
|
||||
['room_selection_form', 'booking_summary'],
|
||||
[
|
||||
'form' => $form->createView(),
|
||||
'bookingCreateDto' => $bookingCreateDto,
|
||||
'participantCount' => $summary['participantCount'],
|
||||
'pricingData' => $summary['pricing'],
|
||||
'groupedRooms' => $groupedRooms,
|
||||
'groupedSelectedRooms' => $groupedSelectedRooms,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user