Conversation
* create postcode lookup component to replace existing lookups used in HOF forms: - use OS Places API to retrieve address data - make the component reusable and configurable by forms
william-gu-hof
left a comment
There was a problem hiding this comment.
Great work, functionality is good, just need some more consideration of code structure, readability, and some data structure checks / sanitisation
components/postcode-lookup/index.js
Outdated
|
|
||
| const defaults = require('./defaults'); | ||
|
|
||
| const conditionalTranslate = (key, t) => { |
There was a problem hiding this comment.
Please use more meaningful variable name, think about the next dev looking at this code, what is t? what is the key for? Why does the logic need to compare the value in t indexed by key with the key itself?
Think about how you name your functions and variables, most of the time, if you named them well, no need for documentation
There was a problem hiding this comment.
Changed the variable name to something more suitable.
components/postcode-lookup/index.js
Outdated
| /* eslint-disable func-names */ | ||
| 'use strict'; | ||
|
|
||
| const axios = require('axios'); |
There was a problem hiding this comment.
Couldn't see axios included in package.json? How is this working?
There was a problem hiding this comment.
Added it back into package.json with the same version in master
controller/validation/validators.js
Outdated
|
|
||
| postcodeValidation(value) { | ||
| // eslint-disable-next-line max-len | ||
| return Validators.regex(value, /^(([GIRgir] ?[ ]*0[ ]*[Aa]{2}[ ]*)|((([A-Za-z][ ]*[0-9]{1,2}[ ]*)|(([A-Za-z][ ]*[A-Ha-hJ-Yj-y][ ]*[0-9][ ]*[0-9]?)[ ]*|(([A-Za-z][ ]*[0-9][ ]*[A-Za-z][ ]*)|([A-Za-z][ ]*[A-Ha-hJ-Yj-y][ ]*[0-9]?[ ]*[A-Za-z][ ]*))))[-,. ]?[ ]*[0-9][ ]*[A-Za-z][ ]*[A-Za-z][ ]*))$/); |
There was a problem hiding this comment.
apart from the fact that the existing postcode validation rule allows for empty string, what are the other differences between the new one and the existing one? (looks like your regex is checking a lot more)
Just thinking about maintainability here of having 2 separate post code validation rules, not sure that is practical, depending on what the new regex is doing, maybe we can just overwrite the existing rule, and in your component, use the required and postcode validation together?
There was a problem hiding this comment.
The new validation also allows punctuation within the postcode, which I'm not sure if that is accepted in all postcode fields outside of this lookup. Would need reviewing to determine if this new postcode validation would suffice for all postcode inputs across our services.
package.json
Outdated
| "name": "hof", | ||
| "description": "A bootstrap for HOF projects", | ||
| "version": "20.5.4", | ||
| "version": "21.1.0", |
There was a problem hiding this comment.
Do we have a defined list of what each of the 3 part of the version number means? Is this a breaking change? Why adding a new component would be a breaking change?
There was a problem hiding this comment.
Changed this to be 21.2.0 since it's not a breaking change.
components/postcode-lookup/index.js
Outdated
| } | ||
| }); | ||
|
|
||
| const getConfig = key => ({ |
There was a problem hiding this comment.
this is not really a key, it is the value of the field name prefix, I would name this variable differently to avoid confusion
There was a problem hiding this comment.
Changed variable name.
| assert.ok(url.includes('step=manual')); | ||
| })); | ||
|
|
||
| it('redirects to the lookup step on a successful postcode lookup', () => browser.url('/postcode-default-one') |
There was a problem hiding this comment.
Once you managed to mock the request, we need to test on success lookup, the correct results are displayed
There was a problem hiding this comment.
Added a unit test to cover this.
test/functional-tests/index.js
Outdated
| const App = require('./lib/app'); | ||
| const assert = require('assert'); | ||
|
|
||
| // TO BE REVISITED FOR CREATING MOCK API CALL |
There was a problem hiding this comment.
Would also be good to write some unit tests after you refactor the index.js of the component with more functions, test them individually
There was a problem hiding this comment.
Added some unit tests for some of the functions in the component.
components/postcode-lookup/index.js
Outdated
| return super.saveValues(req, res, callback); | ||
| }); | ||
| } else if (step === 'lookup') { | ||
| const addressResult = req.form.options.fields[`${addressKey}-select`].options.filter(obj => { |
There was a problem hiding this comment.
Should do some sanitisation of the input values gathered, strip out dodgy characters like < , {, ! and ?
There was a problem hiding this comment.
Added logic to remove any instances of those characters
components/postcode-lookup/index.js
Outdated
| }).catch(error => { | ||
| if(error.response.status === '404' || error.response.status === '429' || | ||
| error.response.status === '503' || error.response.status === '500') { | ||
| req.query.step === 'postcodeSearchIssue'; |
There was a problem hiding this comment.
too generic, are there detailed info on the API documentation on what the errors means?
There was a problem hiding this comment.
Added logger within the block of code where the function is called and got rid of the 'if' block from the code above to remove duplication. Also, modified the logic so that instead of checking for these specific error codes, it will now log the error regardless of the error code.
components/postcode-lookup/index.js
Outdated
| axios.get(apiURL + '?postcode=' + enteredPostcode + '&key=' + apiKey) | ||
| .then(function (response) { | ||
| const addressList = []; | ||
| _.forOwn(response.data.results, function (value) { |
There was a problem hiding this comment.
do some checks on the response, make sure it is of the expected structure & type
There was a problem hiding this comment.
Added a check for the 'data' property in the response, but didn't check for any sub-properties of that because the component needs to still go to the address lookup page when there are no results returned for a given postcode.
- add feature to enable the toggle of de-indexing so that it can be set per service
william-gu-hof
left a comment
There was a problem hiding this comment.
Code is organised much better.
Just some small questions/suggestions that may help with code readability
.vscode/settings.json
Outdated
| @@ -0,0 +1,2 @@ | |||
| { | |||
There was a problem hiding this comment.
maybe add this folder to the .gitignore file
There was a problem hiding this comment.
Added the folder to the .gitignore file
| @@ -0,0 +1,31 @@ | |||
| /* eslint max-len: 0, no-process-env: 0 */ | |||
|
|
|||
There was a problem hiding this comment.
either the naming of the file or the exported object structure can be improved. It looks like the file is just default error messages, so you can either rename the file to something like defaultErrorMessages.js or change the structure of the object returned to something like:
`module.exports = {
errorMessages: {
CANT_FIND: '...',
...;
}
}`
There was a problem hiding this comment.
I've now moved the variables linked to an issue with the postcode lookup to within the POSTCODE_ERROR structure within this file. The rest of the properties are not issues with the lookup, so have kept them as independent variables.
| return convertPage(page); | ||
| } | ||
| }, | ||
| deIndexForm: process.env.DEINDEX_FORM || 'false', |
There was a problem hiding this comment.
is this needed for the work on postcode lookup?
There was a problem hiding this comment.
Accidentally left in from the de-indexing changes. Removed this now.
| const health = require('./lib/health'); | ||
| const serveStatic = require('./lib/serve-static'); | ||
| const gaTagSetup = require('./lib/ga-tag'); | ||
| const deIndexer = require('./lib/deindex'); |
There was a problem hiding this comment.
similar to above, is this needed for the postcode lookup work?
There was a problem hiding this comment.
Removed this now.
ab3906b to
34f4f76
Compare
- amend postcode-lookup component to remove punctuation from postcode during api call to fix 400 error - amend postcode-lookup component to remove punctuation from postcode in locals - amend postcode-lookup component to remove punctuation from postcode in prepopulated manual fields - amend postcode validation to allow all punctuation - update yarn.lock - remove unused vscode folder
34f4f76 to
2440040
Compare
sulthan-ahmed
left a comment
There was a problem hiding this comment.
I've just reviewed @Rhodine-orleans-lindsay change and that makes sense
What?
Create the postcode lookup component as a reusable solution, using the OS Places API - (HOFF-754, HOFF-768, HOFF-770, & HOFF-771)
Why?
How?
/components/postcode-lookupincluding:/templatesfolder with the postcode lookup HTML viewsdefaults.jsfile to include the default values used in the componentindex.jsfile to handle the logic behind the postcode lookup (configuring fields, routing, OS Places API call, etc.). The values within this are set up dynamically to allow any form to customise the wording as required/components/index.js/controller/validation/validators.js/test/functional-tests/index.js/test/functional-tests/apps/postcode-lookup-default.js/test/data/postcode-lookup-data.jsonTesting?
Tested the component on a service
Anything Else? (optional)
Check list
here is an example commit