Skip to content

Commit 43ec559

Browse files
committed
Update UI to reflect new distinction between safe/maybe/unsafe formats and types
1 parent 5f448a1 commit 43ec559

3 files changed

Lines changed: 56 additions & 35 deletions

File tree

lib/importer/nunjucks/importer/macros/format_picker.njk

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,22 @@
7979
<td class="govuk-table__cell govuk-table__cell--numeric">
8080
{% if possibleFormats %}
8181
<select class="govuk-select" style="float: right;" name="format-{{h.index}}">
82-
{% for format in possibleFormats %}
82+
<optgroup label="Matches every row">
83+
{% for format in possibleFormats[0] %}
8384
<option value="{{format.name}}"
84-
{% if format.name == currentValue %}selected{% endif %}>
85-
{% if format.likely %}
86-
{{format.displayName}} (*)
87-
{% else %}
88-
{{format.displayName}}
89-
{% endif %}
85+
{% if format.name == currentValue %}selected{% endif %}>
86+
{{format.displayName}}
9087
</option>
9188
{% endfor %}
89+
</optgroup>
90+
<optgroup label="Matches some rows">
91+
{% for format in possibleFormats[1] %}
92+
<option value="{{format.name}}"
93+
{% if format.name == currentValue %}selected{% endif %}>
94+
{{format.displayName}}
95+
</option>
96+
{% endfor %}
97+
</optgroup>
9298
</select>
9399
{% endif %}
94100
</td>

lib/importer/src/functions.js

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ const importerMappedColumnDetails = (data) => {
8282
//--------------------------------------------------------------------
8383
// As long as column->field mappings have been set up, this function will return
8484
// an array indexed on column, with each element being list of possible formats
85-
// for that column. The array element for a column will be false if the column is mapped to a
86-
// field whose type does not require formats, otherwise it will be a list of
87-
// objects with 'name' (internal code), 'displayName' (human-facing name) and
88-
// 'description' (longer description) fields.
85+
// for that column. The array element for a column will be false if the column
86+
// is mapped to a field whose type does not require formats, otherwise it will
87+
// be TWO lists of objects with 'name' (internal code), 'displayName'
88+
// (human-facing name) and 'description' (longer description) fields. The first
89+
// is "safe" formats that will work for all the values in the column, the second
90+
// are "maybe" formats that will work for some of them. Those that work for none
91+
// of them are not present at all.
8992
// --------------------------------------------------------------------
9093
const importerPossibleColumnFormats = (data) => {
9194
// FIXME: This should probably move into the backend, as a SessionSuggestFormats function
@@ -105,11 +108,20 @@ const importerPossibleColumnFormats = (data) => {
105108
const columnDef = session.fields.find((field) => field.name == columnField);
106109
const columnTypeName = columnDef.type;
107110
const columnType = supportedTypes.get(columnTypeName);
108-
let likelyFormats = new Set(); // Names of formats this column is likely to have, according to the guesser
109-
if (columnGuesses.types && columnGuesses.types.has(columnTypeName)) {
110-
const guessedFormats = columnGuesses.types.get(columnTypeName);
111+
112+
let safeFormats = new Set(); // Names of formats this column is likely to have, according to the guesser
113+
if (columnGuesses.safeTypes && columnGuesses.safeTypes.has(columnTypeName)) {
114+
const guessedFormats = columnGuesses.safeTypes.get(columnTypeName);
115+
if(guessedFormats && columnType.formats) {
116+
safeFormats = new Set(Array.from(columnType.formats.keys()).filter((fmtName) => guessedFormats.includes(fmtName)));
117+
}
118+
}
119+
120+
let maybeFormats = new Set(); // Names of formats this column merely might have, according to the guesser
121+
if (columnGuesses.maybeTypes && columnGuesses.maybeTypes.has(columnTypeName)) {
122+
const guessedFormats = columnGuesses.maybeTypes.get(columnTypeName);
111123
if(guessedFormats && columnType.formats) {
112-
likelyFormats = new Set(Array.from(columnType.formats.keys()).filter((fmtName) => guessedFormats.includes(fmtName)));
124+
maybeFormats = new Set(Array.from(columnType.formats.keys()).filter((fmtName) => guessedFormats.includes(fmtName)));
113125
}
114126
}
115127

@@ -119,10 +131,13 @@ const importerPossibleColumnFormats = (data) => {
119131
name: fmtEntry[0],
120132
displayName: fmtEntry[1].displayName,
121133
description: fmtEntry[1].description,
122-
likely: likelyFormats.has(fmtEntry[0])
134+
safe: safeFormats.has(fmtEntry[0]),
135+
maybe: maybeFormats.has(fmtEntry[0])
123136
};
124137
});
125-
return options;
138+
const formats = [options.filter((fmtEntry) => fmtEntry.safe),
139+
options.filter((fmtEntry) => fmtEntry.maybe)];
140+
return formats;
126141
} else {
127142
return false;
128143
}
@@ -158,19 +173,19 @@ const importerGetTrailingRows = (data, count) => {
158173
// "All {n} rows of {sheet}"
159174
//--------------------------------------------------------------------
160175
const importerGetTableCaption =
161-
(data, prefix, rowsAskedFor, sheetName = null) => {
162-
const session_data = data[IMPORTER_SESSION_KEY];
163-
const session = new session_lib.Session(session_data)
176+
(data, prefix, rowsAskedFor, sheetName = null) => {
177+
const session_data = data[IMPORTER_SESSION_KEY];
178+
const session = new session_lib.Session(session_data)
164179

165-
const sheet = (sheetName ??= session.sheet);
166-
const rowCount = sheets_lib.GetTotalRows(session.backendSid, sheet);
180+
const sheet = (sheetName ??= session.sheet);
181+
const rowCount = sheets_lib.GetTotalRows(session.backendSid, sheet);
167182

168-
if (rowCount > rowsAskedFor) {
169-
return `${prefix} ${rowsAskedFor} rows of '${sheet}'`;
170-
}
183+
if (rowCount > rowsAskedFor) {
184+
return `${prefix} ${rowsAskedFor} rows of '${sheet}'`;
185+
}
171186

172-
return `All rows of '${sheet}'`;
173-
}
187+
return `All rows of '${sheet}'`;
188+
}
174189

175190
//--------------------------------------------------------------------
176191
// In the absence of a step to choose headers, we will instead provide
@@ -208,8 +223,8 @@ const importerGetHeaders = (data) => {
208223
session.backendSid,
209224
session.sheet,
210225
index,
211-
/* cellWidth */ 20,
212-
/* count */ 5,
226+
/* cellWidth */ 20,
227+
/* count */ 5,
213228
).inputValues;
214229

215230
let exampleString = examples.join(", ").trim();
@@ -306,8 +321,8 @@ const data_avg = (data, column) => {
306321
}
307322

308323
const numbers = mapResults.resultRecords
309-
.filter((x) => x !== undefined && x[idx] !== undefined)
310-
.map((x) => parseNumberFromString(x[idx]))
324+
.filter((x) => x !== undefined && x[idx] !== undefined)
325+
.map((x) => parseNumberFromString(x[idx]))
311326

312327

313328
const avg = numbers.reduce((acc, i) => acc + i, 0) / numbers.length
@@ -354,7 +369,7 @@ module.exports = {
354369
}
355370

356371
/*
357-
Local Variables:
358-
js-indent-level: 4
359-
End:
372+
Local Variables:
373+
js-indent-level: 4
374+
End:
360375
*/

lib/importer/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ exports.Initialise = (config, router, prototypeKit) => {
442442
if ("other" in request.query) {
443443
const columnsNeedingFormats = backend.SessionGetColumnsNeedingFormats(session.backendSid);
444444
if(columnsNeedingFormats.length == 0) {
445-
const otherPage = decodeURIComponent(request.query.other)
445+
const otherPage = decodeURIComponent(request.query.other);
446446
if (otherPage) { request.query.next = request.query.other }
447447
}
448448
}

0 commit comments

Comments
 (0)