Skip to content

Commit 4f7dd3f

Browse files
committed
Skip the formats page if no columns are mapped to attributes whose types require formats
1 parent bb93e20 commit 4f7dd3f

File tree

7 files changed

+158
-10
lines changed

7 files changed

+158
-10
lines changed

lib/importer/src/dudk/backend.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ exports.SessionSetFile = (sid, filename) => {
5656
sheetNames: Array.from(Object.keys(wb.Sheets)),
5757
jobs: new Map(),
5858
// State we store for the frontend (we don't do anything with it, just store it and spit it back on demand)
59+
attributeDefs: [], // A list of objects with {name:string, required:boolean, type:string} defining the attributes we output
5960
headerRanges: {}, // A map from sheetname to the header range
6061
footerRanges: {}, // A map from sheetname to the footer range
6162
mappingRules: {}, // A map from column index to attribute name
@@ -68,14 +69,9 @@ exports.SessionSetFile = (sid, filename) => {
6869
/// Storage of UI state for the frontend
6970
///
7071

71-
exports.SessionGetMappingRules = (sid) => {
72-
let session = sessionStore.get(sid);
73-
return session.mappingRules;
74-
};
75-
76-
exports.SessionSetMappingRules = (sid, rules) => {
77-
sessionStore.apply(sid, (s) => { s.mappingRules = rules })
78-
};
72+
exports.SessionSetAttributes = (sid, attrDefs) => {
73+
sessionStore.apply(sid, (s) => { s.attribudeDefs = attrDefs })
74+
}
7975

8076
exports.SessionSetHeaderRange = (sid, range) => {
8177
assert(range != null, "Null range passed to SessionSetHeaderRange");
@@ -100,6 +96,15 @@ exports.SessionGetFooterRange = (sid, sheetName) => {
10096
return session.footerRanges[sheetName]
10197
};
10298

99+
exports.SessionGetMappingRules = (sid) => {
100+
let session = sessionStore.get(sid);
101+
return session.mappingRules;
102+
};
103+
104+
exports.SessionSetMappingRules = (sid, rules) => {
105+
sessionStore.apply(sid, (s) => { s.mappingRules = rules })
106+
};
107+
103108
exports.SessionGetFormatChoices = (sid) => {
104109
let session = sessionStore.get(sid);
105110
return session.formatChoices;
@@ -109,6 +114,35 @@ exports.SessionSetFormatChoices = (sid, choices) => {
109114
sessionStore.apply(sid, (s) => { s.formatChoices = choices })
110115
};
111116

117+
///
118+
/// Frontend helpers
119+
///
120+
121+
122+
// Returns an array of column indexes that need a format to be chosen - requires
123+
// that SessionSetAttributes and SessionSetMappingRules have both happened so we
124+
// have the required information.
125+
exports.SessionGetColumnsNeedingFormats = (sid) => {
126+
const session = sessionStore.get(sid);
127+
const attributes = session.attribudeDefs;
128+
const mappings = session.mappingRules;
129+
const supportedTypes = attributeTypes.supportedTypes;
130+
131+
let columnsNeedingFormats = new Array();
132+
133+
Object.entries(mappings).forEach((entry) => {
134+
const [colIdx, columnAttribute] = entry;
135+
const columnDef = attributes.find((attr) => attr.name == columnAttribute);
136+
const columnTypeName = columnDef.type;
137+
const columnType = supportedTypes.get(columnTypeName);
138+
if (columnType.formats) {
139+
columnsNeedingFormats.push(colIdx);
140+
}
141+
});
142+
143+
return columnsNeedingFormats;
144+
};
145+
112146
///
113147
/// Information about the loaded input file
114148
///

lib/importer/src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const multer = require("multer");
22
const conf = require("./config.js");
33
const fs = require("node:fs");
44
const path = require("node:path");
5+
const backend = require("./dudk/backend.js");
56
const session_lib = require("./session.js");
67
const sheets_lib = require("./dudk/sheets.js");
78
const tpl_functions = require("./functions.js")
@@ -434,6 +435,17 @@ exports.Initialise = (config, router, prototypeKit) => {
434435
delete request.session.data[IMPORTER_ERROR_EXTRA_KEY]
435436
delete request.session.data[IMPORTER_ERROR_DATA_KEY]
436437
}
438+
439+
// If we are processing mappings, AND we are given two next-page paths
440+
// to consider, then we should select the other next page if there are
441+
// no format choices to be made
442+
if ("other" in request.query) {
443+
const columnsNeedingFormats = backend.SessionGetColumnsNeedingFormats(session.backendSid);
444+
if(columnsNeedingFormats.length == 0) {
445+
const otherPage = decodeURIComponent(request.query.other)
446+
if (otherPage) { request.query.next = request.query.other }
447+
}
448+
}
437449
}
438450

439451
// Ensure the session is persisted. Currently in session, eventually another way

lib/importer/src/session.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Session {
1414
backend.SessionSetFile(this.backendSid, p.filename);
1515
}
1616

17+
backend.SessionSetAttributes(this.backendSid, p.fields);
1718
this.fields = p.fields;
1819
this.sheet = p.sheet ?? "";
1920
}

lib/importer/templates/format.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{% extends "layouts/main.html" %}
2+
3+
{% block pageTitle %} {{ serviceName }} – GOV.UK Prototype Kit {% endblock %}
4+
5+
{% block beforeContent %}
6+
{{ govukBackLink({ text: "Back", href: "javascript:window.history.back()" }) }}
7+
{% endblock %}
8+
9+
{% from "importer/macros/format_picker.njk" import dudkFormatPicker %}
10+
11+
{% block content %}
12+
<div class="govuk-grid-row">
13+
<div class="govuk-grid-column-full">
14+
<h1 class="govuk-heading-l">Identify columns</h1>
15+
<p class="govuk-body">
16+
Select a format for any fields that need it. FIXME: Write better text here...
17+
</p>
18+
19+
<h2 class="govuk-heading-m">{{sheet}}</h2>
20+
21+
<form action="{{ importerMapDataPath('/review') }}" method="post">
22+
23+
{{ dudkFormatPicker({data: data}) }}
24+
25+
<div class="govuk-grid-column-two-thirds">
26+
<div class="govuk-button-group">
27+
{{ govukButton({ text: "Submit" }) }}
28+
</div>
29+
</div>
30+
31+
</form>
32+
</div>
33+
</div>
34+
{% endblock %}

lib/importer/templates/mapping.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h1 class="govuk-heading-l">Identify columns</h1>
2020

2121
<h2 class="govuk-heading-m">{{sheet}}</h2>
2222

23-
<form action="{{ importerMapDataPath('/format') }}" method="post">
23+
<form action="{{ importerMapDataPath('/format','/review') }}" method="post">
2424

2525
{{ dudkFieldMapper({data: data}) }}
2626

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{% extends "layouts/main.html" %}
2+
3+
{% block pageTitle %} {{ serviceName }} – GOV.UK Prototype Kit {% endblock %}
4+
5+
{% block beforeContent %}
6+
{{
7+
govukBreadcrumbs({
8+
items: [
9+
{
10+
text: "Home",
11+
href: "/"
12+
},
13+
{
14+
text: "Upload spreadsheet",
15+
href: "/upload"
16+
},
17+
{
18+
text: "Select sheet",
19+
href: "/select_sheet"
20+
},
21+
{
22+
text: "Select header row",
23+
href: "/select_header_row"
24+
},
25+
{
26+
text: "Select footer row",
27+
href: "/select_footer_row"
28+
},
29+
{
30+
text: "Identify columns",
31+
href: "/mapping"
32+
},
33+
{
34+
text: "Choose formats",
35+
href: "/format"
36+
}
37+
]
38+
})
39+
}}
40+
{% endblock %}
41+
42+
{% from "importer/macros/format_picker.njk" import dudkFormatPicker %}
43+
44+
{% block content %}
45+
<div class="govuk-grid-row">
46+
<div class="govuk-grid-column-full">
47+
<h1 class="govuk-heading-l">Choose formats</h1>
48+
<p class="govuk-body">
49+
Select a format for any fields that need it. FIXME: Write better text here...
50+
</p>
51+
52+
<h2 class="govuk-heading-m">{{sheet}}</h2>
53+
54+
<form action="{{ importerMapDataPath('/review') }}" method="post">
55+
56+
{{ dudkFormatPicker({data: data}) }}
57+
58+
<div class="govuk-grid-column-two-thirds">
59+
<div class="govuk-button-group">
60+
{{ govukButton({ text: "Submit" }) }}
61+
</div>
62+
</div>
63+
64+
</form>
65+
</div>
66+
</div>
67+
{% endblock %}

prototypes/basic/app/views/mapping.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ <h1 class="govuk-heading-l">Identify columns</h1>
4949

5050
<h2 class="govuk-heading-m">{{sheet}}</h2>
5151

52-
<form action="{{ importerMapDataPath('/format') }}" method="post">
52+
<form action="{{ importerMapDataPath('/format','/review') }}" method="post">
5353

5454
{{ dudkFieldMapper({data: data}) }}
5555

0 commit comments

Comments
 (0)