From 69e5f8759bed8f54909cbad739070f43529fdda4 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 14 Oct 2024 13:33:02 +0100 Subject: [PATCH] [Scorecards] Added council suggestions for comparisson tool --- scoring/static/scoring/js/main.js | 38 ++++++++++++++++++-------- scoring/templates/scoring/council.html | 29 ++++++++++++++------ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/scoring/static/scoring/js/main.js b/scoring/static/scoring/js/main.js index 92bfde1c..12484223 100644 --- a/scoring/static/scoring/js/main.js +++ b/scoring/static/scoring/js/main.js @@ -144,19 +144,35 @@ forEachElement('.js-location-compare-autocomplete', function(input){ autoFirst: true } ); + input.parentNode.addEventListener('awesomplete-selectcomplete', function(data){ - var council = findItem(councils, {'name': data.text }); - var sp = new URLSearchParams(window.location.search) - var comparisons = sp.getAll('comparisons'); + handleCouncilSelection(data.text); + }); +}); - if (!comparisons.includes(council.slug)) { - comparisons.push(council.slug); - } - sp.delete('comparisons'); - for (comparison of comparisons.values()) { - sp.append('comparisons', comparison); - } - window.location.href = window.location.pathname + '?' + sp.toString() + '#questions'; +// Function to handle selection from both autocomplete and suggestions +function handleCouncilSelection(councilName) { + var council = findItem(councils, {'name': councilName}); + var sp = new URLSearchParams(window.location.search); + var comparisons = sp.getAll('comparisons'); + + if (!comparisons.includes(council.slug)) { + comparisons.push(council.slug); + } + sp.delete('comparisons'); + for (comparison of comparisons.values()) { + sp.append('comparisons', comparison); + } + window.location.href = window.location.pathname + '?' + sp.toString() + '#questions'; +} + +// Handling suggestion clicks +forEachElement('.js-council-compare-suggestions', function(suggestion) { + suggestion.addEventListener('click', function(event) { + event.preventDefault(); + var councilSlug = this.dataset.slug; + var council = findItem(councils, {'slug': councilSlug}); // Get the council by slug + handleCouncilSelection(council.name); // Pass the name to the selection handler }); }); diff --git a/scoring/templates/scoring/council.html b/scoring/templates/scoring/council.html index f658703e..00cc06ef 100644 --- a/scoring/templates/scoring/council.html +++ b/scoring/templates/scoring/council.html @@ -154,19 +154,32 @@

Action Scorecard: {{ council.short_name }}

-
+

Choose {% include "scoring/includes/scoring-group-name.html" with group=council.get_scoring_group.slug plural=1 %} to compare

- {% if comparisons|length > 2 %} + {% if comparisons|length > 2 %}

You have reached the maximum number of councils that can be compared.

- {% else %} - - {% endif %} - {% if comparisons %} + {% else %} + +

Based on we suggest the following councils:

+ + {% endif %} + {% if comparisons %} +
{% for comparison in comparisons %} {{ comparison.council.name }} @@ -178,8 +191,8 @@

Choose {% include "scoring/includes/scoring-group-name.htm

-
-
+
+

Filter questions in the table