Skip to content
This repository was archived by the owner on Jun 10, 2019. It is now read-only.

Commit 29b23a2

Browse files
committed
Add error-handling to fuzzy search and fix render
Found a bug that may occur is there is a typo within the state field of a school within the code_schools API endpoint. Added error handling for if that situation occurs again. Variable names changed. Rendering functionality changed to cope with new API implementation.
1 parent b8b6d29 commit 29b23a2

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/scenes/home/codeSchools/stateSortedSchools/stateSortedSchools.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,42 @@ class StateSortedSchools extends Component {
2929

3030
searchState = (string) => {
3131
const userInput = string.replace(/\w\S*/g, txt => txt.toUpperCase());
32-
const schools = [];
32+
const matchingSchools = [];
3333

34-
// Return true if input matches state code or name (ex: "CA or California")
35-
function matchesState(school, input) {
36-
const stateName = stateCodes[school.state].toUpperCase();
37-
return school.state.includes(input) || stateName.includes(input);
34+
// Return true if thisInput matches thisCampus's state code or name (ex: "CA or California")
35+
function matchesState(thisInput, thisCampus) {
36+
try {
37+
const stateName = stateCodes[thisCampus.state].toUpperCase();
38+
return thisCampus.state.includes(thisInput) || stateName.includes(thisInput);
39+
} catch (e) {
40+
if (e instanceof TypeError) {
41+
console.log('Error: Typo in code_schools.yaml on the back-end under the `state` field');
42+
console.log(e);
43+
return false;
44+
}
45+
// Unknown error issue
46+
return e;
47+
}
3848
}
3949

4050
this.props.schools.forEach((school) => {
41-
school.locations.filter(_school => matchesState(_school, userInput)).forEach((location) => {
42-
schools.push({
51+
school.locations.filter(location => matchesState(userInput, location)).forEach((campus) => {
52+
matchingSchools.push({
4353
name: school.name,
4454
url: school.url,
45-
address: location.address1,
46-
city: location.city,
47-
state: location.state,
48-
zip: location.zip,
55+
address: campus.address1,
56+
city: campus.city,
57+
state: campus.state,
58+
zip: campus.zip,
4959
logo: school.logo,
50-
va_accepted: location.va_accepted,
60+
va_accepted: campus.va_accepted,
5161
full_time: school.full_time,
5262
hardware_included: school.hardware_included
5363
});
5464
});
5565
});
5666

57-
return schools;
67+
return matchingSchools;
5868
};
5969

6070
render() {

0 commit comments

Comments
 (0)