|
1 | 1 | import json |
2 | | -from urllib.parse import unquote |
| 2 | +from urllib.parse import unquote_plus |
3 | 3 |
|
4 | | -from flask import current_app, url_for |
| 4 | +from flask import current_app |
5 | 5 | from markupsafe import Markup |
6 | 6 |
|
7 | | -from mavis.reporting.helpers.mavis_helper import mavis_public_url |
| 7 | +FALLBACK_ITEMS = [ |
| 8 | + {"path": "/programmes", "title": "Programmes"}, |
| 9 | + {"path": "/patients", "title": "Children"}, |
| 10 | + {"path": "/sessions", "title": "Sessions"}, |
| 11 | + {"path": "/vaccines", "title": "Vaccines"}, |
| 12 | + {"path": "/consent-forms", "title": "Unmatched responses"}, |
| 13 | + {"path": "/school-moves", "title": "School moves"}, |
| 14 | + {"path": "/imports", "title": "Imports"}, |
| 15 | + {"path": "/team", "title": "Your team"}, |
| 16 | +] |
8 | 17 |
|
9 | 18 |
|
10 | | -def parse_navigation_counts_cookie(request): |
11 | | - nav_counts = {} |
12 | | - if cookie_value := request.cookies.get("mavis_navigation_counts"): |
| 19 | +def build_navigation_items(request): |
| 20 | + items = FALLBACK_ITEMS |
| 21 | + if cookie_value := request.cookies.get("mavis_navigation_items"): |
13 | 22 | try: |
14 | | - decoded_value = unquote(cookie_value) |
15 | | - nav_counts = json.loads(decoded_value) |
16 | | - current_app.logger.info(f"Navigation counts from cookie: {nav_counts}") |
| 23 | + decoded_value = unquote_plus(cookie_value) |
| 24 | + if parsed := json.loads(decoded_value): |
| 25 | + items = parsed |
17 | 26 | except (json.JSONDecodeError, ValueError): |
18 | 27 | current_app.logger.warning( |
19 | | - f"Failed to parse navigation counts cookie: {cookie_value}" |
| 28 | + f"Failed to parse navigation items cookie: {cookie_value}" |
20 | 29 | ) |
21 | | - else: |
22 | | - current_app.logger.info("No mavis_navigation_counts cookie found") |
23 | | - return nav_counts |
24 | | - |
25 | | - |
26 | | -def build_nav_item(href, text, nav_counts, active=False, count_key=None): |
27 | | - item = {"href": href, "active": active} |
28 | 30 |
|
29 | | - if count_key and (count := nav_counts.get(count_key)) is not None: |
30 | | - badge = ( |
31 | | - '<span class="app-count">' |
32 | | - '<span class="nhsuk-u-visually-hidden"> (</span>' |
33 | | - f"{count}" |
34 | | - '<span class="nhsuk-u-visually-hidden">)</span>' |
35 | | - "</span>" |
36 | | - ) |
37 | | - item["html"] = Markup(f"{text}{badge}") |
38 | | - item["classes"] = "app-header__navigation-item--with-count" |
39 | | - else: |
40 | | - item["text"] = text |
41 | | - |
42 | | - return item |
| 31 | + nav_items = [] |
| 32 | + for item in items: |
| 33 | + nav_item = {"href": item["path"], "text": item["title"]} |
| 34 | + |
| 35 | + if (count := item.get("count")) is not None: |
| 36 | + badge = ( |
| 37 | + '<span class="app-count">' |
| 38 | + '<span class="nhsuk-u-visually-hidden"> (</span>' |
| 39 | + f"{count}" |
| 40 | + '<span class="nhsuk-u-visually-hidden">)</span>' |
| 41 | + "</span>" |
| 42 | + ) |
| 43 | + nav_item["html"] = Markup(f"{item['title']}{badge}") |
| 44 | + nav_item["classes"] = "app-header__navigation-item--with-count" |
| 45 | + del nav_item["text"] |
43 | 46 |
|
| 47 | + nav_items.append(nav_item) |
44 | 48 |
|
45 | | -def build_navigation_items(request): |
46 | | - nav_counts = parse_navigation_counts_cookie(request) |
47 | | - return [ |
48 | | - build_nav_item(url_for("main.dashboard"), "Reports", nav_counts, active=True), |
49 | | - build_nav_item( |
50 | | - mavis_public_url(current_app, "/sessions"), "Sessions", nav_counts |
51 | | - ), |
52 | | - build_nav_item( |
53 | | - mavis_public_url(current_app, "/patients"), "Children", nav_counts |
54 | | - ), |
55 | | - build_nav_item( |
56 | | - mavis_public_url(current_app, "/consent-forms"), |
57 | | - "Unmatched responses", |
58 | | - nav_counts, |
59 | | - count_key="unmatched_consent_responses", |
60 | | - ), |
61 | | - build_nav_item( |
62 | | - mavis_public_url(current_app, "/school-moves"), |
63 | | - "School moves", |
64 | | - nav_counts, |
65 | | - count_key="school_moves", |
66 | | - ), |
67 | | - build_nav_item( |
68 | | - mavis_public_url(current_app, "/vaccines"), "Vaccines", nav_counts |
69 | | - ), |
70 | | - build_nav_item( |
71 | | - mavis_public_url(current_app, "/imports"), |
72 | | - "Imports", |
73 | | - nav_counts, |
74 | | - count_key="imports", |
75 | | - ), |
76 | | - build_nav_item( |
77 | | - mavis_public_url(current_app, "/team"), "Your team", nav_counts |
78 | | - ), |
79 | | - ] |
| 49 | + return nav_items |
0 commit comments