Skip to content

Commit 8dbddd3

Browse files
Add referrer functionality so we can go back to places sensibly
1 parent 69c4b8e commit 8dbddd3

File tree

6 files changed

+136
-15
lines changed

6 files changed

+136
-15
lines changed

app/assets/sass/_misc.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
1414
text-decoration: none;
1515
color: $nhsuk-secondary-text-color;
1616
}
17+
18+
19+
.app-image-flip-horizontal {
20+
-webkit-transform: scaleX(-1);
21+
transform: scaleX(-1);
22+
}

app/lib/utils/referrers.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// app/lib/utils/referrers.js
2+
3+
/**
4+
* Handle chained referrer URLs for back navigation
5+
* Allows deep linking while maintaining proper back navigation chains
6+
*/
7+
8+
/**
9+
* Parse referrer string into array of URLs
10+
* @private
11+
* @param {string|Array} referrer - Referrer string or array
12+
* @returns {Array} Array of referrer URLs
13+
*/
14+
const parseReferrerChain = (referrer) => {
15+
if (!referrer) return []
16+
if (Array.isArray(referrer)) return referrer
17+
return referrer.split(',').filter(Boolean)
18+
}
19+
20+
/**
21+
* Get destination from referrer chain, falling back to provided URL if no referrer
22+
* @param {string} url - Default URL to use if no referrer
23+
* @param {string} referrer - Referrer chain
24+
* @returns {string} URL to use for back link
25+
* @example
26+
* // In templates:
27+
* <a href="{{ '/default-path' | orReferrer(referrer) }}">Back</a>
28+
*/
29+
const orReferrer = function(url, referrer) {
30+
// Get currentUrl from context if available
31+
const currentUrl = this?.ctx?.currentUrl
32+
33+
const chain = parseReferrerChain(referrer)
34+
.filter(ref => ref !== currentUrl)
35+
36+
if (!chain.length) return url
37+
38+
// For single referrer, return it directly
39+
if (chain.length === 1) return chain[0]
40+
41+
// For multiple referrers, return last one with remaining chain as query param
42+
const remainingChain = chain.slice(0, -1)
43+
const destination = chain[chain.length - 1]
44+
45+
return `${destination}?referrer=${remainingChain.join(',')}`
46+
}
47+
48+
/**
49+
* Add referrer to URL as query parameter
50+
* @param {string} url - Base URL
51+
* @param {string} referrer - Referrer to append
52+
* @returns {string} URL with referrer query param
53+
* @example
54+
* // In templates:
55+
* <a href="{{ '/next-page' | withReferrer(referrer) }}">Continue</a>
56+
*/
57+
const withReferrer = (url, referrer) => {
58+
if (!referrer) return url
59+
return `${url}?referrer=${referrer}`
60+
}
61+
62+
/**
63+
* Append a URL to an existing referrer chain
64+
* @param {string|Array} existingReferrer - Existing referrer chain
65+
* @param {string} newUrl - URL to append
66+
* @returns {string} Combined referrer chain
67+
* @example
68+
* // In templates:
69+
* {% set updatedReferrer = referrer | appendReferrer(currentUrl) %}
70+
*/
71+
const appendReferrer = (existingReferrer, newUrl) => {
72+
if (!newUrl) return existingReferrer
73+
if (!existingReferrer) return newUrl
74+
75+
const chain = parseReferrerChain(existingReferrer)
76+
chain.push(newUrl)
77+
return chain.join(',')
78+
}
79+
80+
module.exports = {
81+
orReferrer,
82+
withReferrer,
83+
appendReferrer,
84+
}

app/locals.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
// app/locals.js
22

33
module.exports = (config) => (req, res, next) => {
4-
res.locals.serviceName = config.serviceName
5-
res.locals.url = req.path
6-
res.locals.flash = req.flash()
7-
res.locals.query = req.query
4+
5+
const locals = {
6+
serviceName: config.serviceName,
7+
currentUrl: req.path,
8+
flash: req.flash(),
9+
query: req.query,
10+
referrer: req.query.referrer
11+
}
12+
13+
// Assign all local variables at once
14+
Object.assign(res.locals, locals)
15+
816
next()
917
}

app/views/_includes/event-header.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@
3838

3939
<p>{{ event.timing.startTime | formatTime }} ({{ event.timing.duration }} minutes) - {{ clinic.date | formatDate }} ({{ clinic.date | formatRelativeDate }})<span class="nhsuk-u-margin-left-3"><a href="#">Reschedule appointment</a></span></p>
4040

41-
<p class="nhsuk-u-margin-bottom-4"><a href="/participants/{{ participant.id }}">View participant record</a></p>
41+
<p class="nhsuk-u-margin-bottom-4"><a href="{{ '/participants/' + participant.id | withReferrer(currentUrl) }}">View participant record</a></p>
4242

app/views/_includes/fake-images.njk

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{% set images = [
22
{
3-
view: "CC (cranio-caudal)",
3+
view: "CC (craniocaudal)",
44
side: "Right"
55
},
66
{
77
view: "MLO (mediolateral oblique)",
88
side: "Right"
99
},
1010
{
11-
view: "CC (cranio-caudal)",
11+
view: "CC (craniocaudal)",
1212
side: "Left"
1313
},
1414
{
@@ -45,6 +45,7 @@
4545
<div class="nhsuk-grid-column-one-quarter">
4646

4747
<div class="app-mammogram-image--placeholder">
48+
<img class="nhsuk-image__img {{ 'app-image-flip-horizontal' if params.side == 'Right' }}" src="/images/mammograms/nci-vol-9405-72.jpg" alt="Close-up of a person’s tummy showing a number of creases in the skin under their belly button. Shown on light brown skin.">
4849
<p>[IMAGE HERE]</p>
4950
</div>
5051
</div>
@@ -92,7 +93,7 @@
9293
text: "Is a repeat"
9394
},
9495
value: {
95-
text: params.repeat or "No"
96+
html: params.repeat or "No"
9697
},
9798
actions: {
9899
items: [
@@ -120,6 +121,19 @@
120121
}
121122
]
122123
}
124+
},
125+
{
126+
key: {
127+
text: "Details",
128+
classes: "foo"
129+
},
130+
value: {
131+
html: params.details or "Not provided"
132+
},
133+
actions: {
134+
items: []
135+
},
136+
classes: "nhsuk-summary-list--no-border"
123137
}
124138
]
125139
}) }}
@@ -134,7 +148,7 @@
134148
#}
135149
<div class="nhsuk-u-margin-bottom-4">
136150

137-
{% set timeNow = "" | now %}
151+
{% set timeNow = dayjs().subtract(1, 'minute') %}
138152

139153
{% set imageSides = images | groupby("side") %}
140154

@@ -147,18 +161,27 @@
147161
{% set imageSideHtml %}
148162
{% for image in sideImages %}
149163

150-
{% if not loop.first %}
151-
{% set randomSeconds = [3,4,5] | random %}
164+
{# {% if not loop.first %} #}
165+
{% set randomSeconds = [24,49,52] | random %}
152166
{% set timeNow = dayjs(timeNow).add(randomSeconds, 'seconds') %}
153-
{% endif %}
167+
{# {% endif %} #}
168+
169+
{% set detailsHtml -%}
170+
Captured:
171+
{{ timeNow | formatDate("D MMMM YYYY h:mm:ss")}}
172+
Accession:
173+
9583011-14/{{loop.index}}
174+
{%- endset %}
175+
154176

155177
{% set params = {
156178
view: image.view,
157179
side: image.side,
158180
repeat: image.repeat or "No",
159181
comment: image.comment,
160182
index: loop.index,
161-
timeNow: timeNow
183+
timeNow: timeNow,
184+
details: detailsHtml | trim | nl2br
162185
} %}
163186

164187
{{ makeImage(params) }}

app/views/participants/show.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
{% set back = {
12-
href: "/participants",
13-
text: "Back to participants"
12+
href: "/participants" | orReferrer(referrer),
13+
text: "Back to participants" if not referrer else "Back to appointment"
1414
} %}
1515

1616
{% block pageContent %}

0 commit comments

Comments
 (0)