Skip to content

Commit

Permalink
Add breadcrumbs to TCCP card detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
niqjohnson committed Mar 25, 2024
1 parent c716050 commit aa3c34a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cfgov/tccp/jinja2/tccp/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
| Consumer Financial Protection Bureau
{%- endblock title %}

{% block javascript scoped %}
{{ super() }}
<script src="{{ static( 'apps/tccp/js/index.js' ) }}"></script>
{% endblock %}

{% block css %}
{{ super() }}
<link rel="stylesheet" href="{{ static('apps/tccp/css/main.css') }}">
Expand Down
22 changes: 21 additions & 1 deletion cfgov/tccp/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
11 changes: 11 additions & 0 deletions cfgov/tccp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions cfgov/unprocessed/apps/tccp/js/index.js
Original file line number Diff line number Diff line change
@@ -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();
}

/**
Expand All @@ -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();
});

0 comments on commit aa3c34a

Please sign in to comment.