From 7cdccefbd28bec2da1e9f71dbbcc54d825a41cf6 Mon Sep 17 00:00:00 2001 From: Nicholas Johnson Date: Thu, 21 Mar 2024 14:29:32 -0400 Subject: [PATCH] Add breadcrumbs to TCCP card detail page --- cfgov/tccp/jinja2/tccp/card.html | 5 +++++ cfgov/tccp/tests/test_views.py | 22 +++++++++++++++++++++- cfgov/tccp/views.py | 11 +++++++++++ cfgov/unprocessed/apps/tccp/js/index.js | 17 +++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/cfgov/tccp/jinja2/tccp/card.html b/cfgov/tccp/jinja2/tccp/card.html index 40c608048c..bc8bbb0d88 100644 --- a/cfgov/tccp/jinja2/tccp/card.html +++ b/cfgov/tccp/jinja2/tccp/card.html @@ -8,6 +8,11 @@ | Consumer Financial Protection Bureau {%- endblock title %} +{% block javascript scoped %} + {{ super() }} + +{% endblock %} + {% block css %} {{ super() }} diff --git a/cfgov/tccp/tests/test_views.py b/cfgov/tccp/tests/test_views.py index 52230461a7..b2d0e05a37 100644 --- a/cfgov/tccp/tests/test_views.py +++ b/cfgov/tccp/tests/test_views.py @@ -7,7 +7,7 @@ from django_htmx.middleware import HtmxMiddleware from tccp.models import CardSurveyData -from tccp.views import CardListView, LandingPageView +from tccp.views import CardDetailView, CardListView, LandingPageView from .baker import baker @@ -105,3 +105,23 @@ def test_invalid_json_query_renders_error(self): def test_invalid_html_query_renders_empty_page(self): response = self.make_request("?credit_tier=foo") self.assertContains(response, "There are no results for your search.") + + +class CardDetailViewTests(TestCase): + @classmethod + def setUpTestData(cls): + baker.make( + CardSurveyData, + slug="test-card", + product_name="Test Card", + ) + + def make_request(self, slug): + view = CardDetailView.as_view() + request = RequestFactory().get("/") + return view(request, **{"slug": slug}) + + def test_get(self): + response = self.make_request("test-card") + self.assertContains(response, "Test Card") + self.assertContains(response, "m-breadcrumb") diff --git a/cfgov/tccp/views.py b/cfgov/tccp/views.py index 4fbad67ad4..73db9ab205 100644 --- a/cfgov/tccp/views.py +++ b/cfgov/tccp/views.py @@ -139,3 +139,14 @@ class CardDetailView(FlaggedViewMixin, DetailView): model = CardSurveyData context_object_name = "card" template_name = "tccp/card.html" + breadcrumb_items = CardListView.breadcrumb_items + [ + { + "title": CardListView.heading, + "href": reverse_lazy("tccp:cards"), + } + ] + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["breadcrumb_items"] = self.breadcrumb_items + return context diff --git a/cfgov/unprocessed/apps/tccp/js/index.js b/cfgov/unprocessed/apps/tccp/js/index.js index 76fe38049e..d5a11fa0f8 100644 --- a/cfgov/unprocessed/apps/tccp/js/index.js +++ b/cfgov/unprocessed/apps/tccp/js/index.js @@ -1,11 +1,15 @@ import { attach } from '@cfpb/cfpb-atomic-component'; +import webStorageProxy from '../../../js/modules/util/web-storage-proxy'; + /** * Initialize some things. */ function init() { // Attach "show more" click handler attach('show-more', 'click', handleShowMore); + // Make the breadcrumb on the details page go back to a filtered list + updateBreadcrumb(); } /** @@ -22,6 +26,19 @@ function handleShowMore(event) { event.target.classList.add('u-hidden'); } +/** + * Update the breadcrumb on the card details page to point back to the filtered + * list of cards the user came from. We have to do this client-side to prevent + * Akamai from caching the page with a breadcrumb to a filtered list. + */ +function updateBreadcrumb() { + const breadcrumb = document.querySelector('.m-breadcrumbs_crumb:last-child'); + if (breadcrumb.innerText === 'Customize for your situation') { + breadcrumb.href = + webStorageProxy.getItem('tccp-filter-path') || breadcrumb.href; + } +} + window.addEventListener('load', () => { init(); });