-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1230601
commit 92b4291
Showing
4 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,65 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import axios from 'axios'; | ||
|
||
import Typeahead from '../Typeahead/Typeahead'; | ||
|
||
const optionFetcher = async query => { | ||
const response = await axios.get( | ||
'https://lookups.sls.comicrelief.com/schools/lookup', | ||
{ | ||
params: { query }, | ||
timeout: 3000 | ||
} | ||
); | ||
return response.data.data.schools; | ||
}; | ||
|
||
const optionParser = school => `${school.name}, ${school.post_code}`; | ||
|
||
const SchoolLookup = React.forwardRef( | ||
( | ||
{ | ||
label, | ||
placeholder, | ||
notFoundMessage, | ||
dropdownInstruction, | ||
onSelect, | ||
...rest | ||
}, | ||
ref | ||
) => { | ||
const props = { | ||
optionFetcher, | ||
optionParser, | ||
onSelect, | ||
id: 'school-lookup', | ||
name: 'school-lookup', | ||
label, | ||
placeholder, | ||
notFoundMessage, | ||
dropdownInstruction, | ||
...rest | ||
}; | ||
|
||
return <Typeahead {...props} ref={ref} />; | ||
} | ||
); | ||
|
||
SchoolLookup.propTypes = { | ||
/** This function is used to provide data to the parent component when a selection is made. */ | ||
onSelect: PropTypes.func.isRequired, | ||
label: PropTypes.string, | ||
placeholder: PropTypes.string, | ||
dropdownInstruction: PropTypes.string, | ||
notFoundMessage: PropTypes.string | ||
}; | ||
|
||
SchoolLookup.defaultProps = { | ||
label: 'Enter the name or postcode of your school or nursery', | ||
placeholder: 'Type to start search', | ||
dropdownInstruction: 'Please select a school from the list below', | ||
notFoundMessage: "Sorry, we can't find this school" | ||
}; | ||
|
||
export default SchoolLookup; |
This file contains 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,18 @@ | ||
# PostcodeLookup | ||
|
||
```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 the school\'s details manually.' | ||
: ( | ||
<PostcodeLookup onSelect={school => alert(JSON.stringify(school, null, 2))} fetchErrorHandler={fetchErrorHandler} /> | ||
) | ||
``` |
This file contains 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 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