Simplify js by including full path in district and school options values

This commit is contained in:
Alex Basson 2021-10-05 10:39:18 -04:00
parent 4d6eb4ecf1
commit f8166aed47
7 changed files with 35 additions and 44 deletions

View file

@ -10,8 +10,6 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

View file

@ -1,34 +1,9 @@
document.addEventListener("DOMContentLoaded", function() {
const selectSchoolElement = document.querySelector('#select-school');
selectSchoolElement.addEventListener('change', (event) => {
change_school(event);
document.querySelector('#select-school').addEventListener('change', (event) => {
window.location = event.target.value;
});
const selectDistrictElement = document.querySelector('#select-district');
selectDistrictElement.addEventListener('change', (event) => {
change_district(event);
document.querySelector('#select-district').addEventListener('change', (event) => {
window.location = event.target.value;
});
});
function change_school(event) {
const school_slug = event.target.value;
const district_regex = /districts\/(.+)\/schools/;
const district_slug = window.location.pathname.match(district_regex)[1];
const year_range_regex = /year=(.+)/;
const year_range = window.location.search.match(year_range_regex)[1];
window.location = `/districts/${district_slug}/schools/${school_slug}/dashboard?year=${year_range}`;
};
function change_district(event) {
const district_slug = event.target.value;
const year_range_regex = /year=(.+)/;
const year_range = window.location.search.match(year_range_regex)[1];
window.location = `/districts/${district_slug}/schools/first/dashboard?year=${year_range}`;
};