-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto data session 4 #340
Auto data session 4 #340
Conversation
Important note - this only works with POST. Do we want it on GET too? |
Problem: HTML allows hyphens in the Solutions:
to
|
In the example the checkbox data passes when only one is checked. If two are checked, the value passed is empty If all three are checked, only the first value is passed. |
@charlesrt thanks, that sounds well broken, investigating.. |
Using this, it's awesome. Please merge :) |
57190bd
to
b4856ae
Compare
@charlesrt I think thats fixed now - I wasn't checking if "_unchecked" was being submitted, and instead just removing elements of arrays - this is now behind a proper if statement. Oops |
d906e59
to
1d04e8c
Compare
1d04e8c
to
382f773
Compare
I've changed the way you display data Old
New
This means hyphenated names work, where they were broken before. I think this PR is ready to go, it's worth waiting for #365 as there's a Check your answers page in this PR that should use the new code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a load of stylistic and small points.
I'm in the process of recreating an existing prototype to test functionality - will add another comment tomorrow.
Other things:
- I don't think it's clear how you pre-fill answers - this could be explicitly called out.
- Should we mention to use on server side?
- Should we mention how to not store data?
For those three, I wonder if the guide should have a final tutorial page that does goes over these items?
<fieldset> | ||
|
||
<legend class="visuallyhidden"> | ||
Which of these applies to your vehicle? (Select all that apply) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think '(Select all that apply)' should be on a new line in regular body copy style.
<div class="form-group"> | ||
<fieldset> | ||
|
||
<label class="form-label" for="registration-number">Vehicle registration number</label> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest this be form-label-bold
- better matches our other examples, and helps it stand out on this page.
<p class="heading-medium"> | ||
What type of vehicle do you have? | ||
</p> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could add a paragraph here that describes that these radios have name="vehicle-type"
, and that this will be used later. Matches better to the check your answers page that uses the value.
Which of these applies to your vehicle? (Select all that apply) | ||
</legend> | ||
|
||
<p class="heading-medium"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this should be an h1, and probably placed outside of the fieldset.
<p class="heading-medium"> | ||
Which of these applies to your vehicle? (Select all that apply) | ||
</p> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could add a paragraph here that describes that these checkboxes have name="vehicle-features"
, and that this will be used later. Matches better to the check your answers page that uses the value.
server.js
Outdated
app.use(utils.forceHttps) | ||
} | ||
|
||
console.log({'autodata': useAutoStoreData}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intentionally here? It outputs before the normal welcome messages.
server.js
Outdated
|
||
console.log({'autodata': useAutoStoreData}) | ||
|
||
if (useAutoStoreData === 'true') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should convert useAutoStoreData
to boolean as we do with useDocumentation
on line 30? We're not very consistent, so if you don't want to do now, that's fine.
server.js
Outdated
app.use(function (req, res, next) { | ||
// add nunjucks function to get values, needs to be here as they need access to req.session | ||
|
||
nunjucksAppEnv.addGlobal('checked', function (name, value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be pulled out as a function so it doesn't get duplicated for both apps?
server.js
Outdated
next() | ||
}) | ||
|
||
documentationApp.use(function (req, res, next) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this check if documentationApp exists?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
documentationApp currently always exists - its declared at the top of the file, we might want to change that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The app exists, but routes aren't set up for it.
Later in server.js
we use if (useDocumentation) {}
- do you think something similar would be appropriate here?
I don't have a strong preference here - if you think it's not needed that's fine.
<fieldset> | ||
|
||
<label class="form-label" for="registration-number">Vehicle registration number</label> | ||
<input class="form-control" id="registration-number" name="vehicle-registration" type="text" value="{{vehicleRegistration}}"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the id
match the name
?
I've recreated an existing prototype, and all looks good! Have successfully tested:
Some further comments:
I'm not confident enough to read the javascript in |
server.js
Outdated
return inArray ? 'checked' : '' | ||
} else { | ||
return value === storedValue ? 'checked' : '' | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to refactor the 2 ternaries here you can try this:
var isChecked = false
if (Array.isArray(storedValue)) {
isChecked = storedValue.indexOf(value) !== -1
} else {
isChecked = value === storedValue
}
return isChecked ? 'checked' : ''
Or terser:
var isChecked = Array.isArray(storedValue)
? storedValue.indexOf(value) !== -1
: value === storedValue
return isChecked ? 'checked' : ''
(Just a thought, feel free to ignore)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, I had another bash taking your first as a base, and ended up with this:
var checked = ''
// if data is an array, check it exists in the array
if (Array.isArray(storedValue)) {
if (storedValue.indexOf(value) !== -1){
checked = 'checked'
}
} else {
// the data is just a simple value, check it matches
if (storedValue === value){
checked = 'checked'
}
}
return checked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trying to avoid ternaries since they can be hard to read
|
||
exports.autoStoreData = function (req, res, next) { | ||
if (!req.session.data) { | ||
req.session.data = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use this common pattern too:
req.session.data = req.session.data || {}
6bfd69d
to
6ba6583
Compare
Note this now supports both POST and GET |
8f96526
to
b155c61
Compare
</p> | ||
|
||
<p> | ||
The code for the example is in this folder in the prototype kit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest this be pulled out in to a section like the other examples below.
c67f8ec
to
610e42d
Compare
</pre> | ||
|
||
<h3 class="heading-small"> | ||
Setting inputs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This heading feels jargony. What about something like "Pre-filling answers"
610e42d
to
825b7a7
Compare
edbd8c7
to
51a0866
Compare
51a0866
to
34ce07c
Compare
2bb5fa2
to
047065a
Compare
I think this is 👍 to merge. Amazing work. Thanks so much @joelanman for this. It's a massive improvement to the kit. |
AMAZING WORK JOEAnd Ed. And everyone. But mainly Joe. |
this updates #214 Auto data session 3 so it can be merged - copied from that PR:
This version of automatically storing and showing data across pages does not rely on macros, but instead using the
checked
function directly (see examples below). I'm not sure it's as user-friendly as the version with macros, but it may take a while to finalise our approach to macros.Usage
This will make the kit automatically store all data from forms, for use later in your prototype.
For example if you have an input like this:
You can put this on the next page, or any other page:
Population
To populate a text field with the value a user entered is simple:
To set radios or checkboxes use this:
Using the name and value of the input. For example:
Dealing with checkboxes
This adds javascript so that when a user submits a form, the script looks for checkboxes in that form. For any checkbox, or group of checkboxes with the same name attribute, it adds a hidden input of the same name, with value "_unchecked". If the server only receives "_unchecked", it clears any data associated with that name.