Skip to content

Commit

Permalink
Merge pull request #17422 from code-dot-org/persist_school_district_data
Browse files Browse the repository at this point in the history
Persist school district data
  • Loading branch information
mehalshah committed Aug 31, 2017
2 parents 8f296e5 + 601a0b4 commit 560b8e7
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 8 deletions.
20 changes: 14 additions & 6 deletions apps/src/code-studio/pd/form_components/FormController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,20 @@ export default class FormController extends React.Component {
if (data.responseJSON &&
data.responseJSON.errors &&
data.responseJSON.errors.form_data) {
// if the failure was a result of an invalid form, highlight the errors
// and display the generic error header
this.setState({
errors: data.responseJSON.errors.form_data,
errorHeader: "Please correct the errors below."
});
if (data.responseJSON.general_error) {
this.setState({
errors: data.responseJSON.errors.form_data,
errorHeader: data.responseJSON.general_error,
globalError: true
});
} else {
// if the failure was a result of an invalid form, highlight the errors
// and display the generic error header
this.setState({
errors: data.responseJSON.errors.form_data,
errorHeader: "Please correct the errors below."
});
}
} else {
// Otherwise, something unknown went wrong on the server
this.setState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ import FormComponent from '../form_components/FormComponent';
import {FormGroup} from 'react-bootstrap';

export default class RegionalPartnerContact extends FormController {
/**
* @override
*/
serializeFormData() {
const formData = super.serializeFormData();
Object.assign(formData['form_data'], this.getDistrictData());
return formData;
}

getDistrictData() {
const schoolDistrictData = {};

schoolDistrictData['school-type'] = document.getElementById('school-type').value;
schoolDistrictData['school-state'] = document.getElementById('school-state').value;
schoolDistrictData['school-district'] = document.querySelector('#school-district input').value;
schoolDistrictData['school-district-other'] = document.getElementById('school-district-other').checked;
schoolDistrictData['school'] = document.querySelector('#school input').value;
schoolDistrictData['school-other'] = document.getElementById('school-other').checked;
schoolDistrictData['school-district-name'] = document.getElementById('school-district-name').value;
schoolDistrictData['school-name'] = document.getElementById('school-name').value;
schoolDistrictData['school-zipcode'] = document.getElementById('school-zipcode').value;

return schoolDistrictData;
}
/**
* @override
*/
Expand Down Expand Up @@ -85,3 +109,6 @@ class RegionalPartnerContactComponent extends FormComponent {
);
}
}

RegionalPartnerContactComponent.associatedFields =
['firstName', 'lastName', 'title', 'email', 'role', 'jobTitle', 'gradeLevels', 'notes'];
7 changes: 6 additions & 1 deletion dashboard/app/controllers/api/v1/pd/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def create
if form.valid?
render json: {id: form.id}, status: :created
else
render json: {errors: form.errors.messages}, status: :bad_request
return_data = {
errors: form.errors.messages
}

form.try(:add_general_errors, return_data)
render json: return_data, status: :bad_request
end
end
end
32 changes: 31 additions & 1 deletion dashboard/app/models/pd/regional_partner_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
class Pd::RegionalPartnerContact < ActiveRecord::Base
include Pd::Form

belongs_to :user
belongs_to :regional_partner

validate :validate_district_fields

def self.required_fields
[
:first_name,
Expand All @@ -27,7 +32,6 @@ def self.required_fields
:role,
:job_title,
:grade_levels,
:notes
]
end

Expand All @@ -40,4 +44,30 @@ def self.options
}
)
end

def add_general_errors(return_data)
if errors.messages[:form_data].include? 'schoolDistrictData'
return_data[:general_error] = 'Please fill out data for your school district'
end
end

private

def validate_district_fields
hash = sanitize_form_data_hash

unless hash[:school_type].presence && hash[:school_state].presence
add_key_error(:school_district_data)
end

if ['public', 'charter'].include? hash[:school_type]
if hash[:school_district_other]
add_key_error(:school_district_data) unless hash[:school_district_name].presence
else
add_key_error(:school_district_data) unless hash[:school_district].presence
end
else
add_key_error(:school_district_data) unless hash[:school_name].presence && hash[:school_zipcode]
end
end
end
7 changes: 7 additions & 0 deletions dashboard/app/views/pd/regional_partner_contact/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
%h2
Contact a regional partner

.form-group
.question School Country
%select#us-or-international.form-control{ name: "school_info[country]", type: "select", required: true, onchange: "if (this.value === 'International') {window.location = 'https://support.code.org/hc/en-us/articles/202518373-How-do-I-bring-computer-science-to-my-country-'}"}
%option{value: "", selected: true, disabled: true}
%option{value: "United States"}United States
%option{value: "International"}International
= render partial: 'shared/school_info', locals: {form_name: "school_info", suppress_scrolling: true, assume_usa: true}
%div#application-container
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'test_helper'

class Api::V1::Pd::RegionalPartnerContactsControllerTest < ::ActionController::TestCase
SAMPLE_FORM_DATA = {
first_name: 'Harry',
last_name: 'Potter',
title: 'Mr.',
email: 'potter@hogwarts.edu',
role: 'Teacher',
job_title: 'Defense against dark arts',
grade_levels: ['High School'],
school_type: 'public',
school_state: 'NY',
school_district_other: true,
school_district_name: 'Hogwarts'
}

test 'create creates a new regional partner contact' do
assert_creates Pd::RegionalPartnerContact do
put :create, params: {
form_data: SAMPLE_FORM_DATA
}
end

assert_response :created
end

test 'create returns appropriate errors if school district data is missing' do
new_form = SAMPLE_FORM_DATA.dup
new_form.delete :school_district_name

assert_does_not_create Pd::RegionalPartnerContact do
put :create, params: {
form_data: new_form
}
end

assert_response :bad_request
response_body = JSON.parse(@response.body)

assert_equal 'Please fill out data for your school district', response_body['general_error']
end
end
6 changes: 6 additions & 0 deletions dashboard/test/factories/pd_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,10 @@
factory :pd_pre_workshop_survey, class: 'Pd::PreWorkshopSurvey' do
association :pd_enrollment
end

factory :pd_regional_partner_contact, class: 'Pd::RegionalPartnerContact' do
user nil
regional_partner nil
form_data nil
end
end
94 changes: 94 additions & 0 deletions dashboard/test/models/pd/regional_partner_contact_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'test_helper'

class Pd::RegionalPartnerContactTest < ActiveSupport::TestCase
test 'Test district validation' do
contact = build :pd_regional_partner_contact, form_data: {}.to_json
refute contact.valid?

form_data = {
first_name: 'firstName',
last_name: 'lastName',
title: 'Dr.',
email: 'email',
role: 'School Administrator',
job_title: 'title',
grade_levels: ['High School'],
school_state: 'NY'
}

refute build(:pd_regional_partner_contact, form_data: form_data.to_json).valid?

refute build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'public',
}
).to_json
).valid?

refute build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'private',
}
).to_json
).valid?

refute build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'public',
school_district_other: true
}
).to_json
).valid?

refute build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'public',
school_district_other: false
}
).to_json
).valid?

assert build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'public',
school_district_other: true,
school_district_name: 'District name'
}
).to_json
).valid?

assert build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'public',
school_district_other: false,
school_district: 'District'
}
).to_json
).valid?

refute build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'private',
school_name: 'Name'
}
).to_json
).valid?

assert build(
:pd_regional_partner_contact, form_data: form_data.merge(
{
school_type: 'private',
school_name: 'Name',
school_zipcode: 'Zipcode'
}
).to_json
).valid?
end
end

0 comments on commit 560b8e7

Please sign in to comment.