-
Notifications
You must be signed in to change notification settings - Fork 0
feat: create postcode lookup component in Component Library #590
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
Open
michael-odonovan
wants to merge
42
commits into
master
Choose a base branch
from
2750_create_postcode_lookup_in_CL
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
1230601
feat: create postcode lookup component in Component Library
michael-odonovan 92b4291
setup basic component
michael-odonovan 4b445ca
adjust visual appearance in Component Library
michael-odonovan 351fe7f
add postcode package
michael-odonovan 626aaf7
bring in FSU code
michael-odonovan de27d4c
get api returning data and add a little styling
michael-odonovan 68c4211
refining example
michael-odonovan ebb4119
tidy up props
michael-odonovan 3b85274
clean up
michael-odonovan b1e0337
snaps
michael-odonovan a061e7e
typo
michael-odonovan 8a43e83
add props fro name and label
michael-odonovan 476f482
sort snapshot re. styled-components-jest
michael-odonovan 72b8fb6
update snapshots and props
michael-odonovan fc5092b
mock test in place and working
michael-odonovan fd83679
still working
michael-odonovan 6868bf5
works with right postcode data : )
michael-odonovan a80c959
remove examples
michael-odonovan cd6d868
linting
michael-odonovan 52888d3
Merge branch 'master' of https://github.com/comicrelief/component-lib…
michael-odonovan ea5c8ca
bank
michael-odonovan 48cc0ad
setup Sentry function
michael-odonovan 3ef69ea
tidy up
michael-odonovan 0e850ac
cleanup
michael-odonovan 2e3aa88
cleanup
michael-odonovan 25c851d
cleanup
michael-odonovan 370e9f7
Empty commit to trigger build
michael-odonovan af2515e
Merge branch 'master' of https://github.com/comicrelief/component-lib…
michael-odonovan 8c06816
adjust failing default name props etc
michael-odonovan f2c97e3
name prop shinnigans
michael-odonovan cd1f930
redo name
michael-odonovan 06dd6af
adjust button wander
michael-odonovan 3c20086
add export for PostcodeLookup
michael-odonovan f0331dd
postcode lookup working with population of basic fields
michael-odonovan 592a9b2
adjust spacing and streamline Lookup functionality
michael-odonovan 924ced6
sort opening of manual address fields
michael-odonovan 6980897
add extra props
michael-odonovan 2739da5
snaps
michael-odonovan 518fc18
adjust open fields link spacing / layout
michael-odonovan ce6e92a
snaps and lint
michael-odonovan 9712dcb
Merge branch 'master' of https://github.com/comicrelief/component-lib…
michael-odonovan 855d1fc
add Lookup button props
michael-odonovan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Lookup | ||
| This is a simple basic vanilla component and is rendered here mainly for dev purposes. | ||
| You may would be best served using one of these more substantial components in projects / production: | ||
| - PostcodeLookup | ||
| - SchoolLookup | ||
| - SimpleSchoolLookup | ||
|
|
||
| The `lookupHandler` should be an async function which is called when a lookup is triggered (either by hitting enter or clicking the button). | ||
|
|
||
| It will receive the current search term and should: | ||
| - take care of any validation on the search term | ||
| - perform the actual lookup request | ||
| - return an array of options (or an empty array if none were found) | ||
| - only throw errors with user-friendly messages | ||
|
|
||
| Any errors thrown will be caught and the message will be displayed to the user. | ||
|
|
||
| The `onSelect` function will receive the chosen option. | ||
|
|
||
| ```js | ||
| import Lookup from './Lookup'; | ||
|
|
||
| const schoolFetcher = async query => { | ||
| if (!query || !query.trim()) { | ||
| throw new Error('Please enter a search query'); | ||
| } | ||
|
|
||
| if (query.length < 2) { | ||
| throw new Error('Please enter at least two characters'); | ||
| } | ||
|
|
||
| try { | ||
| const res = await axios.get( | ||
| 'https://lookups.sls.comicrelief.com/schools/lookup', | ||
| { timeout: 10000, params: { query } } | ||
| ); | ||
| return res.data.data.schools; | ||
| } catch (error) { | ||
| // if (typeof Sentry !== 'undefined') { | ||
| // Sentry.captureException(error); | ||
| // } | ||
| throw new Error('Sorry, something unexpected went wrong. Please try again or enter your school manually'); | ||
| } | ||
| }; | ||
|
|
||
| const schoolToString = school => `${school.name}, ${school.post_code}`; | ||
|
|
||
| <Lookup | ||
| name="Lookup" | ||
| label="Custom label" | ||
| placeholder="...placeholder" | ||
| buttonText="Lookup Button" | ||
| dropdownInstruction="Please select an organisation from the list below" | ||
| noResultsMessage='Sorry, we could not find anything matching your search; please use the manual entry option.' | ||
| lookupHandler={schoolFetcher} | ||
| mapOptionToString={schoolToString} | ||
| onSelect={address => alert(JSON.stringify(address, null, 2))} | ||
| // buttonTextColour='black' | ||
| // buttonBgColour='#f04257' | ||
| // buttonHoverTextColour='white' | ||
| // buttonHoverBgColour='orange' | ||
| /> | ||
| ``` | ||
|
|
100 changes: 100 additions & 0 deletions
100
src/components/Molecules/PostcodeLookup/PostcodeLookup.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import React, { useState } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import styled from 'styled-components'; | ||
| import Lookup from '../Lookup/Lookup'; | ||
| import AddressInputs from './utils/AddressInputs'; | ||
| import { addressToString, addressFetcher } from './utils/PostcodeFunctions'; | ||
| import spacing from '../../../theme/shared/spacing'; | ||
|
|
||
| // ${({ theme }) => theme.linkStyles('standard')}; | ||
| const AddressManually = styled.button` | ||
| margin: ${spacing('md')} 0 0 ${spacing('sm')}; | ||
| background: inherit; | ||
| border: none; | ||
| font-size: 1rem; | ||
| &:hover, &:active { | ||
| text-decoration: underline; | ||
| } | ||
| `; | ||
|
|
||
| export default function PostcodeLookup({ | ||
| label, | ||
| name, | ||
| placeholder, | ||
| buttonText, | ||
| noResultsMessage, | ||
| reportError, | ||
| dropdownInstruction, | ||
| buttonColour | ||
| }) { | ||
| const [showFields, setShowFields] = useState(false); | ||
|
|
||
| // Address field state | ||
| const [addressFields, setAddressFields] = useState({ | ||
| line1: '', | ||
| line2: '', | ||
| line3: '', | ||
| posttown: '' | ||
| }); | ||
|
|
||
| // Function to update address fields | ||
| const handleAddressSelect = selectedAddress => { | ||
| setShowFields(true); | ||
| setAddressFields({ | ||
| line1: selectedAddress.Line1 || '', | ||
| line2: selectedAddress.Line2 || '', | ||
| line3: selectedAddress.Line3 || '', | ||
| posttown: selectedAddress.posttown || '' | ||
| }); | ||
| }; | ||
|
|
||
| const handleManualClick = event => { | ||
| event.preventDefault(); | ||
| setShowFields(true); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Lookup | ||
| name={name} | ||
| label={label} | ||
| placeholder={placeholder} | ||
| buttonText={buttonText} | ||
| mapOptionToString={addressToString} | ||
| lookupHandler={postcode => addressFetcher(postcode, reportError)} | ||
| onSelect={handleAddressSelect} | ||
| dropdownInstruction={dropdownInstruction} | ||
| noResultsMessage={noResultsMessage} | ||
| buttonColour={buttonColour} | ||
| /> | ||
| <AddressManually onClick={handleManualClick}> | ||
| Or enter your address manually | ||
| </AddressManually> | ||
| {showFields && <AddressInputs addressFields={addressFields} />} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| PostcodeLookup.propTypes = { | ||
| name: PropTypes.string, | ||
| label: PropTypes.string, | ||
| placeholder: PropTypes.string, | ||
| buttonText: PropTypes.string, | ||
| noResultsMessage: PropTypes.string, | ||
| dropdownInstruction: PropTypes.string, | ||
| reportError: PropTypes.oneOfType([ | ||
| PropTypes.func | ||
| ]), | ||
| buttonColour: PropTypes.string | ||
| }; | ||
|
|
||
| PostcodeLookup.defaultProps = { | ||
| name: 'postcode_lookup', | ||
| label: 'Find address by postcode', | ||
| placeholder: 'Enter postcode...', | ||
| buttonText: 'Find address', | ||
| noResultsMessage: 'Sorry, could not find any addresses for that postcode', | ||
| dropdownInstruction: 'Please select an organisation from the list below', | ||
| reportError: undefined, | ||
| buttonColour: '#f04257' | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| ```js | ||
| import PostcodeLookup from './PostcodeLookup'; | ||
|
|
||
| // This is just an example of how a parent component might handle fetch errors within the component. | ||
| const [enterManually, setEnterManually] = React.useState(false); | ||
|
|
||
| const fetchErrorHandler = () => { | ||
| setEnterManually(true); | ||
| } | ||
|
|
||
| enterManually | ||
| ? 'Sorry, there appears to be a problem. Please enter your details manually.' | ||
| : ( | ||
| <PostcodeLookup | ||
| label="Postal Address" | ||
| buttonText="Find Address" | ||
| // Example for could not find address - n22 4qa | ||
| noResultsMessage='Sorry, we could not find your address.' | ||
| reportError='Custom report error' | ||
| dropdownInstruction="Dropdown instruction here..." | ||
| buttonColour='#f04257' | ||
| /> | ||
| ) | ||
| ``` |
12 changes: 12 additions & 0 deletions
12
src/components/Molecules/PostcodeLookup/PostcodeLookup.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import React from 'react'; | ||
| import 'jest-styled-components'; | ||
|
|
||
| import renderWithTheme from '../../../hoc/shallowWithTheme'; | ||
| import PostcodeLookup from './PostcodeLookup'; | ||
|
|
||
| it('renders correctly', () => { | ||
| const tree = renderWithTheme( | ||
| <PostcodeLookup onSelect={address => alert(JSON.stringify(address, null, 2))} /> | ||
| ).toJSON(); | ||
| expect(tree).toMatchSnapshot() | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.