Skip to content

Commit baae8d5

Browse files
Add filters for attendance status
1 parent 441b404 commit baae8d5

File tree

3 files changed

+89
-24
lines changed

3 files changed

+89
-24
lines changed

app/lib/utils/status.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,22 @@ const getStatusDescription = (status) => {
4545
return descriptions[status] || status;
4646
}
4747

48+
49+
const filterEventsByStatus = (events, filter) => {
50+
switch(filter) {
51+
case 'scheduled':
52+
return events.filter(e => e.status === 'scheduled');
53+
case 'checked-in':
54+
return events.filter(e => e.status === 'checked_in');
55+
case 'attended':
56+
return events.filter(e => ['attended', 'attended_not_screened'].includes(e.status));
57+
default:
58+
return events;
59+
}
60+
}
61+
4862
module.exports = {
4963
getStatusTagColour,
50-
getStatusDescription
64+
getStatusDescription,
65+
filterEventsByStatus
5166
};

app/routes/clinics.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ module.exports = router => {
4040
res.render('clinics/today');
4141
});
4242

43-
// Single clinic view
44-
router.get('/clinics/:id', (req, res) => {
45-
const clinicData = getClinicData(req.session.data, req.params.id);
46-
47-
if (!clinicData) {
48-
res.redirect('/clinics');
49-
return;
50-
}
51-
52-
res.render('clinics/show', {
53-
clinicId: req.params.id,
54-
clinic: clinicData.clinic,
55-
events: clinicData.events,
56-
unit: clinicData.unit,
57-
formatDate: (date) => dayjs(date).format('D MMMM YYYY'),
58-
formatTime: (date) => dayjs(date).format('HH:mm')
59-
});
60-
});
61-
6243
// View participant within clinic context
6344
router.get('/clinics/:clinicId/participants/:participantId', (req, res) => {
6445
const participant = req.session.data.participants.find(p => p.id === req.params.participantId);
@@ -82,6 +63,53 @@ module.exports = router => {
8263
});
8364
});
8465

66+
function filterEvents(events, filter) {
67+
switch(filter) {
68+
case 'scheduled':
69+
return events.filter(e => e.status === 'scheduled');
70+
case 'checked-in':
71+
console.log('filtering checked in')
72+
return events.filter(e => e.status === 'checked_in');
73+
case 'attended':
74+
return events.filter(e => ['attended', 'attended_not_screened'].includes(e.status));
75+
default:
76+
return events;
77+
}
78+
}
8579

80+
// Single clinic view
81+
const VALID_FILTERS = ['scheduled', 'checked-in', 'attended', 'all'];
82+
83+
// Support both /clinics/:id and /clinics/:id/:filter
84+
router.get(['/clinics/:id', '/clinics/:id/:filter'], (req, res) => {
85+
const clinicData = getClinicData(req.session.data, req.params.id);
86+
87+
if (!clinicData) {
88+
return res.redirect('/clinics');
89+
}
90+
91+
// Check filter from either URL param or query string
92+
let filter = req.params.filter || req.query.filter || 'all';
93+
94+
// Validate filter
95+
if (!VALID_FILTERS.includes(filter)) {
96+
return res.redirect(`/clinics/${req.params.id}`);
97+
}
98+
99+
console.log(`Events before: ${clinicData.events.length}`)
100+
const filteredEvents = filterEvents(clinicData.events, filter);
101+
console.log(`Events after: ${filteredEvents.length}`)
102+
103+
res.render('clinics/show', {
104+
clinicId: req.params.id,
105+
clinic: clinicData.clinic,
106+
allEvents: clinicData.events,
107+
filteredEvents: filteredEvents,
108+
unit: clinicData.unit,
109+
currentFilter: filter,
110+
formatDate: (date) => dayjs(date).format('D MMMM YYYY'),
111+
formatTime: (date) => dayjs(date).format('HH:mm')
112+
});
113+
});
86114

87115
};

app/views/clinics/show.html

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,39 @@
33

44
{% block pageContent %}
55
{% set unit = data.breastScreeningUnits | findById(clinic.breastScreeningUnitId) %}
6-
{% set events = data.events | getClinicEvents(clinicId) %}
6+
{# {% set events = data.events | getClinicEvents(clinicId) %} #}
7+
{% set events = allEvents %}
78

89
<h1 class="nhsuk-heading-l">
910
<span class="nhsuk-caption-l">{{ unit.name }}</span>
1011
{{ clinic.serviceType | sentenceCase }} clinic on {{ clinic.date | formatDate }}
1112
</h1>
1213

13-
{% if events.length === 0 %}
14+
<nav class="nhsuk-tabs">
15+
<ul class="nhsuk-tabs__list">
16+
{% for filter in [
17+
{ id: 'all', label: 'All participants' },
18+
{ id: 'scheduled', label: 'Scheduled' },
19+
{ id: 'checked-in', label: 'Checked in' },
20+
{ id: 'attended', label: 'Attended' }
21+
] %}
22+
<li class="nhsuk-tabs__list-item">
23+
<a class="nhsuk-tabs__tab {% if currentFilter === filter.id %}nhsuk-tabs__tab--selected{% endif %}"
24+
href="/clinics/{{ clinicId }}/{{ filter.id if filter.id !== 'all' }}">
25+
{{ filter.label }}
26+
<span class="nhsuk-tag nhsuk-tag--grey">
27+
{{ events | filterEventsByStatus(filter.id) | length }}
28+
</span>
29+
</a>
30+
</li>
31+
{% endfor %}
32+
</ul>
33+
</nav>
34+
35+
{% if filteredEvents.length === 0 %}
1436
<p>No participants scheduled for this clinic.</p>
1537
{% else %}
16-
<p>{{ events.length }} scheduled participants</p>
38+
<p>{{ filteredEvents.length }} scheduled participants</p>
1739

1840
<table class="nhsuk-table">
1941
<thead class="nhsuk-table__head">
@@ -27,7 +49,7 @@ <h1 class="nhsuk-heading-l">
2749
</tr>
2850
</thead>
2951
<tbody class="nhsuk-table__body">
30-
{% for event in events %}
52+
{% for event in filteredEvents %}
3153
{% set participant = data.participants | findById(event.participantId) %}
3254
<tr>
3355
<td class="nhsuk-table__cell">{{ event.statusHistory[0].timestamp | formatTime }}</td>

0 commit comments

Comments
 (0)