Skip to content

Commit eeaa212

Browse files
authored
Fix errors when calling data functions (#208)
1 parent 6dd45ba commit eeaa212

5 files changed

Lines changed: 83 additions & 33 deletions

File tree

lib/importer/src/filters.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ const slugify = (content) => {
1515
return content.toLowerCase().replace(/[^\w\s']|_/g, " ").replace(/\s+/g, "-");
1616
}
1717

18-
module.exports = { currency, slugify }
18+
const pluralize = (count, singular, plural) => {
19+
if (count == 1) return singular;
20+
return plural
21+
}
22+
23+
module.exports = { currency, slugify, pluralize }

lib/importer/src/functions.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ const data_sum = (data, column) => {
173173

174174
const idx = headers.findIndex((x) => x.name == column)
175175
if (idx == -1) {
176-
return "No data found"
176+
return { value: "No data found" }
177177
}
178178

179179
let numbers = mapResults.resultRecords
180180
.filter((x) => x !== undefined && x[idx] !== undefined)
181181
.map((x) => parseNumberFromString(x[idx]))
182182

183183

184-
return numbers.reduce((acc, i) => acc + i, 0)
184+
return { value: numbers.reduce((acc, i) => acc + i, 0), count: numbers.length }
185185
}
186186

187187
const data_avg = (data, column) => {
@@ -191,21 +191,27 @@ const data_avg = (data, column) => {
191191

192192
const idx = headers.findIndex((x) => x.name == column)
193193
if (idx == -1) {
194-
return "No data found"
194+
return { value: "No data found" }
195195
}
196196

197197
const numbers = mapResults.resultRecords
198198
.filter((x) => x !== undefined && x[idx] !== undefined)
199199
.map((x) => parseNumberFromString(x[idx]))
200200

201+
201202
const avg = numbers.reduce((acc, i) => acc + i, 0) / numbers.length
202203
if (Number.isNaN(avg)) {
203-
return "0"
204+
return { value: "0", count: 0 }
204205
}
205-
return avg
206+
return { value: avg, count: numbers.length }
206207
}
207208

208209
const parseNumberFromString = (s) => {
210+
if (typeof s == "number") {
211+
return s
212+
}
213+
if (s === undefined || s == null) return null;
214+
209215
const parsed = s.match(/([0-9]*\.[0-9]+|[0-9]+)/)
210216
if (parsed == null) {
211217
return 0

lib/importer/templates/review.html

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
{% extends "layouts/main.html" %}
22

33
{% block pageTitle %} {{ serviceName }} – GOV.UK Prototype Kit {% endblock %}
4+
{% block beforeContent %}
5+
6+
7+
8+
{{
9+
govukBreadcrumbs({
10+
items: [
11+
{
12+
text: "Home",
13+
href: "/"
14+
},
15+
{
16+
text: "Upload spreadsheet",
17+
href: "/upload"
18+
},
19+
{
20+
text: "Select sheet",
21+
href: "/select_sheet"
22+
},
23+
{
24+
text: "Select header row",
25+
href: "/select_header_row"
26+
},
27+
{
28+
text: "Select footer row",
29+
href: "/select_footer_row"
30+
},
31+
{
32+
text: "Identify columns",
33+
href: "/mapping"
34+
},
35+
{
36+
text: "Review data",
37+
href: "/review"
38+
}
39+
]
40+
})
41+
}}
42+
{% endblock %}
443

544
{% block content %}
645
{% set results = importerMappedData(data) %}
@@ -19,47 +58,44 @@ <h1 class="govuk-heading-l">Review your data</h1>
1958
<thead class="govuk-table__head">
2059
<tr class="govuk-table__row">
2160
<th scope="col" class="govuk-table__header">Row</th>
61+
<th scope="col" class="govuk-table__header">Field</th>
2262
<th scope="col" class="govuk-table__header">Error</th>
2363
</tr>
2464
</thead>
2565
<tbody class="govuk-table__body">
26-
{% for idx, error in results.errors %}
27-
<tr class="govuk-table__row">
28-
<td class="govuk-table__cell"> {{idx}} </td>
29-
<td class="govuk-table__cell"> {{error}} </td>
30-
</tr>
66+
{% for idx, errors in results.errors %}
67+
{% for e in errors %}
68+
<tr class="govuk-table__row">
69+
<td class="govuk-table__cell"> {{idx}} </td>
70+
<td class="govuk-table__cell"> {{e.field}} </td>
71+
<td class="govuk-table__cell"> {{e.message}} </td>
72+
</tr>
73+
{% endfor %}
3174
{% endfor %}
3275
</tbody>
3376
</table>
3477
{% endif %}
3578

3679
{% if results.warningCount != 0 %}
37-
<table class="govuk-table">
38-
<caption class="govuk-table__caption govuk-table__caption--m">Warnings</caption>
39-
<thead class="govuk-table__head">
40-
<tr class="govuk-table__row">
41-
<th scope="col" class="govuk-table__header">Row</th>
42-
<th scope="col" class="govuk-table__header">Warning</th>
43-
</tr>
44-
</thead>
45-
<tbody class="govuk-table__body">
80+
<p class="govuk-body">
81+
<ul class="govuk-list govuk-list--bullet">
4682
{% for idx, warning in results.warnings %}
47-
<tr class="govuk-table__row">
48-
<td class="govuk-table__cell"> {{idx}} </td>
49-
<td class="govuk-table__cell"> {{warning}} </td>
50-
</tr>
83+
<li>Row {{ idx }}: {{ warning }}</li>
5184
{% endfor %}
52-
</tbody>
53-
</table>
85+
</ul>
86+
</p>
5487
{% endif %}
5588

5689
<p class="govuk-body">
57-
The uploaded data contains the following data:
90+
The uploaded data contains the following data:
91+
92+
{% set sum_result = data_sum(data, 'Salary')%}
93+
{% set avg_result = data_avg(data, 'Salary') %}
5894

5995
<ul class="govuk-list govuk-list--bullet">
6096
<li><strong>{{ results.totalCount }}</strong> rows of data</li>
61-
<li>a total salary commitment from the 'Salary' column of <strong>{{ data_sum(data, 'Salary') | currency }}</strong>.</li>
62-
<li>an average salary from the 'Salary' column of <strong>{{ data_avg(data, 'Salary') | currency }}</strong>.</li>
97+
<li>a total salary commitment from the 'Salary' column of <strong>{{ sum_result.value | currency }}</strong> from {{ sum_result.count }} {{ sum_result.count | pluralize("value", "values") }}.</li>
98+
<li>an average salary from the 'Salary' column of <strong>{{ avg_result.value | currency }}</strong> from {{avg_result.count}} {{ avg_result.count | pluralize("value", "values") }}.</li>
6399
</ul>
64100
</p>
65101

prototypes/basic/app/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
{
3030
"name": "Salary",
31-
"type": "text",
31+
"type": "number",
3232
"required": false
3333
},
3434
{

prototypes/basic/app/views/review.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ <h1 class="govuk-heading-l">Review your data</h1>
8787
{% endif %}
8888

8989
<p class="govuk-body">
90-
The uploaded data contains the following data:
90+
The uploaded data contains the following data:
91+
92+
{% set sum_result = data_sum(data, 'Salary')%}
93+
{% set avg_result = data_avg(data, 'Salary') %}
9194

9295
<ul class="govuk-list govuk-list--bullet">
9396
<li><strong>{{ results.totalCount }}</strong> rows of data</li>
94-
<li>a total salary commitment from the 'Salary' column of <strong>{{ data_sum(data, 'Salary') | currency }}</strong>.</li>
95-
<li>an average salary from the 'Salary' column of <strong>{{ data_avg(data, 'Salary') | currency }}</strong>.</li>
97+
<li>a total salary commitment from the 'Salary' column of <strong>{{ sum_result.value | currency }}</strong> from {{ sum_result.count }} {{ sum_result.count | pluralize("value", "values") }}.</li>
98+
<li>an average salary from the 'Salary' column of <strong>{{ avg_result.value | currency }}</strong> from {{avg_result.count}} {{ avg_result.count | pluralize("value", "values") }}.</li>
9699
</ul>
97100
</p>
98101

0 commit comments

Comments
 (0)