fix: keep browser history intact when navigating between cards view and participant form

This commit is contained in:
Björn Fromme
2025-10-31 12:46:37 +01:00
parent cfd346d5df
commit 34a8d42fc3
7 changed files with 136 additions and 70 deletions
@@ -120,7 +120,8 @@ class Step2Controller extends AbstractController
return $this->htmxOobResponse(
'booking/create/step_2.html.twig',
['participant_cards', 'booking_summary'],
$templateData
$templateData,
$this->generateUrl('app_booking_create_step_2')
);
}
@@ -169,21 +170,28 @@ class Step2Controller extends AbstractController
// Get detailed pricing data for summary sidebar
$summary = $this->bookingService->getRoomSummaryAndParticipantCount($bookingDto);
// Render form and sidebar with OOB swap using htmxOobResponse
// This ensures both initial load and refresh use the same block-based rendering
return $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
[
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'summaryData' => $summaryData,
'pricingData' => $summary['pricing'],
'refreshRouteName' => 'app_booking_create_step_2_participant_refresh',
'submitRouteName' => 'app_booking_create_step_2_participant',
]
);
$templateData = [
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'summaryData' => $summaryData,
'pricingData' => $summary['pricing'],
'refreshRouteName' => 'app_booking_create_step_2_participant_refresh',
'submitRouteName' => 'app_booking_create_step_2_participant',
];
// HTMX request: render blocks only with OOB swap
if ($this->isHxRequest($request)) {
return $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
$templateData,
$this->generateUrl('app_booking_create_step_2_participant', ['index' => $index])
);
}
// Regular request: render full template
return $this->render('booking/create/step_2_participant.html.twig', $templateData);
}
/**
+30 -22
View File
@@ -188,7 +188,8 @@ class IndexController extends AbstractController
return $this->htmxOobResponse(
'booking/edit/index.html.twig',
['participant_cards', 'booking_summary'],
$templateData
$templateData,
$this->generateUrl('app_booking_edit', ['id' => $id])
);
}
@@ -274,27 +275,34 @@ class IndexController extends AbstractController
// Fetch mutable data for form constraints
$mutableData = $this->travelDataService->getMutabilityData($bookingData->dateId);
// Render form and sidebar with OOB swap using htmxOobResponse
// This ensures both initial load and refresh use the same block-based rendering
return $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
[
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'summaryData' => $summaryData,
'pricingData' => $summary['pricing'],
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'submitRouteName' => 'app_booking_edit_participant',
'submitRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
'cancelRouteParams' => ['id' => $id],
]
);
$templateData = [
'form' => $form->createView(),
'participantIndex' => $index,
'bookingDto' => $bookingDto,
'bookingData' => $bookingData,
'mutableData' => $mutableData,
'summaryData' => $summaryData,
'pricingData' => $summary['pricing'],
'refreshRouteName' => 'app_booking_edit_participant_refresh',
'refreshRouteParams' => ['id' => $id, 'index' => $index],
'submitRouteName' => 'app_booking_edit_participant',
'submitRouteParams' => ['id' => $id, 'index' => $index],
'cancelRouteName' => 'app_booking_edit',
'cancelRouteParams' => ['id' => $id],
];
// HTMX request: render blocks only with OOB swap
if ($this->isHxRequest($request)) {
return $this->htmxOobResponse(
'booking/_participant_form.html.twig',
['participant_form', 'booking_summary'],
$templateData,
$this->generateUrl('app_booking_edit_participant', ['id' => $id, 'index' => $index])
);
}
// Regular request: render full template
return $this->render('booking/edit/participant.html.twig', $templateData);
}
/**
+10 -2
View File
@@ -121,10 +121,11 @@ trait HxTrait
* @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
* @param string|null $pushUrl Optional URL to push to browser history
*
* @return Response Response containing all rendered blocks with OOB context
*/
protected function htmxOobResponse(string $templateName, array $blockNames, array $context = []): Response
protected function htmxOobResponse(string $templateName, array $blockNames, array $context = [], ?string $pushUrl = null): Response
{
$html = '';
// Add a flag to the context so templates can conditionally add the hx-swap-oob attribute.
@@ -135,6 +136,13 @@ trait HxTrait
$html .= $this->renderBlockView($templateName, $blockName, $oobContext);
}
return new Response($html);
$response = new Response($html);
// Set HX-Push-Url header if URL is provided
if (null !== $pushUrl) {
$response->headers->set('HX-Push-Url', $pushUrl);
}
return $response;
}
}
+8 -14
View File
@@ -50,23 +50,17 @@
</button>
{% else %}
{% if mode == 'edit' %}
<button type="button"
class="button bg-button bg-button--secondary"
hx-get="{{ path('app_booking_edit_participant', {id: bookingId, index: index}) }}"
hx-target="#main-content"
hx-swap="innerHTML"
{{ qa_attribute('btn-edit-participant', index) }}>
<a href="{{ path('app_booking_edit_participant', {id: bookingId, index: index}) }}"
class="button bg-button bg-button--secondary"
{{ qa_attribute('btn-edit-participant', index) }}>
Bearbeiten
</button>
</a>
{% else %}
<button type="button"
class="button bg-button bg-button--secondary"
hx-get="{{ path('app_booking_create_step_2_participant', {index: index}) }}"
hx-target="#main-content"
hx-swap="innerHTML"
{{ qa_attribute('btn-edit-participant', index) }}>
<a href="{{ path('app_booking_create_step_2_participant', {index: index}) }}"
class="button bg-button bg-button--secondary"
{{ qa_attribute('btn-edit-participant', index) }}>
Bearbeiten
</button>
</a>
{% endif %}
{% endif %}
</div>
+12 -16
View File
@@ -360,23 +360,17 @@
<div class="flex justify-between mt-8">
{% if cancelRouteName is defined %}
<button type="button"
class="button bg-button bg-button--secondary"
hx-get="{{ path(cancelRouteName, cancelRouteParams|default({})) }}"
hx-target="#main-content"
hx-swap="innerHTML"
{{ qa_attribute('btn-cancel') }}>
<a href="{{ path(cancelRouteName, cancelRouteParams|default({})) }}"
class="button bg-button bg-button--secondary"
{{ qa_attribute('btn-cancel') }}>
Abbrechen
</button>
</a>
{% else %}
<button type="button"
class="button bg-button bg-button--secondary"
hx-get="{{ path('app_booking_create_step_2') }}"
hx-target="#main-content"
hx-swap="innerHTML"
{{ qa_attribute('btn-cancel') }}>
<a href="{{ path('app_booking_create_step_2') }}"
class="button bg-button bg-button--secondary"
{{ qa_attribute('btn-cancel') }}>
Abbrechen
</button>
</a>
{% endif %}
<button type="submit" class="button bg-button bg-button--secondary" {{ qa_attribute('btn-submit') }}>
Speichern
@@ -388,9 +382,10 @@
</div>
{% endblock %}
{# Sidebar summary with conditional OOB swap #}
{# Sidebar summary with conditional OOB swap - only render for HTMX requests #}
{% if htmx_oob_swap|default(false) %}
{% block booking_summary %}
<div id="booking-summary"{% if htmx_oob_swap|default(false) %} hx-swap-oob="true"{% endif %}>
<div id="booking-summary" hx-swap-oob="true">
{% include 'booking/_summary.html.twig' with {
'bookingCreateDto': bookingDto,
'participantCount': summaryData.participantsCount,
@@ -400,3 +395,4 @@
} %}
</div>
{% endblock %}
{% endif %}
@@ -0,0 +1,26 @@
{% extends 'layout.html.twig' %}
{% block content %}
{% include '_partials/_flashes.html.twig' %}
<h1>Neue Buchung</h1>
<div class="grid grid-cols-3 gap-8">
{# Main content area - participant form #}
<div id="main-content" class="col-span-2">
{% include 'booking/_participant_form.html.twig' %}
</div>
{# Sidebar summary #}
<div id="booking-summary">
{% include 'booking/_summary.html.twig' with {
'bookingCreateDto': bookingDto,
'participantCount': summaryData.participantsCount,
'groupedSelectedRooms': summaryData.groupedSelectedRooms,
'assignmentCounts': summaryData.assignmentCounts,
'pricingData': pricingData
} %}
</div>
</div>
{% include 'booking/_cancel_link.html.twig' %}
{% endblock %}
@@ -0,0 +1,26 @@
{% extends 'layout.html.twig' %}
{% block content %}
{% include '_partials/_flashes.html.twig' %}
<h1>Buchung bearbeiten</h1>
<div class="grid grid-cols-3 gap-8">
{# Main content area - participant form #}
<div id="main-content" class="col-span-2">
{% include 'booking/_participant_form.html.twig' %}
</div>
{# Sidebar summary #}
<div id="booking-summary">
{% include 'booking/_summary.html.twig' with {
'bookingCreateDto': bookingDto,
'participantCount': summaryData.participantsCount,
'groupedSelectedRooms': summaryData.groupedSelectedRooms,
'assignmentCounts': summaryData.assignmentCounts,
'pricingData': pricingData
} %}
</div>
</div>
{% include 'booking/_cancel_link.html.twig' %}
{% endblock %}