Skip to content

Commit

Permalink
Merge pull request #20705 from code-dot-org/school-dropdown-flexibility
Browse files Browse the repository at this point in the history
School dropdown flexibility
  • Loading branch information
drewsamnick committed Feb 19, 2018
2 parents be6b434 + a654cef commit d06d459
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
15 changes: 10 additions & 5 deletions apps/src/templates/SchoolAutocompleteDropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ export default class SchoolAutocompleteDropdown extends Component {
onChange: PropTypes.func.isRequired,
// Value is the NCES id of the school
value: PropTypes.string,
fieldName: PropTypes.string
fieldName: PropTypes.string,
schoolDropdownOption: PropTypes.object,
schoolFilter: PropTypes.func,
};

static defaultProps = {
fieldName: "nces_school_s"
fieldName: "nces_school_s",
schoolFilter: () => true,
};


constructSchoolOption = school => ({
value: school.nces_id.toString(),
label: `${school.name} - ${school.city}, ${school.state} ${school.zip}`
label: `${school.name} - ${school.city}, ${school.state} ${school.zip}`,
school: school,
});

constructSchoolNotFoundOption = () => ({
Expand All @@ -45,7 +50,7 @@ export default class SchoolAutocompleteDropdown extends Component {
fetch(searchUrl)
.then(response => response.ok ? response.json() : [])
.then(json => {
const schools = json.map(school => this.constructSchoolOption(school));
const schools = json.filter(this.props.schoolFilter).map(school => this.constructSchoolOption(school));
schools.unshift(this.constructSchoolNotFoundOption());
return { options: schools };
})
Expand Down Expand Up @@ -93,7 +98,7 @@ export default class SchoolAutocompleteDropdown extends Component {
loadOptions={this.getOptions}
cache={false}
filterOption={() => true}
value={this.props.value}
value={this.props.schoolDropdownOption ? this.props.schoolDropdownOption : this.props.value}
onChange={this.props.onChange}
placeholder={i18n.searchForSchool()}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ export default class SchoolAutocompleteDropdownWithLabel extends Component {
fieldName: PropTypes.string,
singleLineLayout: PropTypes.bool,
showRequiredIndicator: PropTypes.bool,
schoolDropdownOption: PropTypes.object,
schoolFilter: PropTypes.func,
};

schoolDropdown = undefined;

static defaultProps = {
showRequiredIndicator: true,
};
Expand All @@ -43,12 +47,16 @@ export default class SchoolAutocompleteDropdownWithLabel extends Component {
handleSchoolNotFoundCheckbox(event) {
var checkbox = event.target;
if (checkbox.checked) {
this.props.setField("nces", {value: "-1"});
this.props.setField("nces", this.schoolDropdown.constructSchoolNotFoundOption());
} else {
this.props.setField("nces", {value: ""});
this.props.setField("nces", this.props.schoolDropdownOption);
}
}

bindDropdown = (dropdown) => {
this.schoolDropdown=dropdown;
};

render() {
const {showRequiredIndicator, singleLineLayout} = this.props;
const questionStyle = {...styles.question, ...(singleLineLayout && singleLineLayoutStyles)};
Expand All @@ -71,9 +79,12 @@ export default class SchoolAutocompleteDropdownWithLabel extends Component {
{!singleLineLayout && showError && errorDiv}
</div>
<SchoolAutocompleteDropdown
ref={this.bindDropdown}
value={this.props.value}
fieldName={this.props.fieldName}
onChange={this.sendToParent}
schoolDropdownOption={this.props.schoolDropdownOption}
schoolFilter={this.props.schoolFilter}
/>
<label>
<input id="schoolNotFoundCheckbox" type="checkbox" onChange={this.handleSchoolNotFoundCheckbox.bind(this)} checked={this.props.value === "-1"}/>
Expand Down
6 changes: 3 additions & 3 deletions apps/test/unit/templates/SchoolAutocompleteDropdownTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ describe('SchoolAutocompleteDropdown', () => {
return expect(promise).to.eventually.deep.equal({
options: [
{value: '-1', label: 'Other school not listed below (click here to provide details)'},
{value: '10', label: 'Abcd School 1 - Seattle, WA 98101'},
{value: '11', label: 'Abcd School 2 - Redmond, WA 98073'}
{value: '10', label: 'Abcd School 1 - Seattle, WA 98101', school: {nces_id: 10, name: 'Abcd School 1', city: 'Seattle', state: 'WA', zip: '98101'}},
{value: '11', label: 'Abcd School 2 - Redmond, WA 98073', school: {nces_id: 11, name: 'Abcd School 2', city: 'Redmond', state: 'WA', zip: '98073'}}
]
});
});
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('SchoolAutocompleteDropdown', () => {

return expect(promise).to.eventually.deep.equal({
options: [
{value: '9999', label: 'Abcd School 1 - Seattle, WA 98101'}
{value: '9999', label: 'Abcd School 1 - Seattle, WA 98101', school: {nces_id: 9999, name: 'Abcd School 1', city: 'Seattle', state: 'WA', zip: '98101'}}
]
});
});
Expand Down
2 changes: 1 addition & 1 deletion dashboard/lib/api/v1/school_autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.search_by_zip?(query)

# JSON serializer used by get_matches.
class Serializer < ActiveModel::Serializer
attributes :nces_id, :name, :city, :state, :zip, :school_type
attributes :nces_id, :name, :city, :state, :zip, :school_type, :latitude, :longitude

def nces_id
object.id.to_s
Expand Down
12 changes: 9 additions & 3 deletions dashboard/test/controllers/api/v1/schools_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class Api::V1::SchoolsControllerTest < ActionController::TestCase
name: 'Albert Einstein Academy Elementary',
city: 'Santa Clarita',
state: 'CA',
zip: '91355'
zip: '91355',
latitude: "34.4375",
longitude: "-118.576861"
}.deep_stringify_keys.freeze

GLADYS_JUNG_ELEMENTARY = {
Expand All @@ -16,7 +18,9 @@ class Api::V1::SchoolsControllerTest < ActionController::TestCase
name: 'Gladys Jung Elementary',
city: 'Bethel',
state: 'AK',
zip: '99559'
zip: '99559',
latitude: "60.80254",
longitude: "-161.77072"
}.deep_stringify_keys.freeze

JOANN_A_ALEXIE_MEMORIAL_SCHOOL = {
Expand All @@ -25,7 +29,9 @@ class Api::V1::SchoolsControllerTest < ActionController::TestCase
name: 'Joann A. Alexie Memorial School',
city: 'Atmautluak',
state: 'AK',
zip: '99559'
zip: '99559',
latitude: "60.866944",
longitude: "-162.273056"
}.deep_stringify_keys.freeze

test 'search by school name prefix' do
Expand Down

0 comments on commit d06d459

Please sign in to comment.